useI18n.ts 694 B

123456789101112131415161718192021222324252627282930313233
  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: Parameters<typeof t>) => {
  25. if (!key) return '';
  26. return t(getKey(key), ...arg);
  27. },
  28. };
  29. }