How to use the i18n-js.autoDetectLocale function in i18n-js

To help you get started, we’ve selected a few i18n-js examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kabisa / maji / src / i18n.js View on Github external
import I18n from "i18n-js";

// allows nl-NL to fallback to nl if no nl-NL translation is defined
I18n.fallbacks = true;

I18n.autoDetectLocale = ({ defaultLocale }) => {
  const userLocale = navigator.userLanguage || navigator.language;
  I18n.defaultLocale = defaultLocale;

  if (userLocale in I18n.translations) {
    // exact locale is supported, use that
    return (I18n.locale = userLocale);
  }

  const language = userLocale.match(/^[a-z]{2}/i)[0];
  for (const locale in I18n.translations) {
    if (locale.match(/^[a-z]{2}/i)[0] === language) {
      // we've got a match on language, use that
      return (I18n.locale = locale);
    }
  }
};