Search code examples
angularangular5angular-i18n

Using angular i18n on conditional operators (ternary)


Is it possible to use i18n on strings that require conditional operation

<h2 i18n>{{ updateMode ? 'Edit' : 'Add'}}</h2>

Solution

  • <h2 i18n>{{ updateMode ? 'Edit' : 'Add'}}</h2>
    

    In your .xlf file you will get something like this

    <source>
      <x id="INTERPOLATION" equiv-text="{{updateMode  ? 'Edit': 'Add'}}"/>
    </source>
    

    Target should look like this:

    <target>
      {{updateMode  ? 'TranslatedValue1' : 'TranslatedValue2'}}
    </target>
    

    EDIT: This answer is not relevant since angular v.9.0 You can not use ternary operator anymore, instead it use:

    <ng-container *ngIf="updateMode" i18n="@@translationID1>TranslatedValue1</ng-container>
    <ng-container *ngIf="!updateMode" i18n="@@translationID2>TranslatedValue2</ng-container>