【问题标题】:$localize with conditional statement in Angular$localize 在 Angular 中使用条件语句
【发布时间】:2021-06-16 17:24:18
【问题描述】:

我在我的 Angular 应用程序中使用 i18n 进行内部化。

对于 HTML,我使用 i18n="@@<unique_Id>",对于在 TS 类文件中定义的动态元素,我使用 $localize

Example: this.buttontext = $localize`:@@Security.Ok:Click to Continue`;

我想知道如何将它用于如下所示的条件语句:

this.questionLabel = this.screenType == 'reset' ? 'Question' : 'Question 1';

还有如下所示的动态字符串:

this.dialogTitle = `Select Rollback point - ${this.data.number}`

【问题讨论】:

  • 这是 Angular 还是 AngularJS 的问题?
  • @Bargros 是 Angular

标签: angular internationalization i18next


【解决方案1】:

从代码中使用本地化 Angular(从版本 11 开始)支持以下形式的标记模板

$localize`Text to translate`

由于 $localize 是一个公正的函数调用,所以在有条件的情况下你可以这样使用它:

this.questionLabel = this.screenType == 'reset' ? $localize'Question' : $localize'Question 1';

对于动态字符串,您使用相同的方法

this.dialogTitle = $localize `Select Rollback point - ${this.data.number}`

本地化提取后,将为您的变量 ${this.data.number} 生成 PH 占位符,以便您可以在翻译中使用它

您可以阅读更多信息表格official blogpost

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2010-12-05
    • 2011-03-24
    • 2014-10-23
    • 2020-11-06
    相关资源
    最近更新 更多