简体   繁体   English

Ionic 2:在ts文件中使用ng2-translate

[英]Ionic 2 : Use ng2-translate in a ts file

I want to use a translation in an object to use the toast on Ionic 2. But I don't understand how to use ng2-translate in a .ts file... Somenone can show me an example ? 我想在对象中使用转换以在Ionic 2上使用烤面包。但是我不明白如何在.ts文件中使用ng2-translate ...有人可以举一个例子吗?

I want to have a translation in a title in my menu. 我想在菜单的标题中进行翻译。 I try like this but it doesn't work : 我尝试这样,但不起作用:

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}
]

It must be display "test" on my menu, but it continue to display "Accueil". 它必须在菜单上显示“ test”,但仍继续显示“ Accueil”。

No that's correct, 不,那是正确的,

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 is async, so the values assigned within the subscribe , are not yet accessible .subscribe是异步,所以的所分配的值subscribe ,还没有访问

Try 尝试

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);

Or, if you don't want to wait for the async the finish you could try 或者,如果您不想等待异步完成,则可以尝试

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});
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM