ngx-translate - merge translation file of different modules
Asked Answered
S

0

12

I use angular-cli in my project with @ngx-translate. The application has lazy-loaded modules and one shared-module (not lazy-loaded) with components. The language files are separated per modules. The problem is when I use a shared component in a lazy loaded component, the shared component get strings from the translation file of the lazy loaded module (feature.xx.json), and not from app module (core.xx.json). So I should declare the strings of shared components in translation files of every feature modules.

I would like that, the shared module use always the translation file of the app module (core.xx.json) or an own.

app.module.ts

    export function createTranslateLoader(http: HttpClient) {
        return new TranslateHttpLoader(http, './assets/i18n/core.','.json'); 
    }
    ...
    imports [ 
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            },
            isolate: false
        }),
        ...
    ]

shared.module.ts

    imports: [
        TranslateModule,
        ...]

feature modules

    export function createTranslateLoader(http: HttpClient) {
        return new TranslateHttpLoader(http, './assets/i18n/feature.', '.json');
    }
    ...
    imports: [
        ...
        SharedModule,
        TranslateModule.forChild({
            loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            },
        isolate: true
        })] 
Solanum answered 6/3, 2018 at 9:39 Comment(1)
Check the approach described in this Github comment: github.com/ngx-translate/core/issues/425#issuecomment-349994074Disunite

© 2022 - 2024 — McMap. All rights reserved.