Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick app capabilities are oriented for multiple countries and regions and provide the multi-language capability to enable the RPK file of a quick app to switch between multiple languages. In this way, you do not need to create multiple source code projects for different languages, making it easier for project maintenance.
If your quick app uses the default system language of the device, you only need to define and reference resources to implement the multi-language capability. To allow users to change the locale in the quick app, please refer to Obtaining Language Updates.
The quick app platform uses a JSON resource file to store service information definitions of multiple languages, while other technical platforms usually define service information in .properties or .xml files.
Define the i18n folder in the src directory of the source code project and store a resource file for each language/region in the folder. If a language/region has multiple resource files, file matching is performed by priority. For example, for zh-CN, the matching sequence is as follows: zh-CN -> zh -> zh-* -> defaults. If there are multiple zh-* files, they are sorted in ascending alphabetical order.
If your app is planned to support only one language, but you still need to use the formatting, singular-plural conversion, and localization of multi-language support, just declare the defaults.json file.
The format of a JSON file is as follows:
- {
- "message": {
- "appName":"Quick App Sample",
- "pageA": {
- "pageTitle": "Quick App feature display",
- "text": "pure-text-content",
- "format": {
- "object": "type-{name}",
- "array": "type-{0}"
- },
- "array": [
- "item-type-string",
- {
- "key": "item-type-object-attribute"
- },
- ["item-type-array"]
- ],
- "plurals": {
- "double": "car | cars",
- "three": "no apples | one apple | {count} apples",
- "format": {
- "object": "type-{name}",
- "array": "type-{0}"
- }
- }
- }
- }
- }
A page can reference pure-text-content using a path similar to message.pageA.text.
The functions in ViewModel, for example, $t, implement multi-language capabilities. You can call these functions in the template or script element.
The sample code is as follows:
- <template>
- <div>
- <text>{{ $t('message.pageA.text') }}</text>
- <text>{{ $t('message.pageA.format.object', { name: 'arg-object' }) }}</text>
- <text>{{ $t('message.pageA.format.array', ['arg-array']) }}</text>
- </div>
- </template>
- <script>
- export default {
- onInit () {
- // Simple formatting:
- this.$t('message.pageA.text')
- this.$t('message.pageA.format.object', { name: 'arg-object' })
- this.$t('message.pageA.format.array', ['arg-array'])
- }
- }
- </script>
Attribute | Type | Parameters | Description |
|---|---|---|---|
$t | Function |
| Set the parameters based on the system language, for example, this.$t('message.pageA.text'). |
Sample code
- // Sample 1: Formatting without additional parameters
- // Output: "pure-text-content"
- this.$t('message.pageA.text')
- // Sample 2: The additional parameter is an object, which is used to replace the referenced content.
- // Output: "type-arg-object"
- this.$t('message.pageA.format.object', { name: 'arg-object' })
- // Sample 3: The additional parameter is an array, which is used to replace the referenced content.
- // Output: "type-arg-array"
- this.$t('message.pageA.format.array', ['arg-array'])
Attribute | Type | Parameters | Description |
|---|---|---|---|
$tc | Function |
| this.$tc('message.plurals.double'): defines the singular and plural forms based on the system language. NOTE The content of a resource is separated by vertical bars (|). |
Sample code
- // Sample 1: When the value of message contains two options, the passed value is not the singular form.
- // Output: "cars"
- this.$tc('message.pageA.plurals.double', 0)
- // Sample 2: When the value of message contains two options, the passed value is in the singular form.
- // Output: "car"
- this.$tc('message.pageA.plurals.double', 1)
- // Sample 3: When the value of message contains two options, the passed value is not in the singular form.
- // Output: "cars"
- this.$tc('message.pageA.plurals.double', 2)
- // Sample 4: When the value of message contains three or more options, the passed value is not in the singular form.
- // Output: "no apples"
- this.$tc('message.pageA.plurals.three', 0)
- // Sample 5: When the value of message contains three or more options, the passed value is in the singular form.
- // Output: "one apple"
- this.$tc('message.pageA.plurals.three', 1)
- // Sample 6: When the value of message contains three or more options, the passed value is not in the singular form.
- // Output: "10 apples"
- this.$tc('message.pageA.plurals.three', 10)
In some scenarios, you may need to obtain and change the locale of the current system to implement different logic processing. The page layout may vary according to the locale. You can provide the language change function for users through app configuration.
- import configuration from '@system.configuration'
- // Obtain the locale. You can set the locale to the data attribute in the VM and determine the locale in the template to further determine the layout.
- const localeObject = configuration.getLocale()
- // Convert the locale to the character string format, for example, zh or zh-CN.
- const locale = [localeObject.language, localeObject.countryOrRegion]
- .filter(n => !!n)
- .join('-')
- console.info(`Obtain the current locale: ${locale}`)
- import configuration from '@system.configuration'
- // Triggered by the VM lifecycle function onConfigurationChanged after locale is set successfully.
- configuration.setLocale({
- language: 'zh',
- countryOrRegion: 'CN'
- })
The onConfigurationChanged callback is executed when a user modifies system settings or configuration.setLocale is called to change the locale.
- onConfigurationChanged(data) {
- // Output {"type":"locale"}
- console.info(JSON.stringify(data))
- }
Internationalize the app name and page title configured in the manifest.json file.