Search code examples
angularionic2ng2-translate

ng2-translate on component file


im using ng2-translate on my ionic2 app and i have it working nicely on static text on html like:

<ion-title>{{ 'Inventory.view-header' | translate }}</ion-title>

I get this title translated but when i try to translate this:

<ion-item menuClose *ngFor="let p of pages" (click)="openPage(p)">
  <ion-icon color="gray" name="{{p.icon}}" item-left></ion-icon>
  {{p.title}}
</ion-item>

Im not able to translate p.title with the translate pipe as the ion-title. Im only able to do make that with:

this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
  this.pages[0].title = event.translations.Menu.pantry;
  this.pages[1].title = event.translations.Menu.inventory;
...

But i dont want to have a suscribe for every ngfor or array that i want to translate. There is another way to make this work?

My conf on app.module:

import { TranslateStaticLoader, TranslateModule, TranslateLoader} from 'ng2-translate';
export function createTranslateLoader(http: Http) {
    return new TranslateStaticLoader(http, './assets/i18n', '.json');
}

@NgModule({
  declarations: [
    MyApp,
    ...
  ],
  imports: [
    IonicModule.forRoot(MyApp),
    TranslateModule.forRoot({
      provide: TranslateLoader,
      useFactory: (createTranslateLoader),
      deps: [Http] 
    })
  ],
  ...

Solution

  • As you have said that your p.title values are like pantry, inventory then you have to use translate pipe as below :

    <ion-item menuClose *ngFor="let p of pages" (click)="openPage(p)">
      <ion-icon color="gray" name="{{p.icon}}" item-left></ion-icon>
      {{ 'Menu.' + p.title | translate }}
    </ion-item>