问题
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