UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

63 lines (62 loc) 2.65 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.mergeTranslations = mergeTranslations; var _react = _interopRequireWildcard(require("react")); var _useTranslation = require("./useTranslation.js"); var _Context = _interopRequireDefault(require("./Context.js")); var _renderWithFormatting = _interopRequireDefault(require("./renderWithFormatting.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const TranslationImpl = ({ id, children, ...params }) => { const { translation } = (0, _react.useContext)(_Context.default); const result = (0, _useTranslation.formatMessage)(id || children, params, translation); if (typeof result !== 'string') { return _react.default.createElement(_react.default.Fragment, null, String(id)); } return _react.default.createElement(_react.default.Fragment, null, (0, _renderWithFormatting.default)(result)); }; const Translation = TranslationImpl; Translation.withTypes = function withTypes() { return function TypedTranslation(props) { return _react.default.createElement(TranslationImpl, props); }; }; var _default = exports.default = Translation; function isPlainObject(value) { if (value === null || typeof value !== 'object') { return false; } if (Array.isArray(value)) { return false; } return Object.getPrototypeOf(value) === Object.prototype; } function mergeTranslations(...translations) { return translations.reduce((acc, cur) => { Object.keys(cur).forEach(key => { const accValue = acc[key]; const curValue = cur[key]; if (Array.isArray(accValue) && Array.isArray(curValue)) { acc[key] = [...accValue, ...curValue]; return; } if (isPlainObject(accValue) && isPlainObject(curValue)) { acc[key] = mergeTranslations(accValue, curValue); return; } acc[key] = curValue; }); return acc; }, {}); } //# sourceMappingURL=Translation.js.map