Problem with setting translocoConfig using httpcall in factory, when TranslocoService as dependency anywhere #468
Replies: 11 comments
-
I am not sure I completely understand what you’re trying to do, could you sum things up for us? |
Beta Was this translation helpful? Give feedback.
-
@itayod what I am trying to achieve is to have translations working, as they are not, in circumstances that I have - that's what I've tried to describe to best as I could, it's frustrating that you don't understand. Have you checked the minimal replication app that I have uploaded to github? I've put quite work in replicating the problem in new app. It's quite easy to follow from there - you just remove imperative setting of configuration from app.component.ts (lines 30-32) and you can see that i18n does not work anymore. It is not able to use translocoConfig settings that are provided by factory in app.module.ts, if TranslocoService is used as dependency... well... anywhere in the app! In this example it is used as dependency for HttpInterceptor in app.module.ts - after removing it, translations work again, but we need it as dependency. In summary, I am trying to get it work :-/ |
Beta Was this translation helpful? Give feedback.
-
You have a race condition. |
Beta Was this translation helpful? Give feedback.
-
@matusbielik have you checked the race condition @ArielGueta is referring to? can you please give an update here 🙂 |
Beta Was this translation helpful? Give feedback.
-
@shaharkazaz hello. I think it is pretty obvious that there is a race condition as I tried to explain this race condition two times in a row. I think that there are a few clear questions about my race condition problem that I tried to ask without any luck of having them answered, like: "Is there any way to set up this whole config & translation loading process in AppModule only, as with ngx-translate? Or is this simply impossible by design and the config values must be set imperatively from component?" Sure, my question is not best described as a bug but it is more of a asking for design advice for a problem that I would expect in a library's cookbook or something, because I believe that my use case must be pretty common as using config options that are loaded from file by http call is not something exotic and it worked pretty well with ngx-translate, which recommended transloco as a library to migrate to. Maybe it is more suitable for stack overflow post than for a github issue, but I hoped this will reach core developers. I have also put quite a work into recreating minimalistic reproducing of the problem so I was little bit disappointed with answer "race condition", because that's obvious. Maybe saying "this is not possible, you have to use the imperative way from app.component" would help better (and that's how I have "solved" it). So if nobody is about to try to solve or at least suggest best way to solve this issue, I believe you can close it. |
Beta Was this translation helpful? Give feedback.
-
Hi @matusbielik, have you tried loading the configuration you need using |
Beta Was this translation helpful? Give feedback.
-
@itayod ngx-config uses |
Beta Was this translation helpful? Give feedback.
-
@matusbielik I think the aforementioned In the main module: import {APP_INITIALIZER} from '@angular/core';
@NgModule({
// ...
providers: [{
provide: APP_INITIALIZER,
useFactory: myAppInitialization,
deps: [TranslocoService]
}]
})
class MyAppModule { } The factory itself: function myAppInitialization(translocoService: TranslocoService) {
return () => translocoService.load(/* your params */).toPromise();
} Your application will not be considered initialized until that promise resolves. |
Beta Was this translation helpful? Give feedback.
-
Guys, did anyone try to run that docker image @matusbielik prepared? Because what I see from discussion is that no one really understand (or tried to understand) what is the issue. He wants to migrate from ngx-translate to transloco (as transloco is proposed replacement for ngx-translate by the author himself). So summed up and simplified: Current state: First application loads configuration by ngx-config (uses No need to set any configuration values imperatively (i.e. list of available languages) in Desired state: In this setup it works for him, but he can not use/provide custom And finally his questions are:
|
Beta Was this translation helpful? Give feedback.
-
I still can't understand what's the difference with ngx-translate. Can you share how you implemented the |
Beta Was this translation helpful? Give feedback.
-
Because based on your code, I can't see any difference with the loaders. |
Beta Was this translation helpful? Give feedback.
-
I'm submitting a...
Current behavior
After migrating from ngx-translate, transloco can't load
translocoConfig
using factory, as the config object needs to be populated by data from ConfigService (ngx-config), which loads the config file loaded by http call, if the TranslocoService is dependency in any module or provided service, e.g. HttpInterceptor.I know this is very mouthful so please check the minimal reproduction example app below.
Expected behavior
We expect to be able to get values from ConfigService in customTranslateConfigFactory, just as when using ngx-translate, as the configuration of our app has not changed.
Minimal reproduction of the problem with instructions
Please check the code (you can install and serve the app directly, using docker is not mandatory)
in minimal reproduction app and let me explain:
The tech stack in this app is Transloco (migrated from ngx-translate mainly to support Ivy) and ngx-config.
There are slovak and english translation files, identified by lang identifier (sk or en) in the name of the file. The lang identifiers are to be loaded from config file and are replaced in url in HttpInterceptor.
Both config and translation json files are loaded by http calls, hosted on cdn. All the logic is currently in app.module.ts:
for config file:
for transaltions:
all good so far, both files are downloaded, also in correct order. We try to create
translocoConfig
using factory, as we need values from ConfigService.Now... This setup works!
But only as far as the TranslocoService is not used as dependency in anywhere.
In this app, it is used as dependency for CustomHttpInterceptor, where we want to set
this.translate.getActiveLang()
as X-App-Language request header. Please note that this use-case is only for example of the behavior, in the enterprise app we want to use injected TranslocoService in many places, e.g. in ngrx Effects etc.So when TranslocoService is used as dependency of HttpModule (i guess it is as it's used in HttpInterceptor), translocoConfig does not have the ConfigService data (as it needs HttpClient to download data) and the factory returns empty config values. It is a kind of circle dependency problem.
In current state the app works, because the values that have not been able to be set in module are set imperatively in app.component.ts (ConfigService returns correct config data in component):
But this is not the behavior we prefer (setting imperatively).
The app also works if we comment out imperative setting mentioned above, and we remove TranslocoService from CustomHttpInterceptor deps (app.module.ts --- line 85)
What is the motivation / use case for changing the behavior?
Please note that even when this seems to be ngx-config issue at first sight, I really believe it has to do with Transloco too, so please don't shove it off the table right away.
Before, when using ngx-translate, the whole config/translate initialization process was the same, except the class provided for TranslateModule looks little bit different:
in ngx-translate, we use TranslateHttpLoader class to load the data:
in Transloco, we load the translation file directly with HttpClient:
and this is the only difference.
Is there any way to set up this whole config & translation loading process in AppModule only, as with ngx-translate? Or is this simply impossible by design and the config values must be set imperatively from component?
Thanks for any suggestions!
Environment
Beta Was this translation helpful? Give feedback.
All reactions