We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp GuidesQuick AppQuick App DevelopmentDeveloping Your Quick AppInternationalizationMulti-language Capability

Multi-language Capability

Overview

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.

Defining a Resource File

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:

Collapse
Word wrap
Dark theme
Copy code
  1. {
  2. "message": {
  3. "appName":"Quick App Sample",
  4. "pageA": {
  5. "pageTitle": "Quick App feature display",
  6. "text": "pure-text-content",
  7. "format": {
  8. "object": "type-{name}",
  9. "array": "type-{0}"
  10. },
  11. "array": [
  12. "item-type-string",
  13. {
  14. "key": "item-type-object-attribute"
  15. },
  16. ["item-type-array"]
  17. ],
  18. "plurals": {
  19. "double": "car | cars",
  20. "three": "no apples | one apple | {count} apples",
  21. "format": {
  22. "object": "type-{name}",
  23. "array": "type-{0}"
  24. }
  25. }
  26. }
  27. }
  28. }

A page can reference pure-text-content using a path similar to message.pageA.text.

Referencing Resources on a Page

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:

Collapse
Word wrap
Dark theme
Copy code
  1. <template>
  2. <div>
  3. <text>{{ $t('message.pageA.text') }}</text>
  4. <text>{{ $t('message.pageA.format.object', { name: 'arg-object' }) }}</text>
  5. <text>{{ $t('message.pageA.format.array', ['arg-array']) }}</text>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. onInit () {
  11. // Simple formatting:
  12. this.$t('message.pageA.text')
  13. this.$t('message.pageA.format.object', { name: 'arg-object' })
  14. this.$t('message.pageA.format.array', ['arg-array'])
  15. }
  16. }
  17. </script>

Method for Simple Formatting

Expand

Attribute

Type

Parameters

Description

$t

Function

  • path: resource path, string
  • arg0: object
  • array: formatting parameter, optional

Set the parameters based on the system language, for example, this.$t('message.pageA.text').

Sample code

Collapse
Word wrap
Dark theme
Copy code
  1. // Sample 1: Formatting without additional parameters
  2. // Output: "pure-text-content"
  3. this.$t('message.pageA.text')
  4. // Sample 2: The additional parameter is an object, which is used to replace the referenced content.
  5. // Output: "type-arg-object"
  6. this.$t('message.pageA.format.object', { name: 'arg-object' })
  7. // Sample 3: The additional parameter is an array, which is used to replace the referenced content.
  8. // Output: "type-arg-array"
  9. this.$t('message.pageA.format.array', ['arg-array'])

Singular-Plural Conversion Method

Expand

Attribute

Type

Parameters

Description

$tc

Function

  • path: resource path, string
  • count: sequence number of the item, number

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

Collapse
Word wrap
Dark theme
Copy code
  1. // Sample 1: When the value of message contains two options, the passed value is not the singular form.
  2. // Output: "cars"
  3. this.$tc('message.pageA.plurals.double', 0)
  4. // Sample 2: When the value of message contains two options, the passed value is in the singular form.
  5. // Output: "car"
  6. this.$tc('message.pageA.plurals.double', 1)
  7. // Sample 3: When the value of message contains two options, the passed value is not in the singular form.
  8. // Output: "cars"
  9. this.$tc('message.pageA.plurals.double', 2)
  10. // Sample 4: When the value of message contains three or more options, the passed value is not in the singular form.
  11. // Output: "no apples"
  12. this.$tc('message.pageA.plurals.three', 0)
  13. // Sample 5: When the value of message contains three or more options, the passed value is in the singular form.
  14. // Output: "one apple"
  15. this.$tc('message.pageA.plurals.three', 1)
  16. // Sample 6: When the value of message contains three or more options, the passed value is not in the singular form.
  17. // Output: "10 apples"
  18. this.$tc('message.pageA.plurals.three', 10)

Obtaining Language Updates

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.

Collapse
Word wrap
Dark theme
Copy code
  1. import configuration from '@system.configuration'
  2. // 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.
  3. const localeObject = configuration.getLocale()
  4. // Convert the locale to the character string format, for example, zh or zh-CN.
  5. const locale = [localeObject.language, localeObject.countryOrRegion]
  6. .filter(n => !!n)
  7. .join('-')
  8. console.info(`Obtain the current locale: ${locale}`)
  9. import configuration from '@system.configuration'
  10. // Triggered by the VM lifecycle function onConfigurationChanged after locale is set successfully.
  11. configuration.setLocale({
  12. language: 'zh',
  13. countryOrRegion: 'CN'
  14. })

Callback upon Language Switchover

The onConfigurationChanged callback is executed when a user modifies system settings or configuration.setLocale is called to change the locale.

Collapse
Word wrap
Dark theme
Copy code
  1. onConfigurationChanged(data) {
  2. // Output {"type":"locale"}
  3. console.info(JSON.stringify(data))
  4. }

Internationalization Settings in the manifest.json File

Internationalize the app name and page title configured in the manifest.json file.

  • App name: Set name to the parameter (for example, ${message.appName}) that defines the app name in the JSON file.
  • Page title: Set titleBarText under display to the parameter (for example, ${message.pageA.pageTitle}) that defines the page title in the JSON file.

Links

Search in Guides
Enter a keyword.