Angular 7 i18n translation inside service, component and without template

纵然是瞬间 提交于 2020-01-13 17:13:13

问题


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!


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!