在服务,组件和不带模板的情况下进行Angular 7 i18n转换 [英] Angular 7 i18n translation inside service, component and without template
问题描述
我正在尝试从ngx-translate迁移到Angular i18n方法,并希望在迁移之前确保几点.
I am trying to migrate from ngx-translate to Angular i18n approach and wanted to make sure a few points before migration.
- 是否可以在没有任何模板的情况下在服务和组件内部进行翻译?在ngx-translate中,可以使用翻译管道.
- 是否有针对V7引入了angular的方法,或计划在v8中引入以翻译内部组件和服务水平的方法?
- 目前只能使用解决方法吗?没有角度的方法可以做到吗?如果是,我应该使用有角度的i18n方法还是更好地继续使用
ng-tranlate
软件包?
- 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?
提前谢谢!
推荐答案
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.
对于那些正在寻找可能的变通办法以在Angular 7-组件中进行翻译的人:我个人使用以下变通办法,您也可以在 gitlab问题:
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>
使用
@ViewChild('trans-foo') transFoo : ElementRef;
然后获取翻译后的值
transFoo.nativeElement.textContent
这篇关于在服务,组件和不带模板的情况下进行Angular 7 i18n转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!