react-i18next
Version:
Internationalization for react done right. Using the i18next i18n ecosystem.
51 lines • 1.88 kB
JavaScript
import React from 'react';
import { warn, warnOnce, isString, getFormatPlugin, escapeStringValues } from './utils.js';
import { getI18n } from './i18nInstance.js';
import { renderTranslation } from './IcuTransUtils/index.js';
export function IcuTransWithoutContext({
i18nKey,
defaultTranslation,
content,
ns,
values = {},
i18n: i18nFromProps,
t: tFromProps
}) {
const i18n = i18nFromProps || getI18n();
if (!i18n) {
warnOnce(i18n, 'NO_I18NEXT_INSTANCE', `IcuTrans: You need to pass in an i18next instance using i18nextReactModule`, {
i18nKey
});
return React.createElement(React.Fragment, {}, defaultTranslation);
}
const t = tFromProps || i18n.t?.bind(i18n) || (k => k);
let namespaces = ns || t.ns || i18n.options?.defaultNS;
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
const mergedValues = {
...i18n.options?.interpolation?.defaultVariables,
...values
};
const formatPlugin = getFormatPlugin(i18n);
const escapeValues = formatPlugin ? !formatPlugin.options?.escapeVariables : i18n.options?.interpolation?.escapeValue === false;
const translation = t(i18nKey || defaultTranslation, {
defaultValue: defaultTranslation,
...(escapeValues && formatPlugin ? escapeStringValues(mergedValues) : mergedValues),
...(escapeValues && !formatPlugin && {
interpolation: {
escapeValue: true
}
}),
ns: namespaces
});
try {
const rendered = renderTranslation(translation, content);
return React.createElement(React.Fragment, {}, ...rendered);
} catch (error) {
warn(i18n, 'ICU_TRANS_RENDER_ERROR', `IcuTrans component error for key "${i18nKey}": ${error.message}`, {
i18nKey,
error
});
return React.createElement(React.Fragment, {}, translation);
}
}
IcuTransWithoutContext.displayName = 'IcuTransWithoutContext';