最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular i18n Dynamic text - Stack Overflow

programmeradmin2浏览0评论

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.

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 ing from the server.

Is there any way to do the conversion for content ing from the backend/db?

or any other advice/suggestions would be really appreciated.

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.

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 ing from the server.

Is there any way to do the conversion for content ing from the backend/db?

or any other advice/suggestions would be really appreciated.

Share Improve this question asked Aug 25, 2020 at 9:20 NavdeepNavdeep 391 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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?

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.ponent.html

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

评论列表(0)

  1. 暂无评论