You may want your application to behave differently based on the device environment where the application is running, for example, switching to dark mode or a specific language. In this case, you need the Environment API for device environment query.
The Environment API is a singleton object created by the ArkUI framework at application startup, providing AppStorage with immutable primitive-type properties reflecting the application's runtime state.
The Environment API enables reading system environment variables and writing their values to AppStorage. You must access these values through AppStorage. For details, see Environment Built-in Parameters.
Before reading this topic, you are advised to read AppStorage.
| Key | Data Type | Description |
|---|---|---|
| accessibilityEnabled | string | Whether to enable the accessibility screen reader. 'true' means enabled, and 'false' means disabled. |
| colorMode | ColorMode | Color mode type. - ColorMode.LIGHT: light color. - ColorMode.DARK: dark color. |
| fontScale | number | Font scale. To enable the font scale to change with the system, set the configuration tag. The default value follows the default system settings. |
| fontWeightScale | number | Font weight scale. The value range of fontWeightScale may vary on different systems or device models. The default value follows the default system parameter. |
| layoutDirection | LayoutDirection | Layout direction. LayoutDirection.LTR: from left to right. LayoutDirection.RTL: from right to left. |
| languageCode | string | System language code. The value must be in lowercase, for example, zh. The default value follows the default system settings. |
Environment can be called only when UIContext is specified. You call this API inside runScopedTask to make context explicit. If the call is not made in a location where the UI context is explicit, no device environment data can be obtained.
import { UIAbility } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends UIAbility {
onWindowStageCreate(windowStage: window.WindowStage) {
windowStage.loadContent('pages/Index');
let mainWindow = windowStage.getMainWindow();
mainWindow.then(mainWindowInstance => {
let uiContext = mainWindowInstance.getUIContext();
// Environment is called in UIContext.runScopedTask to clarify the context.
uiContext.runScopedTask(() => {
Environment.envProp('languageCode', 'en');
});
});
}
} Use Environment.envProp to store device environment variables in AppStorage.
// Store languageCode to AppStorage. The default value is en.
Environment.envProp('languageCode', 'en'); Obtain the value of languageCode in a custom component through @StorageProp.
@StorageProp('languageCode') lang: string = 'en'; The chain of updates is as follows: Environment > AppStorage > Component.
The application cannot modify environment variables. Therefore, @StorageProp is used to obtain them. In this way, even if environment variables are modified in the component, the modification is not synchronized back to AppStorage, affecting the result of obtaining the environment variables in other components.
// Store languageCode to AppStorage.
Environment.envProp('languageCode', 'en');
@Entry
@Component
struct UiEnvironment {
@StorageProp('languageCode') languageCode: string = 'en';
build() {
Row() {
Column() {
// Obtain the current device language code.
Text(this.languageCode)
.fontSize(20)
.margin(10)
}
.width('100%')
}
.height('100%')
}
} 
import { hilog } from '@kit.PerformanceAnalysisKit';
const DOMAIN = 0x0001;
const TAG = 'environmentalProject';
// Store languageCode to AppStorage.
Environment.envProp('languageCode', 'en');
// Obtain the one-way bound languageCode variable from AppStorage.
const lang: SubscribedAbstractProperty<string> = AppStorage.prop('languageCode');
if (lang.get() === 'en') {
// Replace $r('app.string.AppliedLogic_Hello') with the actual resource file. In this example, the value in the resource file is 'Hello!'.
hilog.info(DOMAIN, TAG, `${$r('app.string.AppliedLogic_Hello')}`);
} else {
hilog.info(DOMAIN, TAG, 'Hello!');
}