简体   繁体   English

使用 ngx-translate (Angular) 将单词翻译成 class 组件 (.ts)

[英]Translate words into the class component (.ts) with ngx-translate (Angular)

I want to translate the content of a table into my typescript class.我想将表格的内容翻译成我的 typescript class。 The table's data are fetched from JSON file located into /assets.该表的数据是从位于 /assets 中的 JSON 文件中获取的。

Is there a way to achieve that?有没有办法做到这一点? How I can mark translation into a typescript class?如何将翻译标记为 typescript class? PS: Please do not suggest doing it into the HTML, since it is not the desired way. PS:请不要建议将其放入 HTML,因为这不是所需的方式。

As mentioned in the ngx-translate docs , you can achieve that by injecting the TranslateService into your class constructor, then use one of the following methods:ngx-translate 文档中所述,您可以通过将TranslateService注入 class 构造函数来实现,然后使用以下方法之一:

constructor(private translate: TranslateService) {}

ngOnInit(): void {
  // Using `instant` function:
  const translatedValueUsingInstant = this.translate.instant(
    'KEY_STORED_IN_TRANSLATION_FILE'
  );

  // OR using `get` function:
  let translatedValueUsingGet: string;
  this.translate
    .get('KEY_STORED_IN_TRANSLATION_FILE')
    .subscribe((value) => (translatedValueUsingGet = value));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM