1# Multi-Language Capability 2 3 4Applications 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. 5 6 7You only need to perform operations in [Resource Files](#resource-files) and [Resource Reference](#resource-reference) to use the multi-language capability of this framework. For details about how to obtain the current system language, see [Language Acquisition](#language-acquisition). 8 9 10## Resource Files 11 12Resource files store application content in multiple languages. This framework uses JSON files to store resource definitions. Place the resource file of each locale in the i18n directory described in [File Organization](js-framework-file.md). 13 14Resource 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, en-US for simplified Chinese, or omit both the script and region, for example, zh for Chinese. 15 16 17``` 18language[-script-region].json 19``` 20 21The following table describes the requirements for the qualifiers of resource file names. 22 23Table 1 Requirements for qualifier values 24 25| Qualifier Type | Description and Value Range | 26| -------- | -------- | 27| 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). | 28| 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). | 29| Country/Region | Indicates the country or region where a 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). | 30 31If 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. 32 33The format of the resource file content is as follows: 34 35**en-US.json** 36 37``` 38{ 39 "strings": { 40 "hello": "Hello world!", 41 "object": "Object parameter substitution-{name}", 42 "array": "Array type parameter substitution-{0}", 43 "symbol": "@#$%^&*()_+-={}[]\\|:;\"'<>,./?" 44 }, 45 46 "files": { 47 "image": "image/en_picture.PNG" 48 } 49} 50``` 51 52 53Different 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. 54 55 56The following example takes **en-US.json** and **ar-AE.json** as examples: 57 58**en-US.json** 59 60```json 61{ 62 "strings": { 63 "people": { 64 "one": "one person", 65 "other": "{count} people" 66 } 67 } 68} 69``` 70 71 72ar-AE.json 73 74```json 75{ 76 "strings": { 77 "people": { 78 "zero": "لا أحد", 79 "one": "وحده", 80 "two": "اثنان", 81 "few": "ستة اشخاص", 82 "many": "خمسون شخص", 83 "other": "مائة شخص" 84 } 85 } 86} 87``` 88 89 90## Resource Reference 91 92Multi-language syntax used on application development pages (including simple formatting and singular-plural formatting) can be used in .hml or .js files. 93 94- Simple formatting 95 96 Use 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. 97 98 Table 2 Simple formatting 99 100 | Attribute | Type | Parameter | Mandatory | Description | 101 | -------- | -------- | -------- | -------- | -------- | 102 | $t | Function | See Table3 | Yes | Sets the parameters based on the system language, for example, this.$t('strings.hello'). | 103 104 105Table 3 $t function parameters 106 107| Parameter | Type | Mandatory | Description | 108| -------- | -------- | -------- | -------- | 109| path | string | Yes | Path of the language resource key | 110| 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']```. | 111 112- Example code for simple formatting 113 114 ```html 115 <!-- xxx.hml --> 116 <div> 117 <!-- Display Hello world! without using a placeholder. --> 118 <text>{{ $t('strings.hello') }}</text> 119 <!-- Replace named placeholder {name} with Hello world and display it. --> 120 <text>{{ $t('strings.object', { name: 'Hello world' }) }}</text> 121 <!-- Replace digit placeholder {0} with Hello world and display it. --> 122 <text>{{ $t('strings.array', ['Hello world']) }}</text> 123 <!-- Obtain the resource content from the .js file and display Hello world. --> 124 <text>{{ hello }}</text> 125 <!-- Obtain the resource content from the .js file, replace named placeholder {name} with Hello world, and display Substitution in an object: Hello world. --> 126 <text>{{ replaceObject }}</text> 127 <!-- Obtain the resource content from the .js file, replace digit placeholder {0} with Hello world, and display Substitution in an array: Hello world. --> 128 <text>{{ replaceArray }}</text> 129 130 <!-- Display the image in the specified file path. --> 131 <image src="{{ $t('files.image') }}" class="image"></image> 132 <!-- Obtain the image file path from the .js file and display the image. --> 133 <image src="{{ replaceSrc }}" class="image"></image> 134 </div> 135 ``` 136 137 ```js 138 // xxx.js 139 // The following example uses the $t function in the .js file. 140 export default { 141 data: { 142 hello: '', 143 replaceObject: '', 144 replaceArray: '', 145 replaceSrc: '', 146 }, 147 onInit() { 148 this.hello = this.$t('strings.hello'); 149 this.replaceObject = this.$t('strings.object', { name: 'Hello world' }); 150 this.replaceArray = this.$t('strings.array', ['Hello world']); 151 this.replaceSrc = this.$t('files.image'); 152 }, 153 } 154 ``` 155 156- Singular-plural formatting 157 158 Table 4 Singular-plural formatting 159 160 | Attribute | Type | Parameter | Mandatory | Description | 161 | -------- | -------- | -------- | -------- | -------- | 162 | $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. | 163 164 Table 5 $tc function parameters 165 166 | Parameter | Type | Mandatory | Description | 167 | -------- | -------- | -------- | -------- | 168 | path | string | Yes | Path of the language resource key | 169 | count | number | Yes | Number | 170 171- Sample code for singular-plural formatting 172 173 ```html 174 <!--xxx.hml--> 175 <div> 176 <!-- When the value 0 is passed, "0 people" matches the Arabic string whose key is zero. --> 177 <text>{{ $tc('strings.people', 0) }}</text> 178 <!-- When the value 1 is passed, "1 person" matches the Arabic string whose key is one. --> 179 <text>{{ $tc('strings.people', 1) }}</text> 180 <!-- When the value 2 is passed, "2 people" matches the Arabic string whose key is two. --> 181 <text>{{ $tc('strings.people', 2) }}</text> 182 <!-- When the value 6 is passed, "6 people" matches the Arabic string whose key is few. --> 183 <text>{{ $tc('strings.people', 6) }}</text> 184 <!-- When the value 50 is passed, "50 people" matches the Arabic string whose key is many. --> 185 <text>{{ $tc('strings.people', 50) }}</text> 186 <!-- When the value 100 is passed, "100 people" matches the Arabic string whose key is other. --> 187 <text>{{ $tc('strings.people', 100) }}</text> 188 </div> 189 ``` 190 191 192## Language Acquisition 193 194For details about how to obtain the language, see [Configuration](../reference/apis/js-apis-application-configuration.md).