d-render
Version:
基于 Element Plus 二次封装的数据驱动渲染组件库 - 表单、搜索表单、表格配置化渲染
34 lines (31 loc) • 987 B
JavaScript
import zhCn from './lang/zh-cn';
export { default as zhCn } from './lang/zh-cn';
export { default as en } from './lang/en';
const defaultLocale = zhCn;
const getByPath = (obj, path) => {
if (!obj || !path)
return void 0;
const keys = path.split(".");
let current = obj;
for (const key of keys) {
if (current == null || typeof current !== "object")
return void 0;
current = current[key];
}
return current;
};
const translate = (path, option, locale) => {
const value = getByPath(locale, path);
const text = typeof value === "string" ? value : path;
return text.replace(/\{(\w+)\}/g, (_, key) => {
var _a;
return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
});
};
const buildTranslator = (locale) => {
return (path, option) => {
const current = typeof locale === "function" ? locale() : locale;
return translate(path, option, current);
};
};
export { buildTranslator, defaultLocale, translate };