Ngx translate with shared/lazy loading modules












2















Probably one of the most common questions, while the documentation and some other questions I found try to clear up things for me, yet I am still not sure how to fix this.



So this is my structure:



enter image description here




  • App module is of course the main module that is bootstrapped

  • Countryselector module: takes care of loading the default country(based on IP) and takes care of deciding what language to use based on the browser. It basically is the core functionality of deciding what country/language is used + it contains a dropdown for a user to change the country/language.

  • Checkout module, selection module, payment module are all lazy-loaded with routing.


AppModule



export function HttpLoaderFactory(http: HttpClient) {
return new TranslateHttpLoader(http);
}

@NgModule({
declarations: [
AppComponent
],
imports: [
HttpClientModule,
BrowserModule,
AppRoutingModule,
ContrySelectorModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
export class AppModule { }


CountrySelectorModule



@NgModule({
declarations: [CountryselectorComponent],
imports: [
CommonModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}
})
],
exports: [
CountryselectorComponent
]
})
export class ContrySelectorModule { }


Selection module:



@NgModule({
declarations: [SelectionComponent],
imports: [
CommonModule,
SelectionRoutingModule,
UspblockModule,
TranslateModule.forChild({//or forRoot, no idea how to configure this
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpClient]
}})
],
})
export class SelectionModule { }


Now the problem is that I don't want to do the language configuration again within the lazy-loaded modules, because the country selector module already takes care of this. I am not sure now how to correctly configure the lazy-loaded modules (and actually not sure if countrySelectorModule is done correctly). Using the standard github documentation I was not able to pull this off. I've tried messing around with .forChild() but no luck so far.



Do I need a share module? Do I need to import countrySelectorModule everywhere (not preferred)? Custom loader? t.b.h. I have no idea and the documentation is a bit short for more advanced scenarios.










share|improve this question





























    2















    Probably one of the most common questions, while the documentation and some other questions I found try to clear up things for me, yet I am still not sure how to fix this.



    So this is my structure:



    enter image description here




    • App module is of course the main module that is bootstrapped

    • Countryselector module: takes care of loading the default country(based on IP) and takes care of deciding what language to use based on the browser. It basically is the core functionality of deciding what country/language is used + it contains a dropdown for a user to change the country/language.

    • Checkout module, selection module, payment module are all lazy-loaded with routing.


    AppModule



    export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http);
    }

    @NgModule({
    declarations: [
    AppComponent
    ],
    imports: [
    HttpClientModule,
    BrowserModule,
    AppRoutingModule,
    ContrySelectorModule,
    TranslateModule.forRoot({
    loader: {
    provide: TranslateLoader,
    useFactory: HttpLoaderFactory,
    deps: [HttpClient]
    }
    })
    ],
    export class AppModule { }


    CountrySelectorModule



    @NgModule({
    declarations: [CountryselectorComponent],
    imports: [
    CommonModule,
    TranslateModule.forRoot({
    loader: {
    provide: TranslateLoader,
    useFactory: HttpLoaderFactory,
    deps: [HttpClient]
    }
    })
    ],
    exports: [
    CountryselectorComponent
    ]
    })
    export class ContrySelectorModule { }


    Selection module:



    @NgModule({
    declarations: [SelectionComponent],
    imports: [
    CommonModule,
    SelectionRoutingModule,
    UspblockModule,
    TranslateModule.forChild({//or forRoot, no idea how to configure this
    loader: {
    provide: TranslateLoader,
    useFactory: HttpLoaderFactory,
    deps: [HttpClient]
    }})
    ],
    })
    export class SelectionModule { }


    Now the problem is that I don't want to do the language configuration again within the lazy-loaded modules, because the country selector module already takes care of this. I am not sure now how to correctly configure the lazy-loaded modules (and actually not sure if countrySelectorModule is done correctly). Using the standard github documentation I was not able to pull this off. I've tried messing around with .forChild() but no luck so far.



    Do I need a share module? Do I need to import countrySelectorModule everywhere (not preferred)? Custom loader? t.b.h. I have no idea and the documentation is a bit short for more advanced scenarios.










    share|improve this question



























      2












      2








      2


      2






      Probably one of the most common questions, while the documentation and some other questions I found try to clear up things for me, yet I am still not sure how to fix this.



      So this is my structure:



      enter image description here




      • App module is of course the main module that is bootstrapped

      • Countryselector module: takes care of loading the default country(based on IP) and takes care of deciding what language to use based on the browser. It basically is the core functionality of deciding what country/language is used + it contains a dropdown for a user to change the country/language.

      • Checkout module, selection module, payment module are all lazy-loaded with routing.


      AppModule



      export function HttpLoaderFactory(http: HttpClient) {
      return new TranslateHttpLoader(http);
      }

      @NgModule({
      declarations: [
      AppComponent
      ],
      imports: [
      HttpClientModule,
      BrowserModule,
      AppRoutingModule,
      ContrySelectorModule,
      TranslateModule.forRoot({
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }
      })
      ],
      export class AppModule { }


      CountrySelectorModule



      @NgModule({
      declarations: [CountryselectorComponent],
      imports: [
      CommonModule,
      TranslateModule.forRoot({
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }
      })
      ],
      exports: [
      CountryselectorComponent
      ]
      })
      export class ContrySelectorModule { }


      Selection module:



      @NgModule({
      declarations: [SelectionComponent],
      imports: [
      CommonModule,
      SelectionRoutingModule,
      UspblockModule,
      TranslateModule.forChild({//or forRoot, no idea how to configure this
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }})
      ],
      })
      export class SelectionModule { }


      Now the problem is that I don't want to do the language configuration again within the lazy-loaded modules, because the country selector module already takes care of this. I am not sure now how to correctly configure the lazy-loaded modules (and actually not sure if countrySelectorModule is done correctly). Using the standard github documentation I was not able to pull this off. I've tried messing around with .forChild() but no luck so far.



      Do I need a share module? Do I need to import countrySelectorModule everywhere (not preferred)? Custom loader? t.b.h. I have no idea and the documentation is a bit short for more advanced scenarios.










      share|improve this question
















      Probably one of the most common questions, while the documentation and some other questions I found try to clear up things for me, yet I am still not sure how to fix this.



      So this is my structure:



      enter image description here




      • App module is of course the main module that is bootstrapped

      • Countryselector module: takes care of loading the default country(based on IP) and takes care of deciding what language to use based on the browser. It basically is the core functionality of deciding what country/language is used + it contains a dropdown for a user to change the country/language.

      • Checkout module, selection module, payment module are all lazy-loaded with routing.


      AppModule



      export function HttpLoaderFactory(http: HttpClient) {
      return new TranslateHttpLoader(http);
      }

      @NgModule({
      declarations: [
      AppComponent
      ],
      imports: [
      HttpClientModule,
      BrowserModule,
      AppRoutingModule,
      ContrySelectorModule,
      TranslateModule.forRoot({
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }
      })
      ],
      export class AppModule { }


      CountrySelectorModule



      @NgModule({
      declarations: [CountryselectorComponent],
      imports: [
      CommonModule,
      TranslateModule.forRoot({
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }
      })
      ],
      exports: [
      CountryselectorComponent
      ]
      })
      export class ContrySelectorModule { }


      Selection module:



      @NgModule({
      declarations: [SelectionComponent],
      imports: [
      CommonModule,
      SelectionRoutingModule,
      UspblockModule,
      TranslateModule.forChild({//or forRoot, no idea how to configure this
      loader: {
      provide: TranslateLoader,
      useFactory: HttpLoaderFactory,
      deps: [HttpClient]
      }})
      ],
      })
      export class SelectionModule { }


      Now the problem is that I don't want to do the language configuration again within the lazy-loaded modules, because the country selector module already takes care of this. I am not sure now how to correctly configure the lazy-loaded modules (and actually not sure if countrySelectorModule is done correctly). Using the standard github documentation I was not able to pull this off. I've tried messing around with .forChild() but no luck so far.



      Do I need a share module? Do I need to import countrySelectorModule everywhere (not preferred)? Custom loader? t.b.h. I have no idea and the documentation is a bit short for more advanced scenarios.







      angular lazy-loading angular7 ngx-translate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 15:32







      CularBytes

















      asked Nov 21 '18 at 13:53









      CularBytesCularBytes

      4,34224063




      4,34224063
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I solved this by doing the following:




          1. Create a CoreModule (basically a shared module), with below code


          CoreModule



          export function HttpLoaderFactory(http: HttpClient) {
          return new TranslateHttpLoader(http);
          }

          @NgModule({
          declarations: ,
          imports: [
          CommonModule,
          TranslateModule.forChild({}) //for root is done in app module, this core module is loaded in other lazy-loaded modules.
          ],
          exports: [TranslateModule],
          })
          export class CoreModule {

          static forRoot(): ModuleWithProviders {
          return {
          ngModule: CoreModule,
          providers: [ShoppingCartService, AccountService]
          }
          }
          }


          This one also solved making sure that ShoppingCartService and AccountService are only one instance and not every lazy-module creating a separate service.




          1. Inside the CountrySelectorModule keep the imports the same, just add CoreModule to the imports.


          2. Change AppModule:



          AppModule



          @NgModule({
          declarations: [
          AppComponent
          ],
          imports: [
          HttpClientModule,
          BrowserModule,
          AppRoutingModule,
          CoreModule.forRoot(),//notice this call
          ContrySelectorModule,

          ],
          providers: [ ],
          bootstrap: [AppComponent]
          })
          export class AppModule { }



          1. Any lazy loaded module just has to import CoreModule and TranslateModule without any method calls.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413612%2fngx-translate-with-shared-lazy-loading-modules%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I solved this by doing the following:




            1. Create a CoreModule (basically a shared module), with below code


            CoreModule



            export function HttpLoaderFactory(http: HttpClient) {
            return new TranslateHttpLoader(http);
            }

            @NgModule({
            declarations: ,
            imports: [
            CommonModule,
            TranslateModule.forChild({}) //for root is done in app module, this core module is loaded in other lazy-loaded modules.
            ],
            exports: [TranslateModule],
            })
            export class CoreModule {

            static forRoot(): ModuleWithProviders {
            return {
            ngModule: CoreModule,
            providers: [ShoppingCartService, AccountService]
            }
            }
            }


            This one also solved making sure that ShoppingCartService and AccountService are only one instance and not every lazy-module creating a separate service.




            1. Inside the CountrySelectorModule keep the imports the same, just add CoreModule to the imports.


            2. Change AppModule:



            AppModule



            @NgModule({
            declarations: [
            AppComponent
            ],
            imports: [
            HttpClientModule,
            BrowserModule,
            AppRoutingModule,
            CoreModule.forRoot(),//notice this call
            ContrySelectorModule,

            ],
            providers: [ ],
            bootstrap: [AppComponent]
            })
            export class AppModule { }



            1. Any lazy loaded module just has to import CoreModule and TranslateModule without any method calls.






            share|improve this answer




























              0














              I solved this by doing the following:




              1. Create a CoreModule (basically a shared module), with below code


              CoreModule



              export function HttpLoaderFactory(http: HttpClient) {
              return new TranslateHttpLoader(http);
              }

              @NgModule({
              declarations: ,
              imports: [
              CommonModule,
              TranslateModule.forChild({}) //for root is done in app module, this core module is loaded in other lazy-loaded modules.
              ],
              exports: [TranslateModule],
              })
              export class CoreModule {

              static forRoot(): ModuleWithProviders {
              return {
              ngModule: CoreModule,
              providers: [ShoppingCartService, AccountService]
              }
              }
              }


              This one also solved making sure that ShoppingCartService and AccountService are only one instance and not every lazy-module creating a separate service.




              1. Inside the CountrySelectorModule keep the imports the same, just add CoreModule to the imports.


              2. Change AppModule:



              AppModule



              @NgModule({
              declarations: [
              AppComponent
              ],
              imports: [
              HttpClientModule,
              BrowserModule,
              AppRoutingModule,
              CoreModule.forRoot(),//notice this call
              ContrySelectorModule,

              ],
              providers: [ ],
              bootstrap: [AppComponent]
              })
              export class AppModule { }



              1. Any lazy loaded module just has to import CoreModule and TranslateModule without any method calls.






              share|improve this answer


























                0












                0








                0







                I solved this by doing the following:




                1. Create a CoreModule (basically a shared module), with below code


                CoreModule



                export function HttpLoaderFactory(http: HttpClient) {
                return new TranslateHttpLoader(http);
                }

                @NgModule({
                declarations: ,
                imports: [
                CommonModule,
                TranslateModule.forChild({}) //for root is done in app module, this core module is loaded in other lazy-loaded modules.
                ],
                exports: [TranslateModule],
                })
                export class CoreModule {

                static forRoot(): ModuleWithProviders {
                return {
                ngModule: CoreModule,
                providers: [ShoppingCartService, AccountService]
                }
                }
                }


                This one also solved making sure that ShoppingCartService and AccountService are only one instance and not every lazy-module creating a separate service.




                1. Inside the CountrySelectorModule keep the imports the same, just add CoreModule to the imports.


                2. Change AppModule:



                AppModule



                @NgModule({
                declarations: [
                AppComponent
                ],
                imports: [
                HttpClientModule,
                BrowserModule,
                AppRoutingModule,
                CoreModule.forRoot(),//notice this call
                ContrySelectorModule,

                ],
                providers: [ ],
                bootstrap: [AppComponent]
                })
                export class AppModule { }



                1. Any lazy loaded module just has to import CoreModule and TranslateModule without any method calls.






                share|improve this answer













                I solved this by doing the following:




                1. Create a CoreModule (basically a shared module), with below code


                CoreModule



                export function HttpLoaderFactory(http: HttpClient) {
                return new TranslateHttpLoader(http);
                }

                @NgModule({
                declarations: ,
                imports: [
                CommonModule,
                TranslateModule.forChild({}) //for root is done in app module, this core module is loaded in other lazy-loaded modules.
                ],
                exports: [TranslateModule],
                })
                export class CoreModule {

                static forRoot(): ModuleWithProviders {
                return {
                ngModule: CoreModule,
                providers: [ShoppingCartService, AccountService]
                }
                }
                }


                This one also solved making sure that ShoppingCartService and AccountService are only one instance and not every lazy-module creating a separate service.




                1. Inside the CountrySelectorModule keep the imports the same, just add CoreModule to the imports.


                2. Change AppModule:



                AppModule



                @NgModule({
                declarations: [
                AppComponent
                ],
                imports: [
                HttpClientModule,
                BrowserModule,
                AppRoutingModule,
                CoreModule.forRoot(),//notice this call
                ContrySelectorModule,

                ],
                providers: [ ],
                bootstrap: [AppComponent]
                })
                export class AppModule { }



                1. Any lazy loaded module just has to import CoreModule and TranslateModule without any method calls.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 14:20









                CularBytesCularBytes

                4,34224063




                4,34224063






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413612%2fngx-translate-with-shared-lazy-loading-modules%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?