Rui Marques
Rui Marques

Reputation: 8914

Angular translation file (ngx-translate) gets loaded twice

I have noticed, in the browser's developer tools network tab, that my Angular app translation file is being loaded twice.

What is wrong? Should this be happening?

network tab

Upvotes: 1

Views: 1447

Answers (1)

Rui Marques
Rui Marques

Reputation: 8914

This might happen if the language you use and the default language you set with ng2-translate's TranslateService, are the same.

Wrong:

  constructor(translate: TranslateService) {
    const DEFAULT_LANG = 'en';
    const userLang = translate.getBrowserLang();

    translate.setDefaultLang(DEFAULT_LANG);
    translate.use(userLang);
  }

Proposed solution:

  constructor(translate: TranslateService) {
    const DEFAULT_LANG = 'en';
    const userLang = translate.getBrowserLang();

    if (userLang !== DEFAULT_LANG) {
      translate.setDefaultLang(DEFAULT_LANG);
    }

    translate.use(userLang);
  }

This is further discussed in this ngx-translate issue.

Note: you should probably run this logic in a service which watches when the user language is changed.

Upvotes: 3

Related Questions