Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/i18n / dist / init.js
Size: Mime:
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = initI18n;
exports.getLocale = void 0;

var _import = require("./import");

var _cookie = _interopRequireDefault(require("./cookie"));

var _locales = _interopRequireDefault(require("./locales"));

var _utils = require("./utils");

/**
 * Get a locale for the user that is supported
 *
 * It considers normalized locales from
 * - the cookie (first full locale, then extracted language)
 * - the navigator locale (first full, then extracted language)
 * - default: 'en'
 *
 * and returns the first match in the normalized supported locales
 *
 * @param {string[]} supportedLocales
 * @returns {string} normalized locale which is supported (or 'en')
 */
const getLocale = supportedLocales => {
  const cookieLocale = (0, _utils.normalizeLocale)(_cookie.default.get('locale'));
  const navigatorLocale = (0, _utils.normalizeLocale)((0, _utils.getNavigatorLocale)());
  const normalizedSuppportedLocales = supportedLocales.map(locale => (0, _utils.normalizeLocale)(locale));
  const desiredLocales = [cookieLocale, (0, _utils.getLanguageCodeFromLocale)(cookieLocale), navigatorLocale, (0, _utils.getLanguageCodeFromLocale)(navigatorLocale), 'en'];
  const matchedLocale = desiredLocales.reduce((result, desiredLocale) => {
    if (!result && normalizedSuppportedLocales.includes(desiredLocale)) {
      return desiredLocale;
    }

    return result;
  }, false);
  return matchedLocale || 'en';
};
/**
 * Initialise I18n asynchronously
 * - determine locale
 * - load Intl polyfill (only executed when window.Intl is not present)
 * - load `react-intl` locale data if not english locale
 * - load translation messages if not english
 *
 * It returns a promise that will always resolve: errors are caught internally, and a default language is returned
 *
 * @async
 * @returns {Promise<object>} Resolves to an object with locale, locale data (optional) and translation messages (optional)
 */


exports.getLocale = getLocale;

async function initI18n({
  supportedLocales = Object.keys(_locales.default),
  loadLocaleFiles
} = {}) {
  try {
    const locale = getLocale(supportedLocales);
    let imports = [(0, _import.importIntl)()];

    if (locale !== 'en') {
      imports = imports.concat(loadLocaleFiles(locale));
    }

    const [, data] = await Promise.all(imports);
    const [reactLocaleData, messages] = data;
    const localeData = (0, _import.extractIntlLocaleData)(reactLocaleData, locale);
    return {
      locale,
      localeData,
      messages
    };
  } catch (error) {
    return {
      locale: 'en'
    };
  }
}
//# sourceMappingURL=init.js.map