useI18n.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // import { i18n } from '/@/locales/setupI18n';
  2. // type I18nGlobalTranslation = {
  3. // (key: string): string;
  4. // (key: string, locale: string): string;
  5. // (key: string, locale: string, list: unknown[]): string;
  6. // (key: string, locale: string, named: Record<string, unknown>): string;
  7. // (key: string, list: unknown[]): string;
  8. // (key: string, named: Record<string, unknown>): string;
  9. // };
  10. // type I18nTranslationRestParameters = [string, any];
  11. // function getKey(namespace: string | undefined, key: string) {
  12. // if (!namespace) {
  13. // return key;
  14. // }
  15. // if (key.startsWith(namespace)) {
  16. // return key;
  17. // }
  18. // return `${namespace}.${key}`;
  19. // }
  20. // export function useI18n(namespace?: string): {
  21. // t: I18nGlobalTranslation;
  22. // } {
  23. // const normalFn = {
  24. // t: (key: string) => {
  25. // return getKey(namespace, key);
  26. // },
  27. // };
  28. // if (!i18n) {
  29. // return normalFn;
  30. // }
  31. // const { t, ...methods } = i18n.global;
  32. // const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => {
  33. // if (!key) return '';
  34. // if (!key.includes('.') && !namespace) return key;
  35. // const res = t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters));
  36. // return res;
  37. // };
  38. // return {
  39. // ...methods,
  40. // t: tFn,
  41. // };
  42. // }
  43. // export const t = (key: string) => key;