Angular 7 i18n 内部服务、组件和无模板的翻译 [英] Angular 7 i18n translation inside service, component and without template

查看:20
本文介绍了Angular 7 i18n 内部服务、组件和无模板的翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 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.

  1. 是否可以在没有任何模板的情况下在服务和组件内部进行翻译?在 ngx-translate 中,可以使用翻译管道.
  2. angular 有没有在 V7 中引入或计划在 v8 中引入的任何方法来转换内部组件和服务级别?
  3. 目前是否只能使用变通方法,而没有角度方法可以做到这一点?如果是,我应该使用 angular i18n 方法还是更好地继续使用 ng-tranlate 包?

提前致谢!

推荐答案

可以找到对应的gitlab issue 此处.目前还没有设置里程碑,所以我想这个功能需要更多的时间来实现.

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:

在模板中创建隐藏元素

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

使用

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

然后检索翻译后的值

transFoo.nativeElement.textContent

这篇关于Angular 7 i18n 内部服务、组件和无模板的翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆