6

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.

sebaferreras
  • 44,206
  • 11
  • 116
  • 134
Al-shimaa Adel
  • 767
  • 1
  • 10
  • 26

2 Answers2

9

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.

David
  • 7,387
  • 3
  • 22
  • 39
  • Why is `forkjoin` not a good fit here? And any reference or doc for above usage, please? – Sampath Sep 12 '17 at 14:54
  • 1
    I don't say it is a "bad" solution, works just as well. But I just don't think its worth importing another rxjs operator if the feature is built right into ngx-translate. – David Sep 12 '17 at 14:56
  • 2
    Yep, Good catch. Hope you'll put this ref in your answer too. +1 https://github.com/ngx-translate/core#methods – Sampath Sep 12 '17 at 15:03
  • 1
    I've deleted my answer since this is the best way to solve the OP. Thanks for sharing @David. +1 – sebaferreras Sep 12 '17 at 16:14
  • @David: hey actually in my scenario language changes on the fly so if I will use the get method it will not reflect the updated language so for that I need to use stream method is it good ? – Dhaval Patel Jan 25 '19 at 07:57
0

@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}
  ];
})
Amr Abdalrahman Ahmed
  • 920
  • 1
  • 14
  • 19