ionic2 对菜单项应用 ngx-translate [英] ionic2 apply ngx-translate for menu items

查看:15
本文介绍了ionic2 对菜单项应用 ngx-translate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ngx-translate 来支持多语言,它运行良好.但我也想申请菜单项.我如何做到这一点.

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.

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

I have 3 menu items, i want to change the language for every title.

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

推荐答案

首先你应该在 assets 文件夹中创建不同语言的 json 文件,例如 en.json, fr.json, kh.json.

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

并将PageObj的标题更改如下:

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

并更正您的 app.module.ts:

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

在您看来 (*.html),您需要使用 translate pipe:

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');

您可以通过以下网址阅读 Angular 2+ 的国际化 (i18n) 库:http://www.ngx-translate.com

希望这会有所帮助;)

这篇关于ionic2 对菜单项应用 ngx-translate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆