简体   繁体   English

角度:使用 ngx-translate 在模板中翻译对象数组的元素

[英]angular: Translate elements of an object array in a template with ngx-translate

I have been searching for hours how I can translate the values of an object array with the module ngx-translate, Here is an extract of my code but I don't know how to implement the json file我一直在搜索如何使用模块 ngx-translate 翻译对象数组的值,这是我的代码的摘录,但我不知道如何实现 json 文件

Template HTML :模板 HTML :

 <div class="sort-header-container">
    <table matSort class="mat-sort">
      <tr *ngFor="let item of items" class="row">
        <td class="row">{{item.critere}}</td>
        <td>{{item.res}}</td>
      </tr>
    </table>
 </div>

The object array in the service :服务中的对象数组:

items: any[] = [
    { critere: "Code-modèle", res: "Mizuno Shadow 4" },
    { critere: "Code Libellé", res: "KR32" },
    { critere: "Stock", res: 10 },
    { critere: "Prix TTC", res: "Bleu" },
    { critere: "Couleur", res: 42 },
    { critere: "Matière", res: 125 },
    { critere: "Zone", res: 100 },
  ];

I need to translate only the critere column我只需要翻译标准列

Thank you谢谢

You need to use the TranslatePipe from ngx-translate .您需要使用ngx-translateTranslatePipe

<div class="sort-header-container">
    <table matSort class="mat-sort">
      <tr *ngFor="let item of items" class="row">
        <td class="row">{{item.critere | translate}}</td>
        <td>{{item.res}}</td>
      </tr>
    </table>
 </div>

Make sure your critere field have the corresponding translations in the json translation files确保您的critere字段在 json 翻译文件中具有相应的翻译

example for en english translation:英文翻译示例:

{
  "Prix TTC": "Price",
  "Couleur": "Color",
}

As taken from the example in ngx-translate github摘自 ngx-translate github 中的示例

You can use the translate pipe in your code, like this:您可以在代码中使用翻译管道,如下所示:

 <td class="row">{{ item.critere | translate }}</td>

For this to work, your critere would need to be keys in your language file, for example:为此,您的critere需要是语言文件中的键,例如:

{
  'Stock': 'Stock english translation',
  'Couleur': 'Is this color?'
}

Thank you very much for your answers Finally here's what I did :非常感谢您的回答最后这是我所做的:

    <tr *ngFor="let item_translate of ('PRODUCT.PRODUCT' | translate); let i = index " class="row"
        [ngStyle]="item_translate.critere == 'Stock' && stock<0 ?{color:'red'}:{color:'#474646'}">
        <td class="row">{{item_translate.critere}}</td>
        <td>{{items[i].res}}</td>
      </tr>

In my json example fr :在我的 json 示例 fr 中:

"PRODUCT": {
    "PRODUCT": [
      {
        "critere": "Code-modèle"
      },
      {
        "critere": "Code Libellé"
      },
      {
        "critere": "Stock"
      },
      {
        "critere": "Prix TTC"
      },
      {
        "critere": "Couleur"
      },
      {
        "critere": "Matière"
      },
      {
        "critere": "Zone"
      }
    ]
  }

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

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