ionic2对菜单项应用ngx-translate

时间:2017-08-16 06:04:28

标签: angular typescript ionic2 ionic3 ngx-translate

我正在使用ngx-translate进行多语言支持,它运行正常。但我也想申请菜单项目。我如何实现这一目标。

我有3个菜单项,我想更改每个标题的语言。

ts文件

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>

和我的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 { }

2 个答案:

答案 0 :(得分:2)

首先,您应该在资源文件夹中为差异语言创建json文件,例如 en.json,fr.json,kh.json

enter image description here

<强> en.json:

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

并更改PageObj的标题如下:

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

并更正你的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 { }

在您的视图中( *。html ),您需要使用翻译管道

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

如果您想设置默认语言或使用语言:

// 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

希望这可以提供帮助;)

答案 1 :(得分:1)

您可以使用每个侧边菜单项中的title属性作为翻译键,如下所示:

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

.ts文件中:

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

然后在视图中,只需将translate管道添加到每个菜单项:

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