我正在使用ngx-translate进行多语言支持,它运行正常。但我也想申请菜单项目。我如何实现这一目标。
我有3个菜单项,我想更改每个标题的语言。
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' }
];
<button ion-item menuClose *ngFor="let p of appPages" (click)="openPage(p)">
<ion-icon item-left [name]="p.icon"></ion-icon>
{{p.title}}
</button>
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 { }
答案 0 :(得分:2)
首先,您应该在资源文件夹中为差异语言创建json文件,例如 en.json,fr.json,kh.json 。
<强> 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');
希望这可以提供帮助;)
答案 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>