@arco-vue-pro-components/pro-components
Version:
基于@arco-design/web-vue组件的高级组件,包括pro-table
68 lines (67 loc) • 1.79 kB
JavaScript
import { ref, reactive, inject, computed } from "vue";
import { configProviderInjectionKey } from "../_utils/context.js";
import { isString } from "../_utils/is.js";
import zhCN from "./lang/zh_CN.js";
import enUS from "./lang/en_US.js";
const LOCALE = ref("zh-CN");
const I18N_MESSAGES = reactive({
"zh-CN": zhCN,
"en-US": enUS
});
const useI18n = () => {
const configProvider = inject(configProviderInjectionKey, void 0);
const i18nMessage = computed(
() => {
var _a;
return (_a = configProvider == null ? void 0 : configProvider.locale) != null ? _a : I18N_MESSAGES[LOCALE.value];
}
);
const locale = computed(() => i18nMessage.value.locale);
const transform = (key, ...args) => {
const keyArray = key.split(".");
let temp = i18nMessage.value;
for (const keyItem of keyArray) {
if (!temp[keyItem]) {
return key;
}
temp = temp[keyItem];
}
if (isString(temp)) {
if (args.length > 0) {
return temp.replace(/{(\d+)}/g, (sub, index) => {
var _a;
return (_a = args[index]) != null ? _a : sub;
});
}
return temp;
}
return temp;
};
const getMessage = (key, defaultMsg, ...args) => {
const keyArray = key.split(".");
let temp = i18nMessage.value;
for (const keyItem of keyArray) {
if (!temp[keyItem]) {
return defaultMsg || key;
}
temp = temp[keyItem];
}
if (isString(temp)) {
if (args.length > 0) {
return temp.replace(/{(\d+)}/g, (sub, index) => {
var _a;
return (_a = args[index]) != null ? _a : sub;
});
}
return temp;
}
return temp;
};
return {
i18nMessage,
locale,
t: transform,
getMessage
};
};
export { useI18n };