UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

73 lines (72 loc) 3.34 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; 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; } import { createContext } from 'react'; import { LOCALE, CURRENCY, CURRENCY_DISPLAY } from './defaults'; import defaultLocales from './locales'; import { extendDeep } from './component-helper'; import pointer from '../extensions/forms/utils/json-pointer'; export function prepareContext(props = {}) { var _props; if ((_props = props) !== null && _props !== void 0 && _props.__context__) { props = Object.assign({}, props, props.__context__); delete props.__context__; } const translations = props.translations || props.locales ? extendDeep({}, defaultLocales, props.translations || props.locales) : extendDeep({}, defaultLocales); const localeWithFallback = handleLocaleFallbacks(props.locale || LOCALE, props.translations || props.locales); for (const locale in translations) { translations[locale] = destructFlatTranslation(translations[locale]); } const translation = translations[localeWithFallback] || defaultLocales[LOCALE] || {}; const context = _objectSpread(_objectSpread({}, props), {}, { updateTranslation: (locale, newTranslations) => { context.translation = newTranslations[locale] || newTranslations[LOCALE]; 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) { const tr = context.translations || context.locales; return tr[locale]; } } return context.translation || defaultLocales[LOCALE]; }, locales: translations, translations, translation }); return _objectSpread({}, context); } function handleLocaleFallbacks(locale, translations = {}) { if (locale === 'en' || String(locale).split('-')[0] === 'en') { return 'en-GB'; } return translations[locale] ? locale : LOCALE; } const Context = createContext(prepareContext({ locale: LOCALE, currency: CURRENCY, currency_display: CURRENCY_DISPLAY })); export default Context; export function destructFlatTranslation(source) { let hasFlatTr = false; const destructed = {}; for (const k in source) { if (String(k).includes('.')) { pointer.set(destructed, '/' + k.replace(/\./g, '/'), source[k]); hasFlatTr = true; } } if (hasFlatTr) { return extendDeep({}, source, destructed); } return source; } //# sourceMappingURL=Context.js.map