使用Angular I18N内部 *ngfor循环使用Angular 5



我想在这样的模板中使用ngfor-loop内的动态翻译:

<mat-card *ngFor="let user of usersList">
    <mat-card-title>
        <span>{{user.name}}</span>
    </mat-card-title>
    <mat-card-content>
        <!--This is the enum i would like to translate dynamic -->
        <span i18n="Role@@role">{{user.role}}</span>
    </mat-card-content>
</mat-card>

当前的Angular 5 I18E(国际化(的板载工具可以动态转换此枚举价值吗?

正如我阅读的,在Angular 6.1或更高版本中,将有一种在.ts中翻译值的方法。但是,如果我现在想使用它,该怎么办?有解决方法吗?

使用替代短信不是一个好方法,因为如果您在枚举中有一百个值,那该怎么办。我会在HTML代码中重复我的枚举。

我还用.xlf

测试

现在对我有用:

<mat-card *ngFor="let user of usersList">
    <mat-card-title>
        <span>{{user.name}}</span>
    </mat-card-title>
    <mat-card-content>
       <span i18n="@@roleKey">{user.roles, select, role {role}}</span>
    </mat-card-content>
</mat-card>

我的Messeges.de.xlf中的翻译部分是

<trans-unit id="roleKey" datatype="html">
        <source>{VAR_SELECT, select, role {role}}</source>
        <target>{VAR_SELECT, select, SUPER_ADMIN {Super Admin} LIZENZ_MANAGER {Lizenz Manager} RECHTLICHE_EXPERT {Rechtliche-Expert} INFRASTRUKTRELLE_EXPERT {Infrastruktrelle-Expert} SPORTLICHE_EXPERT {Sportliche Expert} ADMINISTRATIVES_EXPERT {Administratives-Expert} FINANZIELLE_EXPERT {Finanzielle-Expert} LIZENZ_BEWERBER {Lizenz-Bewerber} }</target>
        <context-group purpose="location">
            <context context-type="sourcefile">app/locallogin/locallogin.component.ts</context>
            <context context-type="linenumber">18</context>
        </context-group>
</trans-unit>

我设法使它起作用。这是我的示例使用格式xlif2与SELECT ICU表达式

https://angular.io/guide/i18n#translate-select

这是我翻译选择

的方式

component.html

    <ul>
      <li *ngFor="let user of users">
        <span i18n="@@role">dummy {user.role, select, admin {admin}}</span>
      </li>
      </ul>

注意

  • 我需要在ICU表达式(虚拟(之前添加一些文本,否则它不起作用。
  • 将ID分配给翻译(这里是role(
  • you 不要需要在此处定义所有可能的值。例如,我只是定义了一个(admin(以具有有效的ICU表达式。所有其他可能的值将仅在翻译文件

然后提取消息文件(例如法语(

    ng xi18n --outputPath src/locale --locale fr--i18nFormat=xlf2 --outFile messages.fr.xlf

然后在消息messages.[language].xlf文件中设置翻译

    <unit id="role">
      <segment>
        <source>ROLE <ph id="0" equiv="ICU" disp="{user.role, select, admin {...} user {...} other {...}}"/></source>
        <target>ROLE <ph id="0" equiv="ICU" disp="{user.role, select, admin {...} user {...} other {...}}"/></target>
      </segment>
    </unit>
    <unit id="7222321253551421654">
      <segment>
        <source>{VAR_SELECT, select, admin {administrator} user {user} other {other} }</source>
        <target>{VAR_SELECT, select, admin {administrateur} user {utilisateur} other {autre} }</target>
      </segment>
    </unit>

单元7222321253551421654包含要翻译的实际值的ID是该工具生成的值。您只需要修改该单元即可添加任意多的角色/翻译

最新更新