翻译不适用于离子模式

Translation not working in ionic modal

我正在使用 Ionic v2 接管一个小型应用程序的代码,并且我正在使用 ng2-translate 进行翻译。我仅在模态 window 中遇到翻译问题。它在任何地方都工作得很好,除了这个模态,我只能看到交易键。

这是我的 AppModule :

@NgModule({
  declarations: [
    MyApp,
    // ...
    SearchPersonModal
  ],
  imports: [
    IonicModule.forRoot(MyApp),
    ChartModule,
    TranslateModule.forRoot({
      provide: TranslateLoader,
      useFactory: (createTranslateLoader),
      deps: [Http]
    })
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    // ...
    SearchPersonModal,
  ],
  providers: [
    ApiCaller
  ]
})
export class AppModule {}

模态在应用程序中用于在远程数据库中搜索用户。这是模态组件的代码:

@Component({
  selector: 'page-search-person',
  templateUrl: 'search-person.html',
})
export class SearchPersonModal {

  public caller : ApiCaller = null;
  public translate : TranslateService = null;

  // ..

  constructor(
    public viewCtrl: ViewController,
    public toastr: ToastController,
    params: NavParams
  ) {
    this.translate = params.get('translate');
    this.caller = params.get('caller');
  }

  // ...
}

模态框的呈现方式如下:

let modal = this.modalCtrl.create(SearchPersonModal, {
  caller: this.caller,
  translate : this.translate
});

我认为代码的作者将服务作为参数传递是因为依赖注入不起作用。事实上,当我尝试这样做时,使用这个构造函数:

export class SearchPersonModal {

  //public caller : ApiCaller = null;
  //public translate : TranslateService = null;

  // ...

  constructor(
    public viewCtrl: ViewController,
    public toastr: ToastController,
    public caller: ApiCaller,
    public translate: TranslateService,
    params: NavParams
  ) {
    //this.translate = params.get('translate');
    //this.caller = params.get('caller');
  }

  // ...
}

翻译仍然无效,但 ApiCaller 服务按预期工作。为什么这项服务很好,但翻译器却不行?

看来这是一个currently known issue with Ionic 2 and has been reported on their repo。解决方法是翻译服务需要 re-initialised 用于问题日志中所述的模态:

Things work on pages, but do not work inside modals. It's as if the translate service is not available inside the modal. If, inside the modal, I re-initialize the translate service by running this.translate.use('fr'); then things work fine. The following code, for example, works fine.

import { TranslateService } from 'ng2-translate/ng2-translate';

@Component({
  templateUrl: 'my-modal.html',
  selector: 'my-modal',
})

export class MyModal {
constructor(
    private translate: TranslateService
  ) {
    var self = this;
    this.translate.use('fr');  // Re-initializing the translate service inside a modal
    // Here on translation works fine
  }
}

因此映射到您当前实施的此解决方法应与此类似:

import { TranslateService } from 'ng2-translate/ng2-translate';

export class SearchPersonModal {

  public caller : ApiCaller = null;
  public translate : TranslateService = null;

  // ...

  constructor(
    public viewCtrl: ViewController,
    public toastr: ToastController,
    public caller: ApiCaller,
    private translate: TranslateService
    params: NavParams
  ) {
    this.caller = params.get('caller');
    this.translate.use('en'); // Re-initialised rather than passed via navParams.
  }

  // ...
}