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.

Lazy loaded module is not adding the new custom translateLoader

See original GitHub issue

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

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

Current behavior When a lazy loaded module is being loaded it triggers the factory for create the new TranslateHttpLoader however it is not adding the translations in the file

Expected/desired behavior Load the translations of the lazy loaded module

Reproduction of the problem I can’t provide a plnkr because but this is the code that i’m using

export function  translateFactory(http: Http) {
        return new TranslateHttpLoader(http, '/metro-apps/maintenances/i18n/', '.json');
}
[...]
TranslateModule.forChild({
    loader: {
        provide: TranslateLoader,
        useFactory: translateFactory,
        deps: [Http]
    }
})

Please tell us about your environment: Kubuntu 16.04

  • ngx-translate version: core=6.0.0 loader: 0.0.3

  • Angular version: 2.4.7

  • Browser: all

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:26 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
k-schneidercommented, Feb 26, 2017

Just bumped into this as well.

I wanted my app module to fetch translations from “/assets/i18n/{lang}.json” and my lazy loaded modules to fetch from “/assets/i18n/{feature}/{lang}.json” but was unable to get this to work.

The custom loader configured by forChild(…) was ignored.

4reactions
sebelgacommented, Jun 15, 2017

I never managed to do it. So I created my own service to add translations to the root translateModule. I have a resolver for the routes accessing the lazyloaded module calling loadTranslationModule() before resolving.

@Injectable()
export class LocalizationService {
    private modulesTranslation: any = {};

    constructor(private http: Http, private translate: TranslateService) { }

    loadTranslationModule(module: string): Observable<any> {
        const lang = this.translate.currentLang || 'en';

        if (this.modulesTranslation[module] && this.modulesTranslation[module][lang]) {
            return Observable.of(this.modulesTranslation[module][lang]);
        }

        const uri = `assets/${module}/i18n/${lang}.json`;
        return this.http.get(uri)
            .map(res => res.json())
            .do((i18n: any) => {
                if (!this.modulesTranslation[module]) {
                    this.modulesTranslation[module] = {};
                }
                this.modulesTranslation[module][lang] = i18n;
                this.translate.setTranslation(lang, i18n, true); // add translation to global translations
                console.log(`[Localization] lang (${lang}) loaded for module (${module})...`);
            })
            .catch((err: any) => {
                console.log(`[Localization] lang (${lang}) not found for module (${module})`);
                return Observable.of({});
            });
    }
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx-translate is not working for lazy loaded module
Configured the project as: Added TranslateModule.forRoot() in app.module.ts like. export function httpLoaderFactory ...
Read more >
Creating lazy-loaded modules - IBM
Lazy -loaded modules typically correspond to features. It is recommended that the features in these modules are added to a folder called features....
Read more >
@ngx-translate/core - npm
1. Import the TranslateModule : Finally, you can use ngx-translate in your Angular project. You have to import TranslateModule.forRoot() in the ...
Read more >
Working With Lazy Loading Modules and Preload Routing ...
When we build an Angular application with multiple modules in a large app, ... and add preload and custom strategies for loading modules....
Read more >
ngx-translate Documentation - CodeAndWeb
Since lazy loaded modules use a different injector from the rest of your ... You have to create the translate loader inside a...
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