Surya Bhargava
Surya Bhargava

Reputation: 51

Angular 7 i18n translation inside service, component and without template

I am trying to migrate from ngx-translate to Angular i18n approach and wanted to make sure a few points before migration.

  1. Is it possible to translate inside the service and component without any template? In ngx-translate it was possible using translate pipe.
  2. Is there any approach which angular has introduced for V7 or planning to introduce in v8 for translating inside component and service level?
  3. Is this currently only possible using workaround and there is no angular way to do it? If yes, shall i go with angular i18n approach OR better to continue with ng-tranlate package?

Thanks in advance!

Upvotes: 5

Views: 1878

Answers (1)

sevic
sevic

Reputation: 972

You can find the corresponding gitlab issue here. There is still no milestone set to it, so I guess it will take some more time for this feature to be implemented.

For those who are looking for a possible workaround to get translations inside Angular 7- components: I personally use the following workaround, which you also can find together with some other proposals in the gitlab issue:

Create hidden elements in the template

<span style="display: none;" #trans-foo i18n>foo</span>

Bind them using

@ViewChild('trans-foo') transFoo : ElementRef;

Then retrieve the translated value

transFoo.nativeElement.textContent

Upvotes: 2

Related Questions