我想在对象中使用翻译来在Ionic 2上使用toast。但我不明白如何在.ts文件中使用ng2-translate ... Somenone能给我一个例子吗?
我想在菜单中的标题中翻译。我尝试这样但它不起作用:
constructor(public platform: Platform, public menu: MenuController, public translate : TranslateService) {
this.initializeApp();
translate.setDefaultLang('fr');
translate.use('fr');
let testTrad : string = 'Accueil';
translate.get('TEST').subscribe(res => {testTrad = res; console.log(testTrad)});
console.log(testTrad);
// Remplissage du tableau des pages.
this.pages= [
{title: testTrad , component: TabsPage},
{title: 'Mon compte', component: MyAccountPage},
{title: 'Changer de mot de passe' , component: ChangePasswordPage},
{title: 'Documents' , component: DocumentsPage}
]
必须在我的菜单上显示“test”,但它会继续显示“Accueil”。
答案 0 :(得分:2)
没错,
translate.get('TEST').subscribe(res => {testTrad = res; console.log(testTrad)});
console.log(testTrad);
// Remplissage du tableau des pages.
this.pages= [
{title: testTrad , component: TabsPage},
{title: 'Mon compte', component: MyAccountPage},
{title: 'Changer de mot de passe' , component: ChangePasswordPage},
{title: 'Documents' , component: DocumentsPage}
]
.subscribe
是异步的,因此 subscribe
中指定的值
尝试
translate.get('TEST').subscribe(res => {
testTrad = res;
console.log(testTrad);
this.pages= [
{title: testTrad , component: TabsPage},
{title: 'Mon compte', component: MyAccountPage},
{title: 'Changer de mot de passe' , component: ChangePasswordPage},
{title: 'Documents' , component: DocumentsPage}
]
});
console.log(testTrad);
或者,如果您不想等待完成异步,您可以尝试
this.pages= [
{title: 'Mon compte', component: MyAccountPage},
{title: 'Changer de mot de passe' , component: ChangePasswordPage},
{title: 'Documents' , component: DocumentsPage}
]
translate.get('TEST').subscribe(res => {
testTrad = res;
this.pages.push({title: testTrad, component: TabsPage});
});