UNPKG

@enact/i18n

Version:

Internationalization support for Enact using iLib

124 lines (118 loc) 6.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isNonLatinLocale = isNonLatinLocale; exports.isRtlLocale = isRtlLocale; exports.updateLocale = void 0; var _ilib = _interopRequireDefault(require("ilib")); var _LocaleInfo = _interopRequireDefault(require("ilib/lib/LocaleInfo")); var _ScriptInfo = _interopRequireDefault(require("ilib/lib/ScriptInfo")); var _case = require("../src/case"); var _excluded = ["onLoad", "latinLanguageOverrides", "nonLatinLanguageOverrides"]; /** * Exports the {@link i18n/locale.isNonLatinLocale}, {@link i18n/locale.isRtlLocale} and * {@link i18n/locale.updateLocale} functions. * * @module i18n/locale * @private */ function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } // Returns `true` if a locale list is provided and it includes either the language (the first part // of the spec e.g. ko) or the entire spec (e.g. ko-KR) var includesLocale = function includesLocale(localeList, locale) { return Array.isArray(localeList) && (localeList.includes(locale.getLanguage()) || localeList.includes(locale.toString())); }; /** * Tell whether or not the given locale is considered a non-Latin locale for webOS purposes. This * controls which fonts are used in various places to show the various languages. An undefined spec * parameter means to test the current locale. * * @memberof i18n/locale * @param {ilib.Locale|string|undefined} spec locale specifier or locale object of the locale to * test, or undefined to test the current locale * @param {Object} [options] An object configuring the request. Must include an `onLoad` member to * receive the result. */ function isNonLatinLocale(spec) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _onLoad = options.onLoad, latinLanguageOverrides = options.latinLanguageOverrides, nonLatinLanguageOverrides = options.nonLatinLanguageOverrides, rest = _objectWithoutProperties(options, _excluded); if (!_onLoad) return; // eslint-disable-next-line no-new new _LocaleInfo["default"](spec, _objectSpread(_objectSpread({}, rest), {}, { onLoad: function onLoad(li) { var locale = li.getLocale(); _onLoad( // the language actually is non-latin and should not be treated as latin li.getScript() !== 'Latn' && !includesLocale(latinLanguageOverrides, locale) || // the language is latin but should be treated as non-latin includesLocale(nonLatinLanguageOverrides, locale)); } })); } /** * Determines if current locale is a right-to-left locale. * * @memberof i18n/locale * @param {Object} [options] An object configuring the request. Must include an `onLoad` member to * receive the result. */ function isRtlLocale() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _onLoad2 = options.onLoad, sync = options.sync; if (!_onLoad2) return; // eslint-disable-next-line no-new new _LocaleInfo["default"](_ilib["default"].getLocale(), _objectSpread(_objectSpread({}, options), {}, { onLoad: function onLoad(li) { var scriptName = li.getScript(); // eslint-disable-next-line no-new new _ScriptInfo["default"](scriptName, { sync: sync, onLoad: function onLoad(script) { _onLoad2(script.getScriptDirection() === 'rtl'); } }); } })); } /** * This Enact hook lets us know that the system locale has changed and gives * us a chance to update the iLib locale before Enact broadcasts its * `onlocalechange` signal. * Provide a locale string, like 'en-US' or 'ja-JP', to conveniently set * that locale immediately. Provide nothing, and reset the locale back to the * browser's default language. * * @memberof i18n/locale * @param {String} locale Locale identifier * @returns {undefined} */ var updateLocale = exports.updateLocale = function updateLocale(locale) { // blow away the cache to force it to reload the manifest files for the new app // eslint-disable-next-line no-undefined if (_ilib["default"]._load) _ilib["default"]._load.manifest = undefined; // remove the cache of the platform name to allow transition between snapshot and browser delete _ilib["default"]._platform; // load any external ilib/resbundle data _ilib["default"].data = global.ilibData || _ilib["default"].data; _ilib["default"].data.cache = _ilib["default"].data.cache || {}; _ilib["default"].data.cache['ResBundle-strings'] = global.resBundleData || {}; // ilib handles falsy values and automatically uses local locale when encountered which // is expected and desired _ilib["default"].setLocale(locale); var newLocale = _ilib["default"].getLocale(); // Recreate the case mappers to use the just-recently-set locale (0, _case.initCaseMappers)(); return newLocale; };