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 / 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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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')
 */
var getLocale = function getLocale(supportedLocales) {
  var cookieLocale = (0, _utils.normalizeLocale)(_cookie.default.get('locale'));
  var navigatorLocale = (0, _utils.normalizeLocale)((0, _utils.getNavigatorLocale)());
  var normalizedSuppportedLocales = supportedLocales.map(function (locale) {
    return (0, _utils.normalizeLocale)(locale);
  });
  var desiredLocales = [cookieLocale, (0, _utils.getLanguageCodeFromLocale)(cookieLocale), navigatorLocale, (0, _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)
 */


exports.getLocale = getLocale;

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

function _initI18n() {
  _initI18n = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var _ref,
        _ref$supportedLocales,
        supportedLocales,
        loadLocaleFiles,
        locale,
        imports,
        normalizedLocale,
        _normalizedLocale$spl,
        _normalizedLocale$spl2,
        language,
        _yield$Promise$all,
        _yield$Promise$all2,
        data,
        _data,
        reactLocaleData,
        messages,
        localeData,
        _args = arguments;

    return _regenerator.default.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 ? Object.keys(_locales.default) : _ref$supportedLocales, loadLocaleFiles = _ref.loadLocaleFiles;
            _context.prev = 1;
            locale = getLocale(supportedLocales);
            imports = [(0, _import.importIntl)()];

            if (locale !== 'en') {
              normalizedLocale = (0, _utils.normalizeLocale)(locale);
              _normalizedLocale$spl = normalizedLocale.split('-'), _normalizedLocale$spl2 = (0, _slicedToArray2.default)(_normalizedLocale$spl, 1), language = _normalizedLocale$spl2[0];
              imports = imports.concat(loadLocaleFiles(normalizedLocale, language));
            }

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

          case 7:
            _yield$Promise$all = _context.sent;
            _yield$Promise$all2 = (0, _slicedToArray2.default)(_yield$Promise$all, 2);
            data = _yield$Promise$all2[1];
            _data = (0, _slicedToArray2.default)(data, 2), reactLocaleData = _data[0], messages = _data[1];
            localeData = (0, _import.extractIntlLocaleData)(reactLocaleData, locale);
            return _context.abrupt("return", {
              locale: locale,
              localeData: localeData,
              messages: messages
            });

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

          case 18:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 15]]);
  }));
  return _initI18n.apply(this, arguments);
}
//# sourceMappingURL=init.js.map