问题
I am trying to migrate from ngx-translate to Angular i18n approach and wanted to make sure a few points before migration.
- Is it possible to translate inside the service and component without any template? In ngx-translate it was possible using translate pipe.
- Is there any approach which angular has introduced for V7 or planning to introduce in v8 for translating inside component and service level?
- 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!
回答1:
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
来源:https://stackoverflow.com/questions/54003880/angular-7-i18n-translation-inside-service-component-and-without-template