UNPKG

@cookbook/solid-intl

Version:

A universal internationalization (i18n) for Solid inspired by React Intl & FormatJS

158 lines (148 loc) 5.09 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var web = require('solid-js/web'); var solidJs = require('solid-js'); var store = require('solid-js/store'); var intl = require('@formatjs/intl'); function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function processConfig(config) { return solidJs.mergeProps(intl.DEFAULT_INTL_CONFIG, { locale: config.locale, timeZone: config.timeZone, fallbackOnEmptyString: config.fallbackOnEmptyString, formats: config.formats, messages: config.messages, defaultLocale: config.defaultLocale, defaultFormats: config.defaultFormats, defaultRichTextElements: config.defaultRichTextElements, onError: config.onError, onWarn: config.onWarn }); } var typeOf = function typeOf(value) { var _toString$call$match$, _toString$call$match; return (_toString$call$match$ = (_toString$call$match = {}.toString.call(value).match(/\s([A-Za-z]+)/)) === null || _toString$call$match === void 0 ? void 0 : _toString$call$match[1].toLowerCase()) !== null && _toString$call$match$ !== void 0 ? _toString$call$match$ : ""; }; var typeOf$1 = typeOf; var is = { // @ts-ignore nullish: function nullish(value) { return value === null || value === undefined; }, // @ts-ignore string: function string(value) { return typeOf$1(value) === "string"; }, // @ts-ignore number: function number(value) { return typeOf$1(value) === "number"; }, // @ts-ignore bool: function bool(value) { return typeOf$1(value) === "boolean"; }, // @ts-ignore object: function object(value) { return typeOf$1(value) === "object"; }, // @ts-ignore array: function array(value) { return Array.isArray(value); }, // @ts-ignore date: function date(value) { return typeOf$1(value) === "date"; }, // @ts-ignore "function": function _function(value) { return typeOf$1(value) === "function"; } }; var is$1 = is; var IntlContext = solidJs.createContext(); var IntlProvider = function IntlProvider(props) { if (is$1.nullish(props.locale)) { throw new ReferenceError('[solid-intl]: <IntlProvider /> expects a "locale" which was not configured. See https://formatjs.io/docs/react-intl/api#intlshape for more details'); } var cache = store.createMutable(intl.createIntlCache()); var _createStore = store.createStore(intl.createIntl(processConfig(props), cache)), _createStore2 = _slicedToArray(_createStore, 2), intl$1 = _createStore2[0], setIntl = _createStore2[1]; solidJs.createComputed(function () { setIntl(intl.createIntl(processConfig(props), cache)); }); return web.createComponent(IntlContext.Provider, { value: intl$1, get children() { return props.children; } }); }; var IntlProvider$1 = IntlProvider; var useIntl = function useIntl() { var intl = solidJs.useContext(IntlContext); if (!intl) { throw new ReferenceError("[solid-intl]: Have you forgot to wrap your application with <IntlProvider /> ?"); } return intl; }; var useIntl$1 = useIntl; Object.defineProperty(exports, 'defineMessage', { enumerable: true, get: function () { return intl.defineMessage; } }); Object.defineProperty(exports, 'defineMessages', { enumerable: true, get: function () { return intl.defineMessages; } }); exports.IntlProvider = IntlProvider$1; exports.useIntl = useIntl$1;