harmony 鸿蒙Preferred Language Setting
Preferred Language Setting
Use Cases
Multi-language users usually set the system language to one language (for example, Chinese) and the language of a specific application to another language (for example, English). When application resources are loaded on the UI, it is expected that the resources be displayed in the language set for the application. To address this issue, you can set a preferred language in locale settings so that resources are loaded in the preferred language when the application is launched. Currently, only one preferred language can be set for an application.
How to Develop
For details about how to use related APIs, see getAppPreferredLanguage.
Import the i18n and intl modules.
import { i18n } from '@kit.LocalizationKit'; import { BusinessError } from '@kit.BasicServicesKit';
Obtain the preferred language of the application.
let appPreferredLanguage: string = i18n.System.getAppPreferredLanguage(); // Obtain the preferred language of the application.
Set the preferred language of the application. After the preferred language of the application is set to the target language, the application UI is switched to the target language. The setting is subject only to the application. It does not affect the system language settings.
try { i18n.System.setAppPreferredLanguage('zh-Hans'); // Set the preferred language of the application to zh-Hans. } catch (error) { let err: BusinessError = error as BusinessError; console.error(`call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); }
Clear the preferred language of the application. If the preferred language is set to default, the application’s language will be the same as the system language, and the setting will take effect upon application restarting.
try { i18n.System.setAppPreferredLanguage ('default'); // Clear the preferred language of the application. } catch(error) { let err: BusinessError = error as BusinessError; console.error(`call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.`); }
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Character Processing
harmony 鸿蒙Overview of Internationalization and Localization
harmony 鸿蒙Language and Locale Name Localization
harmony 鸿蒙Locale ID and Cultural Habit Division
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦