plus-pro-components
Version:
Page level components developed based on Element Plus.
32 lines (29 loc) • 1.06 kB
JavaScript
import { unref, computed, isRef, ref, inject } from 'vue';
import { get } from 'lodash-es';
import { localeContextKey } from 'element-plus';
import English from '../locale/lang/en.mjs';
const buildTranslator = (locale) => (path, option) => translate(path, option, unref(locale));
const translate = (path, option, locale) => get(locale, path, path).replace(
/\{(\w+)\}/g,
(_, key) => {
var _a;
return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
}
);
const buildLocaleContext = (locale) => {
const lang = computed(() => unref(locale).name);
const localeRef = isRef(locale) ? locale : ref(locale);
return {
lang,
locale: localeRef,
t: buildTranslator(locale)
};
};
const useLocale = (localeOverrides) => {
const locale = localeOverrides || inject(localeContextKey, ref());
return buildLocaleContext(computed(() => {
var _a;
return ((_a = locale.value) == null ? void 0 : _a.plus) ? locale.value : English;
}));
};
export { buildLocaleContext, buildTranslator, translate, useLocale };