|
|
@ -1,5 +1,11 @@ |
|
|
|
import { i18n } from '/@/locales/setupI18n'; |
|
|
|
/** |
|
|
|
|
|
|
|
* @program: kicc-ui |
|
|
|
|
|
|
|
* @description: 国际化工具 |
|
|
|
|
|
|
|
* @author: entfrm开发团队-王翔 |
|
|
|
|
|
|
|
* @create: 2022/4/10 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { i18n } from '/@/locales/setupI18n'; |
|
|
|
type I18nGlobalTranslation = { |
|
|
|
type I18nGlobalTranslation = { |
|
|
|
(key: string): string; |
|
|
|
(key: string): string; |
|
|
|
(key: string, locale: string): string; |
|
|
|
(key: string, locale: string): string; |
|
|
@ -8,41 +14,28 @@ type I18nGlobalTranslation = { |
|
|
|
(key: string, list: unknown[]): string; |
|
|
|
(key: string, list: unknown[]): string; |
|
|
|
(key: string, named: Record<string, unknown>): string; |
|
|
|
(key: string, named: Record<string, unknown>): string; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type I18nTranslationRestParameters = [string, any]; |
|
|
|
type I18nTranslationRestParameters = [string, any]; |
|
|
|
|
|
|
|
|
|
|
|
function getKey(namespace: string | undefined, key: string) { |
|
|
|
function getKey(namespace: string | undefined, key: string) { |
|
|
|
if (!namespace) { |
|
|
|
if (!namespace) return key; |
|
|
|
return key; |
|
|
|
if (key.startsWith(namespace)) return key; |
|
|
|
} |
|
|
|
|
|
|
|
if (key.startsWith(namespace)) { |
|
|
|
|
|
|
|
return key; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return `${namespace}.${key}`; |
|
|
|
return `${namespace}.${key}`; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function useI18n(namespace?: string): { |
|
|
|
/** 国际化转换,提供i18n全部功能 */ |
|
|
|
t: I18nGlobalTranslation; |
|
|
|
export function useI18n(namespace?: string): { t: I18nGlobalTranslation; } { |
|
|
|
} { |
|
|
|
|
|
|
|
const normalFn = { |
|
|
|
const normalFn = { |
|
|
|
t: (key: string) => { |
|
|
|
t: (key: string) => getKey(namespace, key) |
|
|
|
return getKey(namespace, key); |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
if (!i18n) return normalFn; |
|
|
|
if (!i18n) { |
|
|
|
|
|
|
|
return normalFn; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { t, ...methods } = i18n.global; |
|
|
|
const { t, ...methods } = i18n.global; |
|
|
|
|
|
|
|
|
|
|
|
const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => { |
|
|
|
const tFn: I18nGlobalTranslation = (key: string, ...arg: any[]) => { |
|
|
|
if (!key) return ''; |
|
|
|
if (!key) return ''; |
|
|
|
if (!key.includes('.') && !namespace) return key; |
|
|
|
if (!key.includes('.') && !namespace) return key; |
|
|
|
return t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters)); |
|
|
|
return t(getKey(namespace, key), ...(arg as I18nTranslationRestParameters)); |
|
|
|
}; |
|
|
|
}; |
|
|
|
return { |
|
|
|
return { ...methods, t: tFn }; |
|
|
|
...methods, |
|
|
|
|
|
|
|
t: tFn, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 国际化转换,只提供i18n简单转换功能 */ |
|
|
|
|
|
|
|
export const t = (key: string): string => useI18n().t(key); |
|
|
|