【发布时间】: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
我在 ionic 2 中更改了后退按钮的名称,但有人知道如何使用 ng2-translate 翻译此按钮吗?
this.config.set('backButtonText', 'Go Back'); // < want to translate this with ng2-translate.
【问题讨论】:
标签: typescript angular ionic2
你可以这样翻译后面的文字,放到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); }
);
}
}); }
【讨论】:
在你的 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}
]})
【讨论】:
我不得不像这样使用它(在 app.component.ts 中)
this.platform.ready().then(() => {
this.translate.get('GENERIC.BACK').subscribe(backLabel => {
this.config.set('ios', 'backButtonText', backLabel);
});
});
【讨论】:
你可以在你的 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
this.config.set('ios','backButtonText', this.translate.get('ui.general.back')['value']);