useI18n.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { getI18n } from '/@/setup/i18n';
  2. import projectSetting from '/@/settings/projectSetting';
  3. export function useI18n(namespace?: string) {
  4. function getKey(key: string) {
  5. if (!namespace) {
  6. return key;
  7. }
  8. if (key.startsWith(namespace)) {
  9. return key;
  10. }
  11. return `${namespace}.${key}`;
  12. }
  13. const normalFn = {
  14. t: (key: string) => {
  15. return getKey(key);
  16. },
  17. };
  18. if (!projectSetting.locale.show || !getI18n()) {
  19. return normalFn;
  20. }
  21. const { t, ...methods } = getI18n().global;
  22. return {
  23. ...methods,
  24. t: (key: string, ...arg: any) => {
  25. if (!key) return '';
  26. return t(getKey(key), ...(arg as Parameters<typeof t>));
  27. },
  28. };
  29. }
  30. // Why write this function?
  31. // Mainly to configure the vscode i18nn ally plugin. This function is only used for routing and menus. Please use useI18n for other places
  32. // 为什么要编写此函数?
  33. // 主要用于配合vscode i18nn ally插件。此功能仅用于路由和菜单。请在其他地方使用useIs18n
  34. export const t = (key: string) => key;