【问题标题】:angular 5 i18n - Able to use translation strings outside a templateangular 5 i18n - 能够在模板外使用翻译字符串
【发布时间】:2018-12-03 23:32:01
【问题描述】:

我目前正在使用 Angular 5.x 并使用指令“i18n”进行翻译。我对 .html 文件或模板没有任何问题,但在打字稿文件中找不到执行此操作的方法。 无论如何,打字稿文件中有翻译字符串吗?

注意:Angular 内置 i18n 似乎不支持仅在模板中翻译 typescript 中的字符串。

【问题讨论】:

标签: angular typescript angular-i18n


【解决方案1】:

不,这是不可能的。常春藤可能有可能。看到这个Github issue

我已经通过创建翻译组件解决了这个问题。

HTML:

<div style="display:none">
  <div id="translation_1" i18n>Text to be translated</div>
</div>

TS(可以是可注入服务):

constructor(@Inject(DOCUMENT) private document)) {}

public getTranslation(id: string) {
    return this.document.getElementById(id);
}

例如在您的演示组件中,您可以执行以下操作: translationService.getTranslation("translation_1");

这并不理想,但效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多