core: Translateservice returns keys in service/Injectable constructor

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 using the translateservice.get() method inside a constructor of a provider/service in ionic 3 does return the translation Keys instead of their values in the subscription.

Expected/desired behavior I would expect the translateserice.get() method to work in a service (@Injectable) constructor so that I can store the values retrieved in a plain javascript array to use them in my service’s methods.

Reproduction of the problem

@Injectable()
export class UserService {

constructor(private translateService : TranslateService){

    private translations : [string];

    this.translateService.get(['SIGNUP_SUCCESS','DOES_NOTEXIST'])
      .subscribe(values=> {
        this.translations = values; //resolves to {DOES_NOTEXIST : "DOES_NOTEXIST", SIGNUP_SUCCESS:"SIGNUP_SUCCESS"}
      })
  }
}

In a @Page or outside of the constructor (inside a method of the UserService) it will work. My providers are added to the app.module.ts file. There the UserService provider is added to the providers Array. And there also is the following :

TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      }
    }),

Please tell us about your environment:

  • ngx-translate version: 8.0.0

  • Angular version: 5.0.0

  • Browser: [Chrome 63]

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 1
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Hello I have this issue also, shouldn’t the translateService.get wait for the loader to finish? I’d assume that this is a safe way to request translations without worrying about race conditions, correct?

I have this same situation like @LeonZhangCarsales but I have already TranslateModule.forChild() I did what @MikaelLambert done and this is working for me - but this is only workaround. Any solution for this?

What I find out problem for slow connection to API (I’m loading JSONs from API) and when I need load few JSONs.

I’m having the same problem in my Ionic 4 app when I try to subscribe to TranslateService.get(). In the end, all I receive is the key I supplied to the function. Does anyone at least have a workaround? Thanks.

Have same problem… thanks Reproduced here: https://plnkr.co/edit/Q5ZcXpWbRpXSZEC4EA5P?p=preview see the console log: screenshot at jan 25 09-29-07

It’s hard to debug what is going on. Could you share the repository or use the following template as a starting point? http://plnkr.co/edit/tpl:01UjWY3TKfP6pgwXKuEa