0

I used this tutorial

https://ionicframework.com/docs/developer-resources/ng2-translate/

everything run , I can take json format but not print output

en.json

{
    "HELLO": "hola",
    "hi":"sa"
  }

.ts constructor;

 constructor(public navCtrl: NavController, public navParams: NavParams, public translate: TranslateService) {
    translate.setDefaultLang('en');
  }

.html block;

<div style="width: 100%;">           
        <p class="baslik">ACCOUNT</p>
      <button ion-item>
          <ion-label>{{ 'hi' | translate }}</ion-label>
      </button>
      <button ion-item>
          <ion-label>{{ 'hi' | translate }}</ion-label>
      </button>
  </div>

but output;

hi

hi

when add button click event , this code block;

this.translate.use('en').subscribe(
      value => {
        // value is our translated string
        console.log(value);
      }

en.json return log;

enter image description here

2 Answers 2

1

I followed the tutorial you provided and could reproduce your problem.

I had also a look in the official documentation of the ngx-translate repository on github and there is a difference to your tutorial.

In the tutorial they are using the Http class of the Angular Http module to load the files:

export function createTranslateLoader(http: Http) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

In the official documentation they are using the HttpClient class of the Angular common module. Change the following in your App Module:

import { HttpClientModule, HttpClient } from '@angular/common/http';

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

There is a comment in the official documentation

NB: if you're still on Angular <4.3, please use Http from @angular/http with [email protected].

Sign up to request clarification or add additional context in comments.

7 Comments

I have added all code from tutorial , but return thats output
Which angular version are you using?
4.1.3 ----------------
I used http instead of httpClient
Use @ngx-translate/http-loader version 0.1.0, the latest version of @ngx-translate/core and Http instead of HttpClient. That should fix your problem
|
1

I'm not familiar with the translation framework you're using but it looks from their documentaiton that you want to use a custom translation definition, but you never call:

translate.setTranslation('en', myTranslation);

where myTranslation is your object.

The 'en' in your code that you're setting as the default is a string (it's in quotes), and looks to be some kind of identifier that the translation service uses to decide what the fallback language is:

Methods:

setDefaultLang(lang: string): Sets the default language to use as a fallback

You want:

setTranslation(lang: string, translations: Object, shouldMerge: boolean = false): Manually sets an object of translations for a given language, set shouldMerge to true if you want to append the translations instead of replacing them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.