useI18n.ts 667 字节
Newer Older
V
vben 已提交
1
import { getI18n } from '/@/setup/i18n';
V
vben 已提交
2
import projectSetting from '/@/settings/projectSetting';
V
vben 已提交
3 4 5 6 7 8 9 10 11 12 13

export function useI18n(namespace?: string) {
  function getKey(key: string) {
    if (!namespace) {
      return key;
    }
    if (key.startsWith(namespace)) {
      return key;
    }
    return `${namespace}.${key}`;
  }
V
vben 已提交
14 15 16 17 18 19 20 21 22 23 24 25
  const normalFn = {
    t: (key: string) => {
      return getKey(key);
    },
  };

  if (!projectSetting.locale.show || !getI18n()) {
    return normalFn;
  }

  const { t, ...methods } = getI18n().global;

V
vben 已提交
26 27 28 29 30 31 32
  return {
    ...methods,
    t: (key: string, ...arg: Parameters<typeof t>) => {
      return t(getKey(key), ...arg);
    },
  };
}