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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Why is forkjoin not a good fit here? And any reference or doc for above usage, please?
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.
Yep, Good catch. Hope you'll put this ref in your answer too. +1 github.com/ngx-translate/core#methods
I've deleted my answer since this is the best way to solve the OP. Thanks for sharing @David. +1
@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 ?
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} ]; }) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.