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

40 lines
974 B
TypeScript
Raw Normal View History

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) => {
if (!key) return '';
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;