harmony 鸿蒙Preferred Language Setting

  • 2025-06-12
  • 浏览 (4)

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.

  1. Import the i18n and intl modules.

    import { i18n } from '@kit.LocalizationKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
  2. Obtain the preferred language of the application.

    let appPreferredLanguage: string = i18n.System.getAppPreferredLanguage(); // Obtain the preferred language of the application.
    
  3. 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}.`);
    }
    
  4. 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 鸿蒙Localization Kit

harmony 鸿蒙Calendar Setting

harmony 鸿蒙Character Processing

harmony 鸿蒙Overview

harmony 鸿蒙DST Transition

harmony 鸿蒙Overview of Internationalization and Localization

harmony 鸿蒙Language and Locale Name Localization

harmony 鸿蒙Locale ID and Cultural Habit Division

harmony 鸿蒙Number and Unit of Measurement Formatting

harmony 鸿蒙Phone Number Formatting

0  赞