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 / cjs / src / init.js
Size: Mime:
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
var cookie = require('./cookie.js');
var utils = require('./utils.js');
var _import = require('./import.js');

/**
 * 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')
 */

var getLocale = function getLocale(supportedLocales) {
  var cookieLocale = utils.normalizeLocale(cookie.get('locale'));
  var navigatorLocale = utils.normalizeLocale(utils.getNavigatorLocale());
  var normalizedSuppportedLocales = supportedLocales.map(function (locale) {
    return utils.normalizeLocale(locale);
  });
  var desiredLocales = [cookieLocale, utils.getLanguageCodeFromLocale(cookieLocale), navigatorLocale, utils.getLanguageCodeFromLocale(navigatorLocale), 'en'];
  var matchedLocale = desiredLocales.reduce(function (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)
 */

function initI18n() {
  return _initI18n.apply(this, arguments);
}

function _initI18n() {
  _initI18n = _rollupPluginBabelHelpers.asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var _ref,
        _ref$supportedLocales,
        supportedLocales,
        locale,
        imports,
        _yield$Promise$all,
        _yield$Promise$all2,
        localeData,
        messages,
        _args = arguments;

    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _ref = _args.length > 0 && _args[0] !== undefined ? _args[0] : {}, _ref$supportedLocales = _ref.supportedLocales, supportedLocales = _ref$supportedLocales === void 0 ? ['en'] : _ref$supportedLocales;
            _context.prev = 1;
            locale = getLocale(supportedLocales);
            imports = [_import.importIntl()];

            if (locale !== 'en') {
              imports.push(_import.importLocaleData(locale), _import.importMessages(locale));
            }

            _context.next = 7;
            return Promise.all(imports);

          case 7:
            _yield$Promise$all = _context.sent;
            _yield$Promise$all2 = _rollupPluginBabelHelpers.slicedToArray(_yield$Promise$all, 3);
            localeData = _yield$Promise$all2[1];
            messages = _yield$Promise$all2[2];
            return _context.abrupt("return", {
              locale: locale,
              localeData: localeData,
              messages: messages
            });

          case 14:
            _context.prev = 14;
            _context.t0 = _context["catch"](1);
            return _context.abrupt("return", {
              locale: 'en'
            });

          case 17:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 14]]);
  }));
  return _initI18n.apply(this, arguments);
}

exports.default = initI18n;
exports.getLocale = getLocale;
//# sourceMappingURL=init.js.map