Search code examples
angularionic-frameworkngfortranslateside-menu

How to translate with pipe the return of a *ngFor loop in Ionic/Angular


I'm new to Ionic/Angular and therefore ask for your kind help. So far I managed to make translating and ngFor work fine in my script, but I would like to mix it in the side-menu of my app, so that it gets automatically translated.

Here is my menu inside app.html working fine : it lists all pages in app.components.ts

<ion-list>
  <button ion-item *ngFor="let p of pages" (click)="openPage(p)">
    {{p.title}}
  </button>
</ion-list>

Here is what I am trying to do : (the translate function works fine when I'm using strings such as "menu_title_1")

<ion-list>
  <button ion-item *ngFor="let p of pages" (click)="openPage(p)">
    {{ {{p.title}} | translate }}
  </button>
</ion-list>

Here is my app.components.ts :

import { Platform, MenuController, NavController, Nav } from 'ionic-angular'; 
import { HomePage } from '../pages/home/home';
import { SingleTechniquePage } from '../pages/single-technique/single-technique';
import { AboutPage } from '../pages/about/about';
import { HelloIonicPage } from '../pages/hello-ionic/hello-ionic';
import { ListPage } from '../pages/list/list';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { TranslateService, TranslateModule } from '@ngx-translate/core';

(...)

this.pages = [
      { title: 'menu_singletechnique', component: SingleTechniquePage },
      { title: 'menu_helloionic', component: HelloIonicPage },
      { title: 'menu_firstlist', component: ListPage },
      { title: 'menu_about', component: AboutPage }

    ];
  }

 (...)

  openPage(page) {
    // close the menu when clicking a link from the menu
    this.menu.close();
    // navigate to the new page if it is not the current page
    this.nav.setRoot(page.component);
  }

Once again, everything works fine untill I try to translate the loop content.

Thank you very much in advance !


Solution

  • You two make it look so simple that I now feel stupid...!

    I only had to replace in app.html :

    {{ {{p.title}} | translate }}
    

    by

    {{ p.title | translate }}
    

    Thank you for answering so quickly to such an easy question.