Applications 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.
You only need to perform operations in Defining Resource Files and 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.
Resource 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. Resource files are named in language-region.json format. For example, the resource file for English (United States) is named en-US.json. If 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.
The format of the resource file content is as follows:
en-US.json
{
"strings": {
"hello": "Hello world!",
"object": "Object parameter substitution-{name}",
"array": "Array type parameter substitution-{0}",
"symbol": "@#$%^&*()_+-={}[]\\|:;\"'<>,./?"
},
"files": {
"image": "image/en_picture.PNG"
}
} 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.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| path | string | Yes | Resource path. |
| params | Array|Object | No | Content used to replace placeholders during runtime. There are two types of placeholders available: Named placeholder, for example, {name}. The actual content must be of the object type, for example, $t('strings.object', { name: 'Hello world' }). Digit placeholder, for example, {0}. The actual content must be of the array type, for example, $t('strings.array', ['Hello world']. |
Sample Code
<!-- xxx.hml -->
<div>
<!-- Display Hello world! without using a placeholder. -->
<text>{{ $t('strings.hello') }}</text>
<!-- Replace named placeholder {name} with Hello world and display it. -->
<text>{{ $t('strings.object', { name: 'Hello world' }) }}</text>
<!-- Replace digit placeholder {0} with Hello world and display it. -->
<text>{{ $t('strings.array', ['Hello world']) }}</text>
<!-- Obtain the resource content from the .js file and display Hello world. -->
<text>{{ hello }}</text>
<!-- Obtain the resource content from the .js file, replace named placeholder {name} with Hello world, and display Substitution in an object: Hello world. -->
<text>{{ replaceObject }}</text>
<!-- Obtain the resource content from the .js file, replace digit placeholder {0} with Hello world, and display Substitution in an array: Hello world. -->
<text>{{ replaceArray }}</text>
<!-- Display the image in the specified file path. -->
<image src="{{ $t('files.image') }}" class="image"></image>
<!-- Obtain the image file path from the .js file and display the image. -->
<image src="{{ replaceSrc }}" class="image"></image>
</div> // xxx.js
// The following example uses the $t function in the .js file.
export default {
data: {
hello: '',
replaceObject: '',
replaceArray: '',
replaceSrc: '',
},
onInit() {
this.hello = this.$t('strings.hello');
this.replaceObject = this.$t('strings.object', { name: 'Hello world' });
this.replaceArray = this.$t('strings.array', ['Hello world']);
this.replaceSrc = this.$t('files.image');
},
}