0

I've been looking into how to use i18n for translations in an Angular app. My struggle is with string interpolation, it seems this has not been implemented into Angular yet.

Goal: Use i18n to translate string interpolation of collection in an Angular app to have a Spanish version.

What I've done: I've been doing a lot of research, and the workarounds I've seen mentioned are:

  1. Polyfill: ( https://github.com/ngx-translate/i18n-polyfill ) (and referenced here: angular-i18n Angular 6 Internationalisation : How to deal with variables )

  2. Hidden div: (Angular internationalization (i18n) and *ngFor )

  3. Verbose method: using n-container and ngswitch. Seems the least clean way: https://github.com/angular/angular/issues/11405#issuecomment-415731284

For a collection like an array, or a database table, how are these workarounds creating separate i18n tags for each element? Will it end up showing just one tag for all elements of the collection?

For example: If you have an array for "Gender", with values of "Male" and "Female", what will happen?

<mat-option *ngFor="let gender of genders" [value]="gender.value">
             {{ gender.viewVal }}
</mat-option>

In the polyfill they mention:

test {{myVar}} !", {myVar: "^_^"})

Does that mean "^_^" is the translation? It's done directly in the typescript, not in an xlf file? If you use a hidden div, how does using the typescript file affect the xlf? The verbose method seems easiest to understand but not good for scalable, what if you have a dropdown with 10+ items, then that really bulks up the code.

What is the best way to go about this goal? I'm not sure I properly understand how the polyfill works and there is not much documentation on these ways of translation.

seesharp
  • 101
  • 1
  • 14
  • Are you bound to the demand using i18n-translation with angular build-in tools? Or are you free to decide which 3rd-party tool to apply? –  Jan 23 '19 at 18:46
  • We have put some structure around the i18n and have translated a all of the site that is not variables, but I am open to hearing different options, what were you thinking? – seesharp Jan 24 '19 at 15:22

0 Answers0