我正在使用' fa'和' en'我的应用程序的语言。有两个按钮可以改变点击方向和语言: setDir(code)功能。单击按钮后,应用程序将重新加载。语言和方向更改但ngx-translation无法正常工作。它总是显示fa.json文件的值。即使我设置了translate.setDefaultLang(' en')。 的 app.component.ts :
import { Component } from '@angular/core';
import { Platform, ModalController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { Storage } from '@ionic/storage';
import { TranslateService } from '@ngx-translate/core';
export class MyApp {
constructor(public platform: Platform, private storage: Storage,
translate: TranslateService, private modalController: ModalController,
statusBar: StatusBar) {
platform.ready().then(() => {
statusBar.styleDefault();
this.splash = modalController.create(SplashPage);
this.splash.present();
});
translate.setDefaultLang('en');
storage.get('languageCode').then((lang) => {
platform.setLang(lang, true);
translate.use(lang);
});
this.storage.get('myDir').then((dir) => {
platform.setDir(dir, true);
});
}
async setDir(code) {
if (code === "fa") {
await this.storage.set('myDir', 'rtl');
await this.storage.set('languageCode', 'fa');
window.location.reload();
} else {
await this.storage.set('myDir', 'ltr');
await this.storage.set('languageCode', 'en');
window.location.reload();
}
}
}
app.html
<ion-item text-end color="none" menuClose="" id="lis-sell" class="mymenu">
{{'simple_sale' | translate}}
</ion-item>
fa.json
{
"simple_sale": "فروش"
}
en.json
{
"simple_sale": "sale"
}
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
...
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, 'assets/i18n/', '.json');
}
@NgModule({
...
imports: [
BrowserModule,
HttpClientModule,
IonicModule.forRoot(MyApp),
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient]
}
}),
...
答案 0 :(得分:0)
哇。我觉得很蠢。一切正常。它只是我定义了两个单独的菜单,我使用ngx-translate作为菜单中的值。一个菜单在rtl工作。当app将方向更改为ltr时,rtl菜单将禁用,并且ltr将变为启用。所以问题完全来自于我的愚蠢,我忘记了我有两个单独的菜单,我只是为一个设置翻译价值。