vue-vben-admin/src/hooks/web/useI18n.ts

34 lines
694 B
TypeScript
Raw Normal View History

2020-11-25 23:20:30 +08:00
import { getI18n } from '/@/setup/i18n';
2020-11-26 21:10:21 +08:00
import projectSetting from '/@/settings/projectSetting';
2020-11-25 23:20:30 +08:00
export function useI18n(namespace?: string) {
function getKey(key: string) {
if (!namespace) {
return key;
}
if (key.startsWith(namespace)) {
return key;
}
return `${namespace}.${key}`;
}
2020-11-26 21:10:21 +08:00
const normalFn = {
t: (key: string) => {
return getKey(key);
},
};
if (!projectSetting.locale.show || !getI18n()) {
return normalFn;
}
const { t, ...methods } = getI18n().global;
2020-11-25 23:20:30 +08:00
return {
...methods,
t: (key: string, ...arg: Parameters<typeof t>) => {
if (!key) return '';
2020-11-25 23:20:30 +08:00
return t(getKey(key), ...arg);
},
};
}