【问题标题】:translate back button ionic 2翻译后退按钮 ionic 2
【发布时间】:2016-08-20 01:54:13
【问题描述】:

我在 ionic 2 中更改了后退按钮的名称,但有人知道如何使用 ng2-translate 翻译此按钮吗?

this.config.set('backButtonText', 'Go Back'); // < want to translate this with ng2-translate.

【问题讨论】:

    标签: typescript angular ionic2


    【解决方案1】:

    你可以这样翻译后面的文字,放到app.component.ts上

    ngAfterViewInit() {
    this.navCtrl.viewWillEnter.subscribe((view) => {
      let parentView = this.navCtrl.getPrevious(view);
    
      if (parentView) {
        this.translate.get('nav.back.label').first().subscribe(
          moduleName => { this.navCtrl.getActive().getNavbar().setBackButtonText(moduleName); }
        );
      }
    }); }
    

    【讨论】:

      【解决方案2】:

      在你的 app.module.ts 中:

      @NgModule({
      declarations: [
          MyApp
      ],
      imports: [
          BrowserModule,
          IonicModule.forRoot(MyApp, {
              platforms: {
                  ios: {
                      backButtonText: 'Voltar'
                  }
              }
          }),
      ],
      bootstrap: [IonicApp],
      entryComponents: [
          MyApp
      ],
      providers: [
          StatusBar,
          SplashScreen,
          {provide: ErrorHandler, useClass: IonicErrorHandler}
      ]})
      

      【讨论】:

        【解决方案3】:

        我不得不像这样使用它(在 app.component.ts 中)

        this.platform.ready().then(() => {
          this.translate.get('GENERIC.BACK').subscribe(backLabel => {
            this.config.set('ios', 'backButtonText', backLabel);
          });
        });

        【讨论】:

          【解决方案4】:

          你可以在你的 app.ts 中像这样翻译后面的文本(假设你已经成功实现了 ng2-translate 模块):

          initializeApp() {
              this.platform.ready().then(() => {
                  this.config.set('backButtonText', this.translate.get('ui.general.back')['value']);
              });
          }
          

          有必要在 ready-function 中设置它,因为本地化加载异步,这是您知道本地化文件已加载并且模块已准备好工作的地方。

          显然我已经在 ui.general.back 下的相应 json 文件中设置了翻译文本;)

          如果您尚未访问配置,则需要将其导入:

          import {..., Config} from 'ionic-angular';
          

          【讨论】:

          • 太好了,这行得通。我还必须添加构造函数private config: Config
          • 也适用于翻译动作表等组件文本
          • 要保持android的箭头样式,在this.config.set中添加'ios'作为第一个参数:this.config.set('ios','backButtonText', this.translate.get('ui.general.back')['value']);
          • 动态改变它怎么样?
          猜你喜欢
          • 2017-06-20
          • 1970-01-01
          • 2017-04-22
          • 1970-01-01
          • 2017-05-13
          • 1970-01-01
          • 1970-01-01
          • 2016-09-14
          • 1970-01-01
          相关资源
          最近更新 更多