【问题标题】:use ngx-translate in .ts file在 .ts 文件中使用 ngx-translate
【发布时间】:2018-02-20 17:32:02
【问题描述】:

我想在 sidemenu 标题中使用翻译,我阅读了this tutorial 并将其解决为:

translate.get('HOME').subscribe(res => {

this.pages= [
 {title: res ,                  component: HomePage},
 {title: 'My Account',               component: MyAccountPage},
 {title: 'Changer Pass' , component: ChangePasswordPage}
]

它可以工作,但问题是我不想从翻译文件中获得很多标题来将它们设置为侧边菜单标题。

【问题讨论】:

    标签: angular typescript ionic2 ionic3


    【解决方案1】:

    @David感谢您的启发。

    你也可以使用这样的东西:

    translate.get(['Home', 'My Account', 'Change Password']).subscribe(translations => {
      this.pages= [
        { title: translations['Home'], component: HomePage},
        { title: translations['My Account'], component: MyAccountPage},
        { title: translations['Change Password'], component: ChangePasswordPage}
      ];
    })
    

    【讨论】:

      【解决方案2】:

      在这种情况下请不要使用forkJoin 运算符。 ngx-translate 支持通过将一组键传递给 get() 方法一次获取多个翻译,如下所示:

      translate.get(['HOME', 'MY_ACCOUNT', 'CHANGE_PASSWORD']).subscribe(translations => {
        this.pages= [
          { title: translations.HOME, component: HomePage},
          { title: translations.MY_ACCOUNT, component: MyAccountPage},
          { title: translations.CHANGE_PASSWORD, component: ChangePasswordPage}
        ];
      })
      

      编辑

      Here你可以找到所有支持的方法及其签名。

      【讨论】:

      • 为什么forkjoin 不适合这里?请问有上述用法的参考资料或文档吗?
      • 我并不是说这是一个“坏”的解决方案,它也同样有效。但我只是认为如果该功能直接内置到 ngx-translate 中,那么导入另一个 rxjs 运算符是不值得的。
      • 是的,很好。希望您也将此参考文献放入您的答案中。 +1 github.com/ngx-translate/core#methods
      • 我已经删除了我的答案,因为这是解决 OP 的最佳方法。感谢分享@大卫。 +1
      • @David:嘿,实际上在我的场景中,语言会即时更改,所以如果我使用 get 方法,它不会反映更新后的语言,所以我需要使用流方法好吗?
      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 2018-09-30
      • 2020-03-10
      • 2018-07-02
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多