1# Environment: Device Environment Query 2 3 4You 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. 5 6 7Environment is a singleton object created by the ArkUI framework at application start. 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 type only. 8 9 10## Application Scenarios 11 12 13### Accessing Environment Parameters from UI 14 15- Use **Environment.EnvProp** to save the environment variables of the device to AppStorage. 16 17 ```ts 18 // Save the language code of the device to AppStorage. The default value is en. 19 // Whenever its value changes in the device environment, it will update its value in AppStorage. 20 Environment.EnvProp('languageCode', 'en'); 21 ``` 22 23- To keep a component variable updated with changes in the device environment, this variable should be decorated with \@StorageProp. 24 25 ```ts 26 @StorageProp('languageCode') lang : string = 'en'; 27 ``` 28 29The chain of updates is as follows: Environment > AppStorage > Component. 30 31> **NOTE** 32> 33> 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. 34 35 36```ts 37// Save the device language code to AppStorage. 38Environment.EnvProp('languageCode', 'en'); 39let enable = AppStorage.Get('languageCode'); 40 41@Entry 42@Component 43struct Index { 44 @StorageProp('languageCode') languageCode: string = 'en'; 45 46 build() { 47 Row() { 48 Column() { 49 // Output the current device language code. 50 Text(this.languageCode) 51 } 52 } 53 } 54} 55``` 56 57 58### Using Environment in Application Logic 59 60 61```ts 62// Use Environment.EnvProp to save the device language code to AppStorage. 63Environment.EnvProp('languageCode', 'en'); 64// Obtain the one-way bound languageCode variable from AppStorage. 65const lang: SubscribedAbstractProperty<string> = AppStorage.Prop('languageCode'); 66 67if (lang.get() === 'en') { 68 console.info('Hi'); 69} else { 70 console.info('Hello!'); 71} 72``` 73