【HarmonyOS 5】应用实现 APP 国际化多语言切换
##鸿蒙开发能力 ##HarmonyOS SDK 应用服务 ##鸿蒙金融类应用 (金融理财 #
前言
在鸿蒙中应用国际化处理,与 Android 和 IOS 基本一致,都是通过 JSON 配置不同的语言文本内容。在 UI 展示时,使用 JSON 配置的字段 key 进行调用,系统选择对应语言文本内容。
跟随系统多语言切换
最常见的处理,只需要配置不同语言的 JSON 配置,以最常见的中英文举例:我们需要先在资源文件对应的语言 json 文本 string 配置表中,进行 key,val 的配置:
zh_CN 为中文,en_US 为英文,base-element 中的 string.json 是默认配置。默认配置是必填,只要你创建了对应语言文件夹,没有在默认配置对应字段节点,系统就会提示报错:
如下图所示,配置好字段文本内容:
当我们配置好字段,只需要在 UI 中进行引用即可:
 $r("app.string.xxx");例如:$r("app.string.test_content");
   复制代码
 创建语言资源文件夹
1.在资源文件夹 resources 右键如下图所示,新增资源文件夹
2.在显示的弹框中选择 Locale,点击右侧按钮,显示语言和地区
3.以英语为例,在语言列表,使用英文输入法可以触发筛选搜索框,选择对应该的语言和地区,点击 OK 即可生成。
APP 独立多语言切换
当我们需要 APP 应用的语言独立于系统,不跟随系统语言设置进行改变。例如系统是中文,设置应用为英文的需求。这时我们需要设置应用优先语言来实现该效果:
语言 ID 列表见文章最下方。
 try {  i18n.System.setAppPreferredLanguage("zh"); // 设置应用偏好语言为zh-Hans} catch(error) {  let err: BusinessError = error as BusinessError;  console.error(`call System.setAppPreferredLanguage zh failed, error code: ${err.code}, message: ${err.message}.`);}
   复制代码
 
DEMO 示例:启动页测试代码
 import { i18n } from '@kit.LocalizationKit';import { BusinessError } from '@kit.BasicServicesKit';
@Entry@Componentstruct Index {  @State message: string | Resource = $r("app.string.test_content");
  build() {    Column() {      Text(this.message).fontSize(50)
      Text("切换中文").fontSize(50)      .onClick(()=>{        // 设置语言偏好        try {          i18n.System.setAppPreferredLanguage("zh"); // 设置应用偏好语言为zh-Hans        } catch(error) {          let err: BusinessError = error as BusinessError;          console.error(`call System.setAppPreferredLanguage zh failed, error code: ${err.code}, message: ${err.message}.`);        }      })
      Text("切换英文").fontSize(50)        .onClick(()=>{          // 设置语言偏好          try {            i18n.System.setAppPreferredLanguage("en");          } catch(error) {            let err: BusinessError = error as BusinessError;            console.error(`call System.setAppPreferredLanguage en                                                                                                                                                                                                                                                                                                                       failed, error code: ${err.code}, message: ${err.message}.`);          }        })    }    .height('100%')    .width('100%')  }}
   复制代码
 
英文:
 {  "string": [    {      "name": "module_desc",      "value": "module description"    },    {      "name": "EntryAbility_desc",      "value": "description"    },    {      "name": "EntryAbility_label",      "value": "label"    },    {      "name": "test_content",      "value": "English"    }  ]}
   复制代码
 
中文:
 {  "string": [    {      "name": "module_desc",      "value": "模块描述"    },    {      "name": "EntryAbility_desc",      "value": "描述"    },    {      "name": "EntryAbility_label",      "value": "标签"    },    {      "name": "test_content",      "value": "中文"    }  ]}
   复制代码
 
默认:
 {  "string": [    {      "name": "module_desc",      "value": "模块描述"    },    {      "name": "EntryAbility_desc",      "value": "描述"    },    {      "name": "EntryAbility_label",      "value": "标签"    },    {      "name": "test_content",      "value": "中文"    }  ]}
   复制代码
 
多语言国际化国家 ID 列表:
【有道云笔记】国家ID列表
评论