question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.forChild() usage with lazy loading routes

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request

Current behavior AppModule

export function createTranslateLoader(http: Http) {
    return new TranslateHttpLoader(http, "/i18n/", "/index.json");
}
imports: [
    TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: createTranslateLoader,
                deps: [Http]
            }
        }),
]

SharedModule

imports: [
    //TranslateModule
],
exports: [
    //TranslateModule,
]

AdminModule

export function createTranslateLoader(http: Http) {
    return new TranslateHttpLoader(http, "/i18n/", "/admin.json");
}
imports: [
    TranslateModule.forChild({
            loader: {
                provide: TranslateLoader,
                useFactory: createTranslateLoader,
                deps: [Http]
            }
        }),
]

Expected/desired behavior I would like to load additional JSON files per component / module, i.e. an admin.json for the administration panel of our app. I would like to import one TranslateModule inside of SharedModule and configure it allow all modules that don’t explicite call .forChild().

Both doesn’t work, it simply doesn’t load the admin.json and hence doesn’t translate strings defined in that file.

Please tell us about your environment:

  • ngx-translate version: 6.0.0-beta.1

  • Angular version: 2.4.7

  • Browser: [Firefox 51.0.1]

  • Language: [TypeScript 2.0]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:47
  • Comments:53 (5 by maintainers)

github_iconTop GitHub Comments

31reactions
subhani1249commented, Feb 26, 2019

appModule.ts

image

Create one shared module translateSharedModule.ts

image

appComponent.ts import {TranslateService} from ‘@ngx-translate/core’; constructor(public translate: TranslateService) { translate.setDefaultLang(‘en’);

}

homeModule.ts //or any other submodules import { TranslateSharedLazyModule } from ‘…/…/Common/translateSharedLazyModule’; imports : [
TranslateSharedLazyModule ], …

28reactions
neolanderscommented, Feb 21, 2017

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 setup a way that work pretty well for me:

I’ve created two SharedModules, (one for lazyLoading and one for the other part of my application)

SharedLazyModule for lazy loading content:

@NgModule({
  imports: [
    HttpModule,
    CommonModule,
    TranslateModule.forChild({}),
  ],
  exports: [
    CommonModule,
    TranslateModule
  ]
})
export class SharedLazyModule {}

SahredModule for App

// AoT requires an exported function for factories
export function HttpLoaderFactory(http: Http) {
   return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

@NgModule({
    imports: [
      HttpModule,
      CommonModule,
      TranslateModule.forRoot({
           provide: TranslateLoader,
           useFactory: HttpLoaderFactory,
           deps: [Http],
         })
    ],
    exports: [
      CommonModule,
      TranslateModule
    ]
})
export class SharedModule {
    
     constructor(private translate: TranslateService) {
                  
        translate.addLangs(["en", "fr"]);
        translate.setDefaultLang('en');

        let browserLang = translate.getBrowserLang();
        translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
    }
}

See Plunker: https://plnkr.co/LVmIuI1Xw9vFn0IuC2jW

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy-loading feature modules - Angular
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. AppRoutingModule (excerpt)
Read more >
How To Use Lazy Loading Routes in Angular | DigitalOcean
To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy ...
Read more >
How to Nest Lazy-Loaded Modules - DEV Community ‍ ‍
This route is now lazy-loaded. Note that in this module we use the forChild() method instead of forRoot(). You must do this for...
Read more >
Routing and Lazy Loading with Angular's Standalone ...
It allows to get hold of all the providers defined in existing NgModules and hence is the key for using them with Standalone...
Read more >
How to Nest Lazy-Loaded Modules - Medium
A not-nested, lazy-loaded module. This route is now lazy-loaded. Note that in this module we use the forChild() method instead of forRoot().
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found