HarmonyOS 如何让多语言支持占位

比如多语言内容如下:

{
  "name":"just_pay",
  "value":"Are you sure to pay {price} score?"
}
  • 1.
  • 2.
  • 3.
  • 4.

调用的时候如下:

$r('app.string.just_pay')
  • 1.

期望的效果:

$r('app.string.just_pay', 100)
  • 1.

实际显示:

Are you sure to pay 100 score?
  • 1.

HarmonyOS是否支持?

HarmonyOS
2025-01-10 08:23:08
755浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-resource-manager-V5

// string.json
{
  "name": "just_pay",
  "value": "%s you sure to pay %d %s?"
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
// page.ets
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';

@Entry()
@Component
struct Login {
  private context = getContext(this) as common.UIAbilityContext;

  build() {
    Column({ space: 20 }) {
      Button('change', { type: ButtonType.Capsule, stateEffect: true })
        .onClick(() => {
          try {
            this.context.resourceManager.getStringSync($r('app.string.just_pay'), 'Are', 100, 'score')
            console.log('resStr',
              this.context.resourceManager.getStringSync($r('app.string.just_pay'), 'Are', 100, 'score'))
          } catch (error) {
            let code = (error as BusinessError).code;
            let message = (error as BusinessError).message;
            console.error(`getStringSync failed, error code: ${code}, message: ${message}.`);
          }
        })
        .width(120)
        .height(40)
        .fontSize(20)
    }
    .alignItems(HorizontalAlign.Center)
    .height('100%')
    .width('100%')
    .padding(80)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
分享
微博
QQ
微信
回复
2025-01-10 09:56:38


相关问题
HarmonyOS 多语言支持繁体中文吗
722浏览 • 1回复 待解决
HarmonyOS Web页面如何支持多语言
830浏览 • 1回复 待解决
HarmonyOS 多语言修改失效
939浏览 • 1回复 待解决
求助ETS如何多语言切换?
4269浏览 • 1回复 待解决
怎么实现多语言环境适配?
302浏览 • 0回复 待解决
HarmonyOS 多语言国际化咨询
689浏览 • 1回复 待解决
HarmonyOS 多语言字符串转换
924浏览 • 1回复 待解决