gpt4 book ai didi

angular - ionic2 将 ngx-translate 应用于菜单项

转载 作者:行者123 更新时间:2023-12-02 15:08:21 24 4
gpt4 key购买 nike

我正在使用 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 { }

最佳答案

首先,您应该在 Assets 文件夹中为不同的语言创建 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 { }

在你的 View 中(*.html),你需要使用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>

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

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

希望这能有所帮助;)

关于angular - ionic2 将 ngx-translate 应用于菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45706135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com