1# Environment: Device Environment Query
2
3
4If you need the environment parameters of the device where the application runs to set different scenarios, such as multi-language support and dark/light color mode, use Environment to query the parameters.
5
6
7Environment 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.
8
9
10## Environment Built-in Parameters
11
12| Key| Data Type| Description                                     |
13| ------------------ | ------------------ | ------------------ |
14| accessibilityEnabled              | boolean                  | Whether to enable accessibility.                |
15| colorMode              | ColorMode                  | Color mode. The options are as follows:<br>- **ColorMode.LIGHT**: light mode.<br>- **ColorMode.DARK**: dark mode.                |
16| fontScale              | number                  | Font scale. To enable the font scale to change with the system, set the [configuration](./app-configuration-file.md#configuration) tag.               |
17| fontWeightScale              | number                  | Font weight.               |
18| layoutDirection              | LayoutDirection                  | Layout direction. The options are as follows:<br>- **LayoutDirection.LTR**: from left to right.<br>- **LayoutDirection.RTL**: from right to left.                |
19| languageCode              | string                  | Current system language. The value is in lowercase, for example, **zh**.                |
20
21
22## Use Scenarios
23
24
25### Accessing Environment Parameters from UI
26
27- Use **Environment.envProp** to save the environment variables of the device to AppStorage.
28
29  ```ts
30  // Save languageCode to AppStorage. The default value is en.
31  Environment.envProp('languageCode', 'en');
32  ```
33
34- Decorate the variables with \@StorageProp to link them with components.
35
36  ```ts
37  @StorageProp('languageCode') lang : string = 'en';
38  ```
39
40The chain of updates is as follows: Environment > AppStorage > Component.
41
42> **NOTE**
43>
44> 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.
45
46
47```ts
48// Save the device language code to AppStorage.
49Environment.envProp('languageCode', 'en');
50
51@Entry
52@Component
53struct Index {
54  @StorageProp('languageCode') languageCode: string = 'en';
55
56  build() {
57    Row() {
58      Column() {
59        // Output the current device language code.
60        Text(this.languageCode)
61      }
62    }
63  }
64}
65```
66
67
68### Using Environment in Application Logic
69
70
71```ts
72// Use Environment.EnvProp to save the device language code to AppStorage.
73Environment.envProp('languageCode', 'en');
74// Obtain the one-way bound languageCode variable from AppStorage.
75const lang: SubscribedAbstractProperty<string> = AppStorage.prop('languageCode');
76
77if (lang.get() === 'en') {
78  console.info('Hi');
79} else {
80  console.info('Bonjour');
81}
82```
83
84
85## Constraints
86
87
88Environment 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.
89
90
91```ts
92// EntryAbility.ets
93import { UIAbility } from '@kit.AbilityKit';
94import { window } from '@kit.ArkUI';
95
96export default class EntryAbility extends UIAbility {
97  onWindowStageCreate(windowStage: window.WindowStage) {
98    windowStage.loadContent('pages/Index');
99    let window = windowStage.getMainWindow()
100    window.then(window => {
101      let uicontext = window.getUIContext()
102      uicontext.runScopedTask(() => {
103        Environment.envProp('languageCode', 'en');
104      })
105    })
106  }
107}
108```
109