Fernando Del Olmo
Fernando Del Olmo

Reputation: 1450

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] 
    })
  ],
  ...

Upvotes: 0

Views: 1472

Answers (2)

Philipp
Philipp

Reputation: 29

here you can find a repo of Ionic2 rc3 using TranslationPipe and TranslationService. https://github.com/philipphalder/ionic2-rc3-NG2-Translate

Upvotes: 0

ranakrunal9
ranakrunal9

Reputation: 13558

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>

Upvotes: 1

Related Questions