2016-04-26 117 views
6

我更改了離子2中後退按鈕的名稱,但有人知道如何用ng2-translate翻譯此按鈕嗎?翻譯按鈕離子2

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

回答

9

可以轉換這樣後面的文本(假設你已經成功實施了NG2-轉換模塊)在app.ts:

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

這裏有必要設置這樣的準備功能,因爲本地化加載異步,這是你知道本地化文件已被加載和模塊已準備好工作的地方。

很顯然,我已經設置下ui.general.back適當的JSON-文件翻譯文本;)

如果您還沒有訪問的配置,那麼你需要將其導入:

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

大,這個工程。我還必須在構造函數中添加'private config:Config' – Guus

+0

也適用於翻譯組件文本(如動作表) –

+2

要保留android的僅箭頭樣式,請在this.config.set中添加'ios'作爲第一個參數: 'this .config.set('ios','backButtonText',this.translate.get('ui.general.back')['value']);' –

2

您可以翻譯後的文字是這樣,穿上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); } 
    ); 
    } 
}); } 
+0

這適用於我。做得很好 :) –

1

我不得不使用它像這樣(在app.component.ts)

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

1

在你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} 
]})