core: Using TranslateService in a provider is not working

I’m submitting a … (check one with “x”)

[x] bug report => check the FAQ and search github for a similar issue or PR before submitting
[ ] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior Does not find the TranslateService…

Reproduction of the problem

Add a provider, import TranslateService and add it to the constructor… says “The name “TranslateService” cannot be found.”

What is the expected behavior?

Please tell us about your environment:

cordova CLI: 6.5.0
Ionic Framework Version: 2.1.0-201703061537
Ionic CLI Version: 2.2.1
Ionic App Lib Version: 2.2.0
Ionic App Scripts Version: 1.1.3
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 10
Node Version: v6.10.0
Xcode version: Not installed

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 24

Most upvoted comments

Calling getTranslation before get resolves the issue.

Make sure to use service.use ('nl'); somewhere at the start of you application. Then you will get the correct values.

One thing that might be the reason why you think it’s not working is because the result is not an array but an object with key-value pairs.

Try the following things, what does this output?

this.translate.get('LOGIN').subscribe(result => {
    console.log(result);
});

this.translate.get(['LOGIN','LOADING']).subscribe(result => {
    console.log(JSON.stringify(result));
});

console.log(this.translate.instant('LOGIN'));

Thanks was Visual Studio… still got a problem.

Can you get the translation data. save it and then use it later? Because this way I just get the name of the values and not the values itself 😕

private translationLet: any;

this.translate.get(['LOGIN','LOADING','DOWNLOADED','NOCONNECTION']).subscribe((translation: [string]) => {
        this.translationLet = translation;
    });

@AlexanderKozhevin

This works for me, I have called getTranslation before calling get method:

  translated: Array<{display: string, target: any}> = [];

    var tmp = this.translate.getTranslation('fa');

    var keys= ['AppTitle','Home.Register','AboutUs.Title'];
    this.translate.get(keys).subscribe(
      value => {
        var values = _.values(value);
        for(var key in keys){
          this.translated.push({display:values[key],target:keys[key]});
        }
      });

Note that I’m using lodash plugin to work with arrays