简体   繁体   English

角度翻译文件(ngx-translate)加载两次

[英]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. 我注意到,在浏览器的开发人员工具的“网络”选项卡中,我的Angular应用翻译文件已加载两次。

What is wrong? 怎么了? Should this be happening? 这应该发生吗?

网络标签

This might happen if the language you use and the default language you set with ng2-translate's TranslateService , are the same. 如果您使用的语言与ng2-translate的TranslateService设置的默认语言相同,则可能会发生这种情况。

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 . 这是在ngx-translate问题中进一步讨论的。

Note: you should probably run this logic in a service which watches when the user language is changed. 注意:您可能应该在监视用户语言更改的服务中运行此逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM