Ionic TranslateService http.get


#1

Hi. I’ve just finished to translate all my app in multiple languages and now I want to get the specific content for each language but it doesn’t work and I don’t know why.
Here is my code.

  import { TranslateService } from '@ngx-translate/core';
 public dblanguage;
  constructor(private translate: TranslateService) 
  {
  this.translate.get('Language.db').subscribe(translated => {
   this.dblanguage = translated;
  });
  this.http.get('https://www.url.com/app.php?lang='this.dblanguage)
  .map(res => res.json())
  .subscribe(data => {
        this.posts = data;
    },
    err => {
        //
    });
   
  }
  navigate(link)
   {
     this.navCtrl.push(PlaytestPage,{
     url: link,
     lang: this.dblanguage
    })
    }

Something is wrong but I can’t figure out what!
Thank you!

Edit: It seems that this is not working on first tab land. If I use this on the second tab is working. But one thing is also not working. The http.get is not updating after I change the language from my account only the images where I have pic_{{‘Language.db’ | translate}}.jpg ( language.db = en, es, pt )


#2

You need to wait till

this.translate.get('Language.db').subscribe(translate

is finished before you can run

this.http.get('https://www.url.com/app.php?lang='this.dblanguage)....

so you know the language variable.

Something like

this.translate.get('Language.db').subscribe(translated => {

  this.http.get('https://www.url.com/app.php?lang='translated)
    .map(res => res.json())
    .subscribe(data => {
          this.posts = data;
      },
      err => {
          //
      });
  });
  

#3

Thank you. Still not working!
When I first enter on my app on home.html it doesn’t get the Language.db. I’ve created a ionRefresh, so after I refresh only it works but at first entry it returns Language.db not “en” or pt.

Also from account.html if I change the language to pt the http.get is not updated only the html on each pages.

Edit:
I managed to get the language at first app entry. I’ve moved the default language from app.compontent.ts to home.ts

translate.setDefaultLang('en');

But now how can I update the http.get after I change the language from account.html ? I have this.

    <ion-item>
      <ion-label>{{'AccountPage.Changelanguage' | translate}}</ion-label>
      <ion-select [(ngModel)]="changeLanguage" (ionChange)="onChange(changeLanguage)">
        <ion-option value="en" checked="true">{{'Languages.en' | translate}}</ion-option>
        <ion-option value="pt">{{'Languages.pt' | translate}}</ion-option>
        <ion-option value="hu">{{'Languages.hu' | translate}}</ion-option>
        <ion-option value="id">{{'Languages.id' | translate}}</ion-option>
        <ion-option value="ro">{{'Languages.ro' | translate}}</ion-option>
      </ion-select>
    </ion-item>
    onChange(changeLanguage){
      this.translate.setDefaultLang(changeLanguage);
      
    }

My html is changing but my http.get url it’s the same ( dynamic content is not changed ). Is there a way to clear cache from tabs?
Thank you!


#4

You can use a lifecycle event so you can set the variable before the page is loaded
http://blog.ionic.io/navigating-lifecycle-events/

The best way to do this is to create a service so you can access the language variable on each component/page.
http://jbavari.github.io/blog/2015/11/06/ionic-2-dot-0-generators-services/

Maybe you can use the ‘ionViewWillEnter’ lifecycle hook?