@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
77 lines (76 loc) • 4.39 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.destructFlatTranslation = destructFlatTranslation;
exports.prepareContext = prepareContext;
require("core-js/modules/es.string.replace.js");
var _react = require("react");
var _defaults = require("./defaults");
var _locales = _interopRequireDefault(require("./locales"));
var _componentHelper = require("./component-helper");
var _jsonPointer = _interopRequireDefault(require("../extensions/forms/utils/json-pointer"));
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 prepareContext() {
var _props;
let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if ((_props = props) !== null && _props !== void 0 && _props.__context__) {
props = Object.assign({}, props, props.__context__);
delete props.__context__;
}
const translations = props.translations || props.locales ? (0, _componentHelper.extendDeep)({}, _locales.default, props.translations || props.locales) : (0, _componentHelper.extendDeep)({}, _locales.default);
const localeWithFallback = handleLocaleFallbacks(props.locale || _defaults.LOCALE, translations);
const translation = destructFlatTranslation((0, _componentHelper.extendDeep)({}, _locales.default[_defaults.LOCALE], translations[localeWithFallback]));
const context = _objectSpread(_objectSpread({}, props), {}, {
updateTranslation: (locale, newTranslations) => {
context.translation = newTranslations[locale] || newTranslations[_defaults.LOCALE];
context.translation = destructFlatTranslation(context.translation);
context.translations = newTranslations;
if (context.locales) {
context.locales = context.translations;
}
},
getTranslation: localProps => {
if (localProps) {
const locale = localProps.lang || localProps.locale;
if (locale && (context.translations || context.locales)[locale] && locale !== localeWithFallback) {
return destructFlatTranslation((context.translations || context.locales)[locale]);
}
}
return context.translation || _locales.default[_defaults.LOCALE];
},
locales: translations,
translations,
translation
});
return _objectSpread({}, context);
}
function handleLocaleFallbacks(locale) {
let translations = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (locale === 'en' || String(locale).split('-')[0] === 'en') {
return 'en-GB';
}
return translations[locale] ? locale : _defaults.LOCALE;
}
const Context = (0, _react.createContext)(prepareContext({
locale: _defaults.LOCALE,
currency: _defaults.CURRENCY,
currency_display: _defaults.CURRENCY_DISPLAY
}));
var _default = exports.default = Context;
function destructFlatTranslation(source) {
for (const k in source) {
if (String(k).includes('.')) {
_jsonPointer.default.set(source, '/' + k.replace(/\./g, '/'), source[k]);
}
}
return source;
}
//# sourceMappingURL=Context.js.map