@rcp/use.i18n
Version:
A react hook for using i18n
46 lines • 1.42 kB
JavaScript
;
/**
* @file index.ts
* @author imcuttle
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
var React = require("react");
var tiny_i18n_1 = require("tiny-i18n");
/**
* @public
* @param presetDict {{}}
* @param options
* @param [options.language] {string}
* @param [options.locale] {{}}
* @example
* useI18n({
* zh: { name: '姓名' },
* en: { name: 'Name' },
* }, { language: 'zh' })
* @example
* useI18n({
* zh: { name: '姓名' },
* en: { name: 'Name' },
* }, { locale: {name: 'Customized Name'} })
*/
function useI18nCore(presetDict, options) {
if (options === void 0) { options = {}; }
var language = options.language, _a = options.locale, locale = _a === void 0 ? {} : _a, tinyI18n = options.tinyI18n;
var i18n = React.useMemo(function () {
var i18n = tinyI18n || (0, tiny_i18n_1.createIsolateI18n)();
for (var _i = 0, _a = Object.entries(presetDict); _i < _a.length; _i++) {
var _b = _a[_i], lang = _b[0], dict = _b[1];
i18n.extendDictionary(dict, lang);
}
return i18n;
}, [tinyI18n]);
// 先设置语言
React.useMemo(function () {
language && i18n.setLanguage(language);
locale && Object.keys(locale).length && i18n.extendDictionary(locale);
}, [language, locale, i18n]);
return i18n;
}
exports.default = useI18nCore;
//# sourceMappingURL=index.js.map