core icon indicating copy to clipboard operation
core copied to clipboard

setTranslate does not work

Open MMcKester opened this issue 5 years ago • 5 comments

Hi,

I use setTranslation to add another translation, but the translate pipe does not resolve the entry.

At my program start I execute the following statement:

translate.setTranslation(lang, {HELLO: "foo"}, true);

And in my template I resolve the translation:

{{ 'HELLO' | translate }}

I can confirm that all other translations which got added by .json files through the normal startup and loading routines of ngx-translate work fine, but only my manual translation (in this case HELLO) fails. Does anyone know what I am doing wrong?

Thanks a lot!

MMcKester avatar Oct 05 '20 14:10 MMcKester

I find up (in my situation) it is json import problem.

beautifulinda avatar Mar 15 '21 09:03 beautifulinda

Same case for me. It is intermittent. Sometimes it works, sometimes it doesn't

And I'm pretty sure the second parameter always gets filled.

I tried using a Custom Loader instead of manually setting and again, no luck after publishing the package and importing it at a different project

angular 8, ngx-translate core 11

FlavioRodr avatar May 04 '21 18:05 FlavioRodr

Any update on this problem? We're facing the same behaviour.

"@ngx-translate/core": "^11.0.1",
"@ngx-translate/http-loader": "^4.0.0",

app.module

export function HttpLoaderFactory(http: HttpClient): TranslateHttpLoader {
  return new TranslateHttpLoader(http, assetUrl('i18n/'));
}

const translateModuleConfig = {
  loader: {
    provide: TranslateLoader,
    useFactory: HttpLoaderFactory,
    deps: [HttpClient],
  },
};

imports: [
...
  TranslateModule.forRoot(translateModuleConfig),
...

app.component

public ngOnInit() {
  this.translateService.setTranslation(language, { HELLO: "TEST" }, true);
  this.translateService.use(language);
  this.language = language;
  this.translateService.getTranslation(language).subscribe(lang => console.log({ lang }));
}

console.log outputs only translations from json file.

kmiasko avatar Jul 05 '21 09:07 kmiasko

Same issue but only once built, works fine in NG 12 and in serve mode.

smasala avatar Jul 21 '21 13:07 smasala

Turns out it was down to the translation service working in a non-singleton context and therefore loosing the translations at some point down the line 🤷‍♂️

smasala avatar Jul 21 '21 16:07 smasala

Turns out it was down to the translation service working in a non-singleton context and therefore loosing the translations at some point down the line 🤷‍♂️

Hi @smasala, how did you solve this problem?

Heila-Almogren avatar May 07 '23 08:05 Heila-Almogren

Turns out it was down to the translation service working in a non-singleton context and therefore loosing the translations at some point down the line 🤷‍♂️

Hi @smasala, how did you solve this problem?

Can't remember exactly, don't have the code base anymore. I think it had to do with the dependency injection scope (provideIn).

smasala avatar May 07 '23 10:05 smasala