I am using ngx-translate for multi language support and it is working fine. but i want to apply for menu items also. How do i achieve this.
I have 3 menu items, i want to change the language for every title.
ts file
appPages: PageObj[] = [
{ title: 'Profile', component: ProfilePage, icon: 'person' },
{ title: 'My Account', component: MyaccountPage, index: 1, icon: 'cash' },
{ title: 'FAQ', component: FaqPage, index: 3, icon: 'chatbubbles' }
];
HTML
<button ion-item menuClose *ngFor="let p of appPages" (click)="openPage(p)">
<ion-icon item-left [name]="p.icon"></ion-icon>
{{p.title}}
</button>
And my module.ts
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {HttpClientModule, HttpClient} from '@angular/common/http';
import {TranslateModule, TranslateLoader} from '@ngx-translate/core';
import {TranslateHttpLoader} from '@ngx-translate/http-loader';
import {AppComponent} from './app';
// AoT requires an exported function for factories
export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}
@NgModule({
imports: [
BrowserModule,
HttpClientModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
bootstrap: [AppComponent]
})
export class AppModule { }
You could use the
title
property from each side menu item, to be the translation key, like this:And in your
.ts
file:Then in the view, just add the
translate
pipe to each menu item:First you should create json file for difference languages in assets folder such as en.json, fr.json, kh.json.
en.json:
And change title of PageObj as below:
And correct your app.module.ts:
In your view (*.html), you need to use translate pipe:
if you want to set default or use language:
You can read The internationalization (i18n) library for Angular 2+ with this url : http://www.ngx-translate.com
Hope this could help ;)