HarmonyOS 如何让多语言支持占位?

比如多语言内容如下:

{
  "name":"just_pay",
  "value":"Are you sure to pay {price} score?"
}

调用的时候如下:

$r('app.string.just_pay')

期望的效果:

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

实际显示:

Are you sure to pay 100 score?

HarmonyOS是否支持?

阅读 581
1 个回答

参考文档: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?"
}
// 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)
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进