@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
35 lines (34 loc) • 1.16 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(...translations) {
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