use ngx-translate in .ts file

此生再无相见时 提交于 2019-12-12 08:07:06

问题


I want to use a translation in sidemenu titles, I read this tutorial and I solve it as:

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

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

It works, but the problem is that I want t get many title from the translation file to set them as sidemenu titles.


回答1:


Please do not use the forkJoin operator in this case. ngx-translate supports fetching multiple translations at once by passing an array of keys to the get() method like this:

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}
  ];
})

Edit:

Here you can find all supported methods and their signature.




回答2:


@David thank you for your inspiration.

also you can use something like this:

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}
  ];
})


来源:https://stackoverflow.com/questions/46173238/use-ngx-translate-in-ts-file

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