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>) => {
|
|
|
|
|
return t(getKey(key), ...arg);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|