简体   繁体   English

Angular i18n 动态文本

[英]Angular i18n Dynamic text

I am trying to figure out if it is possible to change language for a dynamic content using i18n and related libraries like (ngx-translate) for which I am doing a POC.我试图弄清楚是否可以使用 i18n 和相关库(例如(ngx-translate))更改动态内容的语言,我正在为其做 POC。

I am able to translate the static content but I am not able to figure out how to do the same for dynamic content, like, content in ngfor loop or data coming from the server.我能够翻译静态内容,但我无法弄清楚如何对动态内容执行相同的操作,例如 ngfor 循环中的内容或来自服务器的数据。

Is there any way to do the conversion for content coming from the backend/db?有没有办法对来自后端/数据库的内容进行转换?

or any other advice/suggestions would be really appreciated.或任何其他建议/建议将不胜感激。

This is fairly simple, provided you know the values your backend may deliver:这相当简单,只要您知道后端可能提供的值:

// backend data returns known possible values:
const values = ['red', 'blue', 'green']

// de.json
color: {
  "red": "Rot",
  "blue": "Blau",
  "green": "Grün"
}

// app.component.html

<div *ngFor="let value of values">
{{ ('color.' + value) | translate }}
</div>
    

Any content that matches a key in your translation files would do.任何与翻译文件中的键匹配的内容都可以。 So if your backend gives you such keys, you can just use translate:所以如果你的后端给你这样的键,你可以使用翻译:

 <div *ngFor="let key of keys"> <p translate>{{key}}</p> </div>

Or something like that.或类似的东西。 If this is what you meant?如果这就是你的意思?

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

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