5

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!

Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39

1 Answers1

2

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
sevic
  • 879
  • 11
  • 36
  • That's a great way to do it. Actually prefer this than having the texts inside the component. It's more declarative and you can still build language specific apps at build time. – Spock Apr 18 '19 at 09:08