@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
38 lines (37 loc) • 1.29 kB
JavaScript
"use client";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["id", "children"];
import React, { useContext } from 'react';
import { formatMessage } from './useTranslation';
import SharedContext from './Context';
export default function Translation(_ref) {
let {
id,
children
} = _ref,
params = _objectWithoutProperties(_ref, _excluded);
const {
translation
} = useContext(SharedContext);
const result = formatMessage(id || children, params, translation);
if (typeof result !== 'string') {
return React.createElement(React.Fragment, null, String(id));
}
return React.createElement(React.Fragment, null, result);
}
export function mergeTranslations() {
for (var _len = arguments.length, translations = new Array(_len), _key = 0; _key < _len; _key++) {
translations[_key] = arguments[_key];
}
return translations.reduce((acc, cur) => {
Object.keys(cur).forEach(key => {
if (acc[key] !== null && cur[key] !== null && typeof acc[key] === 'object' && typeof cur[key] === 'object') {
acc[key] = mergeTranslations(acc[key], cur[key]);
} else {
acc[key] = cur[key];
}
});
return acc;
}, {});
}
//# sourceMappingURL=Translation.js.map