-
-
Notifications
You must be signed in to change notification settings - Fork 571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It can't work normally within lazyload(loadChildren) NgModules #232
Comments
Did you define import the TranslateModule in your root app module ? |
@ocombe, no, I only import the TranslateModule in the share module. I have 4 modules, others are article, user, and root app. How could I do? |
if you import it in the shared module, you should add it to the exports of the shared module as well |
@ocombe, I aready add it to the exports.https://github.com/doxiaodong/darlin-angular2/blob/develop/src/app/base/index.ts#L55 |
oh you're calling the service from within the module class, and you're calling "use" again there, you shouldn't have to do that |
@ocombe, I forgot to tell you that I call 'use' in the root app https://github.com/doxiaodong/darlin-angular2/blob/develop/src/app/footer/footer.component.ts#L45, and if I do not call in a lazyload module(like article), it will look like as follow. |
You should define all of that in the app root component: https://github.com/doxiaodong/darlin-angular2/blob/develop/src/app/app.component.ts |
I'm sorry, I do it, but it doesn't work. doxiaodong/darlin-angular2@0ebc0ee |
#217 is probably related to this. I have a similar setup and it's not working in lazy-loaded sub modules. |
Also getting same behaviour/error. Using https://github.com/AngularClass/angular2-webpack-starter and following your sharedModule setup. Works fine for regular components but not for lazy loaded ones. |
I had the same problem. |
Ok I just checked the problem, this is what I did:
@NgModule({
imports: [
...MODULES,
TranslateModule.forRoot({
provide: TranslateLoader,
useFactory: (http: Http) => new TranslateLocalStorageLoader(http, 'assets/i18n', '.json?t=' + (new Date().getTime())),
deps: [Http]
})
],
declarations: [
...COMPONENTS,
...DIRECTIVES,
...PIPES
],
providers: [
...PROVIDED_SERVICES
],
exports: [
...COMPONENTS,
...DIRECTIVES,
...PIPES,
// modules
...MODULES,
TranslateModule
]
})
export class SharedModule {
constructor(translate: TranslateService) {
// get the current UserLang
userLang = translate.getBrowserLang();
// this language will be used as a fallback when a translation isn't found in the current language
translate.setDefaultLang('en');
// the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use(userLang);
}
} And voilà ! It works in all my lazy loaded modules, as long as they import the SharedModule |
Well, first of all, thanks for having a look at this, but I noticed this does not completely fix the problem. What it does fix is that no strings will be translated at all. What it doesn't fix is changing the language (using EDIT: export class SharedModule {
public static translate;
constructor(translate: TranslateService) {
SharedModule.translate = translate;
// ...code from above
}
} export class NavbarComponent {
private switchLang(lang: string): void {
// this.translate.use(lang);
SharedModule.translate.use(lang);
}
} Could you please have a look at this again? Thank you. |
It fetch '*.json' when each NgModule loaded |
ok, I still have a trick in my sleeve if I cannot find a way to fix this, I can make the translations and currentLang static so that you can have multiple instances but they keep the same "state", but it means that it won't be possible to have different instances of the service in different states (I don't think it's a good idea, but there may be some use cases). I will try to fix it this week end, but I don't know if I'll have the time (because I need to prepare myself for angular connect next week). |
Singleton services provided in shared modules are still cloned by all lazy modules which is an expected behaviour, look here https://angular.io/docs/ts/latest/guide/ngmodule.html#!#shared-module
|
Ok, static instances it is then, thanks for the info |
I'm not sure about that. I think the correct behaviour was already proposed here #209 |
@chpasha Yes, that is the correct way to do this. Never ever call |
Yep, SamVerschueren's proposed solution works perfectly for me. |
@SamVerschueren nice, and should I close this issue? @ocombe |
Yes :) |
Since I didn't found any plunker working with ngx-translate librairy and I also had some difficulties to manage to make it work with LoadChildren, I've created two SharedModules, (one for lazyLoading and one for the other part of my application) SharedLazyModule for lazy loading content:
SahredModule for App
See Plunker: |
Hi @neolanders What about putting the second one into a |
Hi @dancancro, you are totaly right. The SharedModule could be the appModule or any other CoreModule. |
@neolanders Thanks. Can you take a look at my project and see what I might be doing wrong? It is based on JHipster and so the language services are provided by way of JhiLanguageService in the I don't know how to make my lazy-loaded pages use the same translation instances as the eager-loaded part of the app so that changing the language in an eager component of the nav bar affects the language used by the lazy-loaded pages. I have also tried this idea but I think it suffers from the same problem that I don't know how to control ngx-translate configuration in a JHipster app. This is probably more a JHipster question but maybe you have some ideas. I'm asking in different places. Such is life when using open source software. |
Hi @dancancro Did you fixed the above JHipster issues. On Following file, this library has its own loader, so in the browser two times the json files is getting loaded, one from app.modules.ts and this below json from node_modules folder. [Visit] (https://github.com/jhipster/ng-jhipster/blob/v0.3.6/index.ts )
|
@karthic200mw Hi. Sorry, I have not looked at this project in years. Good luck. |
Thanks, There was an issue in the loading of webpack, For translation i am using the package @larscom/ngx-translate-module-loader. but added translation in main.json file and now its working fine. Thanks @deepu105 for adding support for the ngx-translate in the ngjhipster package. |
I'm submitting a ... (check one with "x")
Current behavior
when I use lazyload(loadChildren) route, it's not easy to subscribe TranslateService change between each NgModules. I create a global translate service to subscribe the change but it fetch '*.json' when each NgModule loaded.
Expected/desired behavior
It can auto change the language within NgModules
Please tell us about your environment:
The text was updated successfully, but these errors were encountered: