2020-11-25 23:20:30 +08:00
|
|
|
|
import { getI18n } from '/@/setup/i18n';
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-12-23 21:16:27 +08:00
|
|
|
|
if (!getI18n()) {
|
2020-11-26 21:10:21 +08:00
|
|
|
|
return normalFn;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const { t, ...methods } = getI18n().global;
|
|
|
|
|
|
|
2020-11-25 23:20:30 +08:00
|
|
|
|
return {
|
|
|
|
|
|
...methods,
|
2020-12-15 00:13:23 +08:00
|
|
|
|
t: (key: string, ...arg: any) => {
|
2020-12-01 20:59:17 +08:00
|
|
|
|
if (!key) return '';
|
2020-12-13 21:17:37 +08:00
|
|
|
|
return t(getKey(key), ...(arg as Parameters<typeof t>));
|
2020-11-25 23:20:30 +08:00
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2020-12-09 00:12:44 +08:00
|
|
|
|
|
|
|
|
|
|
// Why write this function?
|
|
|
|
|
|
// Mainly to configure the vscode i18nn ally plugin. This function is only used for routing and menus. Please use useI18n for other places
|
|
|
|
|
|
|
|
|
|
|
|
// 为什么要编写此函数?
|
2020-12-21 23:38:16 +08:00
|
|
|
|
// 主要用于配合vscode i18nn ally插件。此功能仅用于路由和菜单。请在其他地方使用useI18n
|
2020-12-09 00:12:44 +08:00
|
|
|
|
export const t = (key: string) => key;
|