最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trouble with detecting browser language and using it with i18next - Stack Overflow

programmeradmin11浏览0评论

I'm working on a webapp. I'm using i18next and it doesn't detect language unless I specify "lng" on options. If I use "navigator.language || navigator.userLanguage" and it's OK with Chrome. It returns something like "en". But Firefox for example, returns "en-GB". I don't use regional translation and i18next doesn't redirect "en-GB" to "en". So, system doesn't work.

Here is my code:

$(document).ready(function(){
      i18next.use(i18nextXHRBackend);
      i18next.init({
          'debug': true,
          'lng': navigator.language || navigator.userLanguage,
          'fallbackLng': 'en',
          'load': 'currentOnly',
          'backend': {
            loadPath: 'locales/{{lng}}/{{ns}}.json'
          }
      }, function() {
          jqueryI18next.init(i18next, $);
          $('body').localize();
      });
    });

I'm working on a webapp. I'm using i18next and it doesn't detect language unless I specify "lng" on options. If I use "navigator.language || navigator.userLanguage" and it's OK with Chrome. It returns something like "en". But Firefox for example, returns "en-GB". I don't use regional translation and i18next doesn't redirect "en-GB" to "en". So, system doesn't work.

Here is my code:

$(document).ready(function(){
      i18next.use(i18nextXHRBackend);
      i18next.init({
          'debug': true,
          'lng': navigator.language || navigator.userLanguage,
          'fallbackLng': 'en',
          'load': 'currentOnly',
          'backend': {
            loadPath: 'locales/{{lng}}/{{ns}}.json'
          }
      }, function() {
          jqueryI18next.init(i18next, $);
          $('body').localize();
      });
    });

Share Improve this question asked Jul 22, 2016 at 17:07 mutlucan96mutlucan96 4271 gold badge5 silver badges12 bronze badges 2
  • 1 It's probably not a very well supported feature … Would navigator.language.substr(0, 2) suffice? That way 'en-GB' is turned into 'en' … This really depends on the list of values that i18next accepts for 'lng'. – Aurel Bílý Commented Jul 22, 2016 at 17:14
  • @AurelBílý It works with that. Thanks. So, nothing wrong with my code, is it? – mutlucan96 Commented Jul 22, 2016 at 18:50
Add a ment  | 

1 Answer 1

Reset to default 5

i18next supports region languages...that's what localization is about.

if you like to support only languages without region set load: 'languageOnly':

$(document).ready(function(){
  i18next.use(i18nextXHRBackend);
  i18next.init({
      'debug': true,
      'load': 'languageOnly',
      'lng': navigator.language || navigator.userLanguage,
      'fallbackLng': 'en',
      'load': 'currentOnly',
      'backend': {
        loadPath: 'locales/{{lng}}/{{ns}}.json'
      }
  }, function() {
      jqueryI18next.init(i18next, $);
      $('body').localize();
  });
});

this will avoid accepting en-GB for lookups and only use en. Without setting this i18next will still output same result but also try to load en-GB translations.

发布评论

评论列表(0)

  1. 暂无评论