ionic2 apply ngx-translate for menu items

2019-07-23 15:41发布

问题:

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 { }

回答1:

First you should create json file for difference languages in assets folder such as en.json, fr.json, kh.json.

en.json:

{
  // Your content...,
  "PROFILE": "Profile",
  "MY_ACCOUNT": "My Account",
  "FAQ": "FAQ"
}

And change title of PageObj as below:

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

And correct your app.module.ts:

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import { Http, HttpModule } from '@angular/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: Http) {
    return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
    imports: [
        BrowserModule,
        HttpModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: HttpLoaderFactory,
                deps: [Http]
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

In your view (*.html), you need to use translate pipe:

<button ion-item menuClose *ngFor="let p of appPages" (click)="openPage(p)">
    <ion-icon item-left [name]="p.icon"></ion-icon>
     {{ p.title | translate }}
 </button>

if you want to set default or use language:

// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('en');

// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use('en');

You can read The internationalization (i18n) library for Angular 2+ with this url : http://www.ngx-translate.com

Hope this could help ;)



回答2:

You could use the title property from each side menu item, to be the translation key, like this:

// Language file
// en.json
{
  // Your content...,
  "PROFILE": "Profile",
  "MY_ACCOUNT": "My Account",
  "FAQ": "FAQ"
}

And in your .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' }    
  ];

Then in the view, just add the translate pipe to each menu item:

<button ion-item menuClose *ngFor="let p of appPages" (click)="openPage(p)">
    <ion-icon item-left [name]="p.icon"></ion-icon>
     {{ p.title | translate }}
 </button>