DEV Community

HarmonyOS
HarmonyOS

Posted on

How to use interpolation in internationalization

Read the original article:How to use interpolation in internationalization

Context

While using internalization, users need to customize messages.

Description

Internalization is used to have multilingual applications. During this process, developers may need to customize messages for app users to provide a better experience. For example, instead of saying "Welcome, dear user." they want to say "Welcome, Mr X.".

Given the message in string.json, how can we customize it for users?

{
    "name": "welcome",
    "value": "Welcome, dear user."
},
Enter fullscreen mode Exit fullscreen mode

Solution / Approach

The resource configuration file string.json supports the configuration of string and number placeholders such as %s and %d. You can define variables in the corresponding page and use Resources and variable splicing in the actual component to achieve the desired effect.

{
    "name": "welcome",
    "value": "Welcome %s."
},
Enter fullscreen mode Exit fullscreen mode

And use it like this:

Text($r('app.string.welcome', 'Mr X'))
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Internalization is used to have multilingual applications.
  • string.json supports the configuration of string and number placeholders such as %s and %d.

Written by Mehmet Karaaslan

Top comments (0)