1e41f4b71Sopenharmony_ci# Multi-Language Capability
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciApplications designed based on the JS UI framework apply to different countries and regions. With the multi-language capability, you do not need to develop application versions in different languages, and your users can switch between various locales. This also facilitates project maintenance.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciYou only need to perform operations in [Defining Resource Files](#defining-resource-files) and [Referencing Resources](#referencing-resources) to use the multi-language capability of this framework. For details about how to obtain the current system language, see [Obtaining the Language](#obtaining-the-language).
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Defining Resource Files
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciResource files store application content in multiple languages. This framework uses JSON files to store resource definitions.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciPlace the resource file of each locale in the i18n directory described in [File Organization](js-framework-file.md). Resource files should be named in _language-script-region_.json format. For example, the resource file for Hong Kong(China) in the traditional script is named zh-Hant-HK. You can omit the region, for example, zh-CN for simplified Chinese, or omit both the script and region, for example, zh for Chinese.  
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci```
16e41f4b71Sopenharmony_cilanguage[-script-region].json
17e41f4b71Sopenharmony_ci```
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe following table describes the requirements for the qualifiers of resource file names.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ciTable 1 Requirements for qualifier values
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci| Qualifier Type| Description and Value Range                                 |
24e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- |
25e41f4b71Sopenharmony_ci| Language   | Indicates the language used by the device. The value consists of two or three lowercase letters. For example, **zh** indicates Chinese, **en** indicates English, and **mai** indicates Maithili.<br>For details about the value range, refer to **ISO 639** (codes for the representation of names of languages).|
26e41f4b71Sopenharmony_ci| Script   | Indicates the script type used by the device. The value starts with one uppercase letter followed by three lowercase letters. For example, **Hans** indicates simplified Chinese, and **Hant** indicates traditional Chinese.<br>For details about the value range, refer to **ISO 15924** (codes for the representation of names of scripts).|
27e41f4b71Sopenharmony_ci| Country/Region| Indicates the country or region where the user is located. The value consists of two or three uppercase letters or three digits. For example, **CN** indicates China, and **GB** indicates the United Kingdom.<br>For details about the value range, refer to **ISO 3166-1** (codes for the representation of names of countries and their subdivisions).|
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciIf there is no resource file of the locale that matches the system language, content in the **en-US.json** file will be used by default.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciThe format of the resource file content is as follows:
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**en-US.json**
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci```json
36e41f4b71Sopenharmony_ci{
37e41f4b71Sopenharmony_ci    "strings": {
38e41f4b71Sopenharmony_ci        "hello": "Hello world!",
39e41f4b71Sopenharmony_ci        "object": "Object parameter substitution-{name}",
40e41f4b71Sopenharmony_ci        "array": "Array type parameter substitution-{0}",
41e41f4b71Sopenharmony_ci        "symbol": "@#$%^&*()_+-={}[]\\|:;\"'<>,./?"
42e41f4b71Sopenharmony_ci    },
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci    "files": {
45e41f4b71Sopenharmony_ci        "image": "image/en_picture.PNG"
46e41f4b71Sopenharmony_ci    }
47e41f4b71Sopenharmony_ci}
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciDifferent languages have different matching rules for singular and plural forms. In the resource file, **zero**, **one**, **two**, **few**, **many**, and **other** are used to define the string content in different singular and plural forms. For example, there is only the **other** scenario in Chinese since the language does not have singular and plural forms. **one** and **other** scenarios are applicable to English. All six scenarios are needed for Arabic.
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ciThe following example takes **en-US.json** and **ar-AE.json** as examples:
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci**en-US.json**
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci```json
59e41f4b71Sopenharmony_ci{
60e41f4b71Sopenharmony_ci    "strings": {
61e41f4b71Sopenharmony_ci        "people": {
62e41f4b71Sopenharmony_ci            "one": "one person",
63e41f4b71Sopenharmony_ci            "other": "{count} people"
64e41f4b71Sopenharmony_ci        }
65e41f4b71Sopenharmony_ci    }
66e41f4b71Sopenharmony_ci}
67e41f4b71Sopenharmony_ci```
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**ar-AE.json**
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci```json
73e41f4b71Sopenharmony_ci{
74e41f4b71Sopenharmony_ci    "strings": {
75e41f4b71Sopenharmony_ci        "people": {
76e41f4b71Sopenharmony_ci            "zero": "لا أحد",
77e41f4b71Sopenharmony_ci            "one": "وحده",
78e41f4b71Sopenharmony_ci            "two": "اثنان",
79e41f4b71Sopenharmony_ci            "few": "ستة اشخاص",
80e41f4b71Sopenharmony_ci            "many": "خمسون شخص",
81e41f4b71Sopenharmony_ci            "other": "مائة شخص"
82e41f4b71Sopenharmony_ci        }
83e41f4b71Sopenharmony_ci    }
84e41f4b71Sopenharmony_ci}
85e41f4b71Sopenharmony_ci```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci## Referencing Resources
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciMulti-language syntax used on application development pages (including simple formatting and singular-plural formatting) can be used in **.hml** or **.js** files.
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci- Simple formatting
93e41f4b71Sopenharmony_ci  
94e41f4b71Sopenharmony_ciUse the **$t** function to reference to resources of different locales. The **$t** function is available for both **.hml** and **.js** files. The system displays content based on a resource file path specified via **$t** and the specified resource file whose locale matches the current system language.
95e41f4b71Sopenharmony_ci  
96e41f4b71Sopenharmony_ciTable 2 Simple formatting
97e41f4b71Sopenharmony_ci  
98e41f4b71Sopenharmony_ci  | Attribute| Type    | Parameter             | Mandatory| Description                                                  |
99e41f4b71Sopenharmony_ci  | ---- | -------- | ----------------- | ---- | ------------------------------------------------------ |
100e41f4b71Sopenharmony_ci| $t   | Function | See Table 3.| Yes  | Sets the parameters based on the system language, for example, **this.$t('strings.hello')**.|
101e41f4b71Sopenharmony_ci  
102e41f4b71Sopenharmony_ciTable 3 $t function parameters
103e41f4b71Sopenharmony_ci  
104e41f4b71Sopenharmony_ci  | Parameter  | Type         | Mandatory| Description                                                        |
105e41f4b71Sopenharmony_ci  | ------ | ------------- | ---- | ------------------------------------------------------------ |
106e41f4b71Sopenharmony_ci  | path   | string        | Yes  | Path of the language resource key.                                                  |
107e41f4b71Sopenharmony_ci| params | Array\|Object | No  | Content used to replace placeholders during runtime. There are two types of placeholders available:<br>- Named placeholder, for example, **{name}**. The actual content must be of the object type, for example, **$t('strings.object', {name:'Hello world'})**.<br> - Digit placeholder, for example, **{0}**. The actual content must be of the array type, for example, **$t('strings.array', [Hello world']**.|
108e41f4b71Sopenharmony_ci  
109e41f4b71Sopenharmony_ci- Example code for simple formatting
110e41f4b71Sopenharmony_ci  ```html
111e41f4b71Sopenharmony_ci  <!-- xxx.hml -->
112e41f4b71Sopenharmony_ci  <div>
113e41f4b71Sopenharmony_ci    <!-- Display Hello world! without using a placeholder. -->
114e41f4b71Sopenharmony_ci    <text>{{ $t('strings.hello') }}</text>
115e41f4b71Sopenharmony_ci    <!-- Replace named placeholder {name} with Hello world and display it. -->
116e41f4b71Sopenharmony_ci    <text>{{ $t('strings.object', { name: 'Hello world' }) }}</text>
117e41f4b71Sopenharmony_ci    <!-- Replace digit placeholder {0} with Hello world and display it. -->
118e41f4b71Sopenharmony_ci    <text>{{ $t('strings.array', ['Hello world']) }}</text>
119e41f4b71Sopenharmony_ci    <!-- Obtain the resource content from the .js file and display Hello world. -->
120e41f4b71Sopenharmony_ci    <text>{{ hello }}</text>
121e41f4b71Sopenharmony_ci    <!-- Obtain the resource content from the .js file, replace named placeholder {name} with Hello world, and display Substitution in an object: Hello world. -->
122e41f4b71Sopenharmony_ci    <text>{{ replaceObject }}</text>
123e41f4b71Sopenharmony_ci    <!-- Obtain the resource content from the .js file, replace digit placeholder {0} with Hello world, and display Substitution in an array: Hello world. -->
124e41f4b71Sopenharmony_ci    <text>{{ replaceArray }}</text>
125e41f4b71Sopenharmony_ci  
126e41f4b71Sopenharmony_ci    <!-- Display the image in the specified file path. -->
127e41f4b71Sopenharmony_ci    <image src="{{ $t('files.image') }}" class="image"></image>
128e41f4b71Sopenharmony_ci    <!-- Obtain the image file path from the .js file and display the image. -->
129e41f4b71Sopenharmony_ci    <image src="{{ replaceSrc }}" class="image"></image>
130e41f4b71Sopenharmony_ci  </div>
131e41f4b71Sopenharmony_ci  ```
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci  ```js
134e41f4b71Sopenharmony_ci  // xxx.js
135e41f4b71Sopenharmony_ci  // The following example uses the $t function in the .js file.
136e41f4b71Sopenharmony_ci  export default {
137e41f4b71Sopenharmony_ci    data: {
138e41f4b71Sopenharmony_ci      hello: '',
139e41f4b71Sopenharmony_ci      replaceObject: '',
140e41f4b71Sopenharmony_ci      replaceArray: '',
141e41f4b71Sopenharmony_ci      replaceSrc: '',
142e41f4b71Sopenharmony_ci    },
143e41f4b71Sopenharmony_ci    onInit() {
144e41f4b71Sopenharmony_ci      this.hello = this.$t('strings.hello');
145e41f4b71Sopenharmony_ci      this.replaceObject = this.$t('strings.object', { name: 'Hello world' });
146e41f4b71Sopenharmony_ci      this.replaceArray = this.$t('strings.array', ['Hello world']);
147e41f4b71Sopenharmony_ci      this.replaceSrc = this.$t('files.image');
148e41f4b71Sopenharmony_ci    },   
149e41f4b71Sopenharmony_ci  }
150e41f4b71Sopenharmony_ci  ```
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci- Singular-plural formatting
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci  Table 4 Singular-plural formatting
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci  | Attribute| Type    | Parameter              | Mandatory| Description                                                        |
157e41f4b71Sopenharmony_ci  | ---- | -------- | ------------------ | ---- | ------------------------------------------------------------ |
158e41f4b71Sopenharmony_ci  | $tc  | Function | See Table 5.| Yes  | Converts between the singular and plural forms based on the system language, for example, **this.$tc('strings.people')**.<br>**NOTE**<br>The resource content is distinguished by the following JSON keys: **zero**, **one**, **two**, **few**, **many**, and **other**.|
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci  Table 5 $tc function parameters
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci  | Parameter | Type  | Mandatory| Description        |
163e41f4b71Sopenharmony_ci  | ----- | ------ | ---- | ------------ |
164e41f4b71Sopenharmony_ci  | path  | string | Yes  | Path of the language resource key.  |
165e41f4b71Sopenharmony_ci  | count | number | Yes  | Number.|
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ci- Example code for singular-plural formatting
168e41f4b71Sopenharmony_ci  ```html
169e41f4b71Sopenharmony_ci  <!--xxx.hml-->
170e41f4b71Sopenharmony_ci  <div>
171e41f4b71Sopenharmony_ci    <!-- When the value 0 is passed, "0 people" matches the Arabic string whose key is zero. -->
172e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 0) }}</text>
173e41f4b71Sopenharmony_ci    <!-- When the value 1 is passed, "1 person" matches the Arabic string whose key is one. -->
174e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 1) }}</text>
175e41f4b71Sopenharmony_ci    <!-- When the value 2 is passed, "2 people" matches the Arabic string whose key is two. -->
176e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 2) }}</text>
177e41f4b71Sopenharmony_ci    <!-- When the value 6 is passed, "6 people" matches the Arabic string whose key is few. -->
178e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 6) }}</text>
179e41f4b71Sopenharmony_ci    <!-- When the value 50 is passed, "50 people" matches the Arabic string whose key is many. -->
180e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 50) }}</text>
181e41f4b71Sopenharmony_ci    <!-- When the value 100 is passed, "100 people" matches the Arabic string whose key is other. -->
182e41f4b71Sopenharmony_ci    <text>{{ $tc('strings.people', 100) }}</text>
183e41f4b71Sopenharmony_ci  </div>
184e41f4b71Sopenharmony_ci  ```
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci## Obtaining the Language
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ciFor details about how to obtain the language, see [Configuration](../reference/apis-ability-kit/js-apis-app-ability-configuration.md).
190