1e41f4b71Sopenharmony_ci# Environment: Querying Device Environment 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciYou 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 Environment for device environment query. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciEnvironment is a singleton object created by the ArkUI framework at application startup. It provides a range of application state attributes to AppStorage that describe the device environment in which the application is running. Environment and its attributes are immutable. All property values are of simple types only. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## Environment Built-in Parameters 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci| Key| Data Type| Description | 13e41f4b71Sopenharmony_ci| ------------------ | ------------------ | ------------------ | 14e41f4b71Sopenharmony_ci| accessibilityEnabled | boolean | Whether to enable accessibility. | 15e41f4b71Sopenharmony_ci| colorMode | ColorMode | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode. | 16e41f4b71Sopenharmony_ci| fontScale | number | Font scale. Range: [0.85, 1.45]. | 17e41f4b71Sopenharmony_ci| fontWeightScale | number | Font weight scale. Range: [0.6, 1.6]. | 18e41f4b71Sopenharmony_ci| layoutDirection | LayoutDirection | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: from left to right.<br>- **LayoutDirection.RTL**: from right to left. | 19e41f4b71Sopenharmony_ci| languageCode | string | Current system language. The value is in lowercase, for example, **zh**. | 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Use Scenarios 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci### Accessing Environment Parameters from UI 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci- Use **Environment.envProp** to save the environment variables of the device to AppStorage. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```ts 30e41f4b71Sopenharmony_ci // Save languageCode to AppStorage. The default value is en. 31e41f4b71Sopenharmony_ci Environment.envProp('languageCode', 'en'); 32e41f4b71Sopenharmony_ci ``` 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci- Decorate the variables with \@StorageProp to link them with components. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci ```ts 37e41f4b71Sopenharmony_ci @StorageProp('languageCode') lang : string = 'en'; 38e41f4b71Sopenharmony_ci ``` 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ciThe chain of updates is as follows: Environment > AppStorage > Component. 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci> **NOTE** 43e41f4b71Sopenharmony_ci> 44e41f4b71Sopenharmony_ci> An \@StorageProp decorated variable can be locally modified, but the change will not be updated to AppStorage. This is because the environment variable parameters are read-only to the application. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci```ts 48e41f4b71Sopenharmony_ci// Save the device language code to AppStorage. 49e41f4b71Sopenharmony_ciEnvironment.envProp('languageCode', 'en'); 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci@Entry 52e41f4b71Sopenharmony_ci@Component 53e41f4b71Sopenharmony_cistruct Index { 54e41f4b71Sopenharmony_ci @StorageProp('languageCode') languageCode: string = 'en'; 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci build() { 57e41f4b71Sopenharmony_ci Row() { 58e41f4b71Sopenharmony_ci Column() { 59e41f4b71Sopenharmony_ci // Output the current device language code. 60e41f4b71Sopenharmony_ci Text(this.languageCode) 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci } 64e41f4b71Sopenharmony_ci} 65e41f4b71Sopenharmony_ci``` 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci### Using Environment in Application Logic 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci```ts 72e41f4b71Sopenharmony_ci// Use Environment.EnvProp to save the device language code to AppStorage. 73e41f4b71Sopenharmony_ciEnvironment.envProp('languageCode', 'en'); 74e41f4b71Sopenharmony_ci// Obtain the one-way bound languageCode variable from AppStorage. 75e41f4b71Sopenharmony_ciconst lang: SubscribedAbstractProperty<string> = AppStorage.prop('languageCode'); 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ciif (lang.get() === 'en') { 78e41f4b71Sopenharmony_ci console.info('Hi'); 79e41f4b71Sopenharmony_ci} else { 80e41f4b71Sopenharmony_ci console.info('Bonjour'); 81e41f4b71Sopenharmony_ci} 82e41f4b71Sopenharmony_ci``` 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci## Restrictions 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ciEnvironment can be called only when the [UIContext](../reference/apis-arkui/js-apis-arkui-UIContext.md#uicontext) is specified. The UIContext is specified when [runScopedTask](../reference/apis-arkui/js-apis-arkui-UIContext.md#runscopedtask) is called. If Environment is called otherwise, no device environment data can be obtained. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci```ts 92e41f4b71Sopenharmony_ci// EntryAbility.ets 93e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 94e41f4b71Sopenharmony_ciimport { window } from '@kit.ArkUI'; 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 97e41f4b71Sopenharmony_ci onWindowStageCreate(windowStage: window.WindowStage) { 98e41f4b71Sopenharmony_ci windowStage.loadContent('pages/Index'); 99e41f4b71Sopenharmony_ci let window = windowStage.getMainWindow() 100e41f4b71Sopenharmony_ci window.then(window => { 101e41f4b71Sopenharmony_ci let uicontext = window.getUIContext() 102e41f4b71Sopenharmony_ci uicontext.runScopedTask(() => { 103e41f4b71Sopenharmony_ci Environment.envProp('languageCode', 'en'); 104e41f4b71Sopenharmony_ci }) 105e41f4b71Sopenharmony_ci }) 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci} 108e41f4b71Sopenharmony_ci``` 109