ngx-translate / core

The internationalization (i18n) library for Angular
MIT License
4.5k stars 574 forks source link

Provide working plunkr to show multimodule setup #640

Open tommueller opened 7 years ago

tommueller commented 7 years ago

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 I cannot figure out how to use ngx-translate throughout a main-module and several child-modules. I have a shared library that is included into the main-project, but I cannot manage to get them both to work with the same instance of ngx-translate. My child-module is in a separate library, installed via npm and then imported into the project.

Expected/desired behavior Have a working plunkr example where I can see how I have to declare the modules.

Reproduction of the problem There is a plunkr here, that kind of works, but still does not do the translation: https://plnkr.co/edit/LVmIuI1Xw9vFn0IuC2jW?p=preview

What I've tried I basically managed to get every case working, BUT the one I need:

Right now I get No provider for TranslateService and this is my setup:

shared.module:

@NgModule({
    exports: [
        TranslateModule
    ],
    imports: [
        TranslateModule
    ]
})
export class SharedModule {
    static forRoot(config: any): ModuleWithProviders {
        return {
            ngModule: SharedModule,
            providers: [
                config.apiConfig,
                _more providers_
            ]
        };
    }
}

app.module:

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

@NgModule({
  imports: [
    SharedModule.forRoot({
      apiConfig: {
        provide: ApiConfig,
        useFactory: LoadApiConfig
      }
    }),
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: (createTranslateLoader),
        deps: [HttpClient]
      },
      isolate: false
    })
  ],
  providers: [
  ],
  bootstrap: [AppComponent]
})

Please tell us about your environment:

jo-russ commented 3 years ago

@tommueller I know it's been a while but have you managed to make it work? I'm kinda in the same boat ATM and can't find any working solution anywhere.

tommueller commented 3 years ago

@jo-russ Nope, sorry. We actually migrated to transloco in the meantime.