react-i18nify
Version:
Simple i18n translation and localization components and helpers for React.
53 lines (52 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.replace = exports.fetchTranslation = void 0;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const replace = (translation, replacements) => {
if (typeof translation === 'string') {
let result = translation;
Object.keys(replacements).forEach(replacement => {
result = result.split(`%{${replacement}}`).join(replacements[replacement] ?? '');
});
return result;
}
if (/*#__PURE__*/_react.default.isValidElement(translation)) {
return translation;
}
if (typeof translation === 'object') {
const result = {};
Object.keys(translation).forEach(translationKey => {
result[translationKey] = replace(translation[translationKey], replacements);
});
return result;
}
return null;
};
exports.replace = replace;
const fetchTranslation = (translations, key, count = null) => {
const _index = key.indexOf('.');
if (typeof translations === 'undefined') {
throw new Error('not found');
}
if (_index > -1) {
return fetchTranslation(translations[key.substring(0, _index)], key.substr(_index + 1), count);
}
if (count !== null) {
if (translations[`${key}_${count}`]) {
// when key = 'items_3' if count is 3
return translations[`${key}_${count}`];
}
if (count !== 1 && translations[`${key}_plural`]) {
// when count is not simply singular, return _plural
return translations[`${key}_plural`];
}
}
if (translations[key] != null) {
return translations[key];
}
throw new Error('not found');
};
exports.fetchTranslation = fetchTranslation;