鸿蒙开发中如何设置app的语言?
使用 i18n.System.setAppPreferredLanguage(language)
其中 language 可选值为:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-i18n-V5#getsystemlanguages9
具体代码如下:
import { TitleBar } from '@devwiki/common_ui';
import { i18n } from '@kit.LocalizationKit';
@Entry
@Component
struct SetLanguagePage {
private readonly languageGroup = "languageGroup"
@State selectedLanguage: string = 'zh-Hans';
build() {
Column() {
TitleBar({
title: $r('app.string.set_language_title')
});
Row() {
Text('简体中文');
Radio({ group: this.languageGroup, value: 'zh-Hans' }).checked(this.selectedLanguage == 'zh-Hans')
.onChange(isChecked => {
if (isChecked) {
i18n.System.setAppPreferredLanguage('zh-Hans')
this.selectedLanguage = 'zh-Hans'
}
});
}
Row() {
Text('英文');
Radio({ group: this.languageGroup, value: 'en-US' }).checked(this.selectedLanguage == 'en-US')
.onChange(isChecked => {
if (isChecked) {
i18n.System.setAppPreferredLanguage('en-US')
this.selectedLanguage = 'en-US'
}
});
}
}.width('100%').height('100%')
}
}
完整项目参见:
Harmony/HMDemo - HMDemo - DevWiki Gitea
https://git.devwiki.net/Harmony/HMDemo