core icon indicating copy to clipboard operation
core copied to clipboard

TranslateStore being created a second time causing no translations in lazy loaded module

Open pjc2007 opened this issue 1 year ago • 3 comments

Current behavior

I have outlined this here in this post, but not expecting much feed back from there.

In summary, as in the post, when loading my second lazy loaded module, I am seeing a second TranslateStore being created, and this being empty I get no translations. This has never happened before I upgraded to v15.0.0, and does not happen a small sample project, which makes it hard to demonstrate. I am doing exactly the same setup in the broken application as in a small sample app that works, so really have no idea of what is different to cause this (obviously my production application has a lot of other dependencies, but nothing else to do with translations)

I have spent a very long time trying to debug, but I just cannot see why a second TranslateStore is being created, as it goes very deep into the Angular DI code, which I just cant follow, so hoping there may be some suggestions on what could possibly cause this.

Expected behavior

To stop this second instance of TranslateStore being created so translation will one work in all modules.

How do you think that we should fix this?

I have no idea why the second TranslateStore is being created. I notice there is a always a new instance of TranslateService for each module being created, but this also occurs in a sample application where I do not have any problems

Environment


ngx-translate version: 15.0.0
Angular version:16.1.3

Browser:
- [ x] Chrome (desktop) version 115.0.5790.110

pjc2007 avatar Jul 29 '23 03:07 pjc2007

I have the exact same issue, on Angular 16. All my translations work outside lazy loaded modules but inside of it properties of TranslateService are undefined.

skrilax91 avatar Aug 31 '23 08:08 skrilax91

My workaround has been to set a reference somewhere at startup (e.g. in a service) to translate.store

this.translator.use(hostInfo.locale);
this.translateStoreLoaderService.setStore(this.translator.store);

Then at the start (e.g. constructor) of each lazy component

translateStoreResetService.resetStore(translate);

with..

public setStore(translateStore: TranslateStore): void {   
    this.translateStore = translateStore;
}

public resetStore(translateService: TranslateService): void {   
   translateService.store = this.translateStore;   
}

The only way I could get it to work after trying many other things.

pjc2007 avatar Aug 31 '23 08:08 pjc2007

Your solution work perfectly !

skrilax91 avatar Aug 31 '23 09:08 skrilax91