@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
76 lines (75 loc) • 2.29 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.mergeTranslations = mergeTranslations;
var _react = require("react");
var _useTranslation = require("./useTranslation.js");
var _Context = _interopRequireDefault(require("./Context.js"));
var _renderWithFormatting = _interopRequireDefault(require("./renderWithFormatting.js"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const TranslationImpl = ({
id,
children,
...params
}) => {
const {
translation,
locale,
messageFormatter
} = (0, _react.useContext)(_Context.default);
const result = (0, _useTranslation.formatMessage)(id || children, params, translation, locale, messageFormatter);
if ((0, _react.isValidElement)(result) || Array.isArray(result)) {
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
children: result
});
}
if (typeof result !== 'string') {
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
children: String(id)
});
}
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
children: (0, _renderWithFormatting.default)(result)
});
};
const Translation = TranslationImpl;
Translation.withTypes = function withTypes() {
return function TypedTranslation(props) {
return (0, _jsxRuntime.jsx)(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