What are HarmonyOS NEXT - Type Definition and I18n?

Resource Resource reference type, used to set the value of component properties. You can create Resource type objects through $r or $rawfile, but you cannot modify the values of various properties in Resource. $r('belonging.type.name') ·belonging: System or application resources, with corresponding values of 'sys' and' app '; ·type: Resource type, supports' string ',' color'、'boolean'、'float'、'intarray'、'integer'、'pattern'、'plural'、'strarray'、'media'; ·name: Resource name, determined during resource definition. $rawfile('filename') ·filename: The file name in the resources/rawfile directory of the project. We recommend that you prioritize using the Resource type and store resource files (strings, images, audio, etc.) in the resources directory for developers to maintain. At the same time, the system can load appropriate resources according to the current configuration, for example, developers can present different layout effects based on screen size, or provide different strings based on language settings. Color code reference: https://www.runoob.com/cssref/css-colornames.html Code Example: ResourcePage @Entry @Component struct ResourcePage { @State message: string = 'Resource'; build() { Column() { Text(this.message) .fontSize(30) .fontWeight(FontWeight.Bold) Button($r('app.string.label_login')).backgroundColor($r('app.color.btn_color')) Image($rawfile('cat.jpg')).size({width:100}) } .height('100%') .width('100%') } } Internationalization @ohos.i18n This module provides system related or enhanced internationalization capabilities, including regional management, phone number processing, calendar, etc. The relevant interfaces are supplementary interfaces not defined in the ECMA 402 standard. The Intl module provides the basic internationalization interface defined by the ECMA 402 standard, and when used in conjunction with this module, it can provide complete internationalization support capabilities. Import module import { i18n } from '@kit.LocalizationKit'; The text is localized and displayed according to the specified country. static getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string Code Examples import { BusinessError } from '@kit.BasicServicesKit'; try { let displayCountry: string = i18n.System.getDisplayCountry("zh-CN", "en-GB"); // displayCountry = "China" } catch (error) { let err: BusinessError = error as BusinessError; console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`); } Localized display of text in the specified language. [code] static getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string code example import { BusinessError } from '@kit.BasicServicesKit'; try { let displayLanguage: string = i18n.System.getDisplayLanguage("zh", "en-GB"); // Display Chinese in English format, displayLanguage = Chinese } catch(error) { let err: BusinessError = error as BusinessError; console.error(call System.getDisplayLanguage failed, error code: ${err.code}, message: ${err.message}.); } List of countries or regions supported by the system for the input language. static getSystemCountries(language: string): Array Code Examples import { BusinessError } from '@kit.BasicServicesKit'; try { let systemCountries: Array = i18n.System.getSystemCountries('zh'); // systemCountries = [ "ZW", "YT", "YE", ..., "ER", "CN", "DE" ] } catch(error) { let err: BusinessError = error as BusinessError; console.error(call System.getSystemCountries failed, error code: ${err.code}, message: ${err.message}.); } Determine if the current language and region match. static isSuggested(language: string, region?: string): boolean Code Examples import { BusinessError } from '@kit.BasicServicesKit'; try { let res: boolean = i18n.System.isSuggested('zh', 'CN'); // res = true } catch(error) { let err: BusinessError = error as BusinessError; console.error(call System.isSuggested failed, error code: ${err.code}, message: ${err.message}.); } Set the preferred language for the application. After setting the preferred language to "default", the application language will follow the system language and the cold start of the application will take effect. static setAppPreferredLanguage(language: string): void Code Examples import { BusinessError } from '@kit.BasicServicesKit'; try { i18n.System.setAppPreferredLanguage('zh'); // Set the current preferred language for the application to "zh" } catch(error) { let err: BusinessError = error as BusinessError; console.error(call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.); }

Mar 25, 2025 - 05:51
 0
What are HarmonyOS NEXT - Type Definition and I18n?

Resource
Resource reference type, used to set the value of component properties.

You can create Resource type objects through $r or $rawfile, but you cannot modify the values of various properties in Resource.

$r('belonging.type.name')
·belonging: System or application resources, with corresponding values of 'sys' and' app ';
·type: Resource type, supports' string ',' color'、'boolean'、'float'、'intarray'、'integer'、'pattern'、'plural'、'strarray'、'media';
·name: Resource name, determined during resource definition.

$rawfile('filename')
·filename: The file name in the resources/rawfile directory of the project.

We recommend that you prioritize using the Resource type and store resource files (strings, images, audio, etc.) in the resources directory for developers to maintain. At the same time, the system can load appropriate resources according to the current configuration, for example, developers can present different layout effects based on screen size, or provide different strings based on language settings.

Color code reference:
https://www.runoob.com/cssref/css-colornames.html

Code Example: ResourcePage

@Entry
@Component
struct ResourcePage {
  @State message: string = 'Resource';

  build() {
    Column() {
      Text(this.message)
        .fontSize(30)
        .fontWeight(FontWeight.Bold)

      Button($r('app.string.label_login')).backgroundColor($r('app.color.btn_color'))

      Image($rawfile('cat.jpg')).size({width:100})
    }
    .height('100%')
    .width('100%')
  }
}

Internationalization @ohos.i18n
This module provides system related or enhanced internationalization capabilities, including regional management, phone number processing, calendar, etc. The relevant interfaces are supplementary interfaces not defined in the ECMA 402 standard. The Intl module provides the basic internationalization interface defined by the ECMA 402 standard, and when used in conjunction with this module, it can provide complete internationalization support capabilities.

Import module

import { i18n } from '@kit.LocalizationKit';

The text is localized and displayed according to the specified country.

static getDisplayCountry(country: string, locale: string, sentenceCase?: boolean): string

Code Examples

import { BusinessError } from '@kit.BasicServicesKit';

try {
    let displayCountry: string = i18n.System.getDisplayCountry("zh-CN", "en-GB"); // displayCountry = "China"
} catch (error) {
    let err: BusinessError = error as BusinessError;
    console.error(`call System.getDisplayCountry failed, error code: ${err.code}, message: ${err.message}.`);
}

Localized display of text in the specified language.
[code]
static getDisplayLanguage(language: string, locale: string, sentenceCase?: boolean): string

code example

import { BusinessError } from '@kit.BasicServicesKit';

try {
let displayLanguage: string = i18n.System.getDisplayLanguage("zh", "en-GB"); // Display Chinese in English format, displayLanguage = Chinese
} catch(error) {
let err: BusinessError = error as BusinessError;
console.error(call System.getDisplayLanguage failed, error code: ${err.code}, message: ${err.message}.);
}


List of countries or regions supported by the system for the input language.

static getSystemCountries(language: string): Array


Code Examples

import { BusinessError } from '@kit.BasicServicesKit';

try {
let systemCountries: Array = i18n.System.getSystemCountries('zh'); // systemCountries = [ "ZW", "YT", "YE", ..., "ER", "CN", "DE" ]
} catch(error) {
let err: BusinessError = error as BusinessError;
console.error(call System.getSystemCountries failed, error code: ${err.code}, message: ${err.message}.);
}


Determine if the current language and region match.

static isSuggested(language: string, region?: string): boolean

Code Examples

import { BusinessError } from '@kit.BasicServicesKit';

try {
let res: boolean = i18n.System.isSuggested('zh', 'CN'); // res = true
} catch(error) {
let err: BusinessError = error as BusinessError;
console.error(call System.isSuggested failed, error code: ${err.code}, message: ${err.message}.);
}


Set the preferred language for the application. After setting the preferred language to "default", the application language will follow the system language and the cold start of the application will take effect.

static setAppPreferredLanguage(language: string): void

Code Examples

import { BusinessError } from '@kit.BasicServicesKit';

try {
i18n.System.setAppPreferredLanguage('zh'); // Set the current preferred language for the application to "zh"
} catch(error) {
let err: BusinessError = error as BusinessError;
console.error(call System.setAppPreferredLanguage failed, error code: ${err.code}, message: ${err.message}.);
}