You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
74 lines
2.0 KiB
import { changeLanguage } from '@/store/system.ts'
|
|
import i18n, { InitOptions, t } from 'i18next'
|
|
import LanguageDetector from 'i18next-browser-languagedetector'
|
|
import { initReactI18next, useTranslation, } from 'react-i18next'
|
|
import { zh, en } from './locales'
|
|
import { ProColumns } from '@ant-design/pro-components'
|
|
|
|
const detectionOptions = {
|
|
// 探测器的选项
|
|
order: [ 'querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag' ],
|
|
lookupQuerystring: 'lng',
|
|
lookupCookie: 'i18next',
|
|
lookupLocalStorage: 'i18nextLng',
|
|
caches: [ 'localStorage', 'cookie' ],
|
|
excludeCacheFor: [ 'cimode' ], // 语言探测模式中排除缓存的语言
|
|
}
|
|
|
|
|
|
export const initI18n = (options?: InitOptions) => {
|
|
|
|
i18n.on('initialized', () => {
|
|
const currentLanguage = i18n.language
|
|
changeLanguage(currentLanguage)
|
|
})
|
|
|
|
return i18n
|
|
.use(initReactI18next)
|
|
.use(LanguageDetector)
|
|
.init({
|
|
resources: {
|
|
en: {
|
|
translation: en.default,
|
|
},
|
|
zh: {
|
|
translation: zh.default,
|
|
},
|
|
},
|
|
fallbackLng: 'zh',
|
|
debug: false,
|
|
detection: detectionOptions,
|
|
interpolation: {
|
|
escapeValue: false,
|
|
},
|
|
...options,
|
|
})
|
|
|
|
}
|
|
|
|
export function getI18nTitle(key: string, defTitle: string): string;
|
|
export function getI18nTitle(key: string, column: ProColumns): string;
|
|
export function getI18nTitle(key: string, option: any): string {
|
|
|
|
if (option.dataIndex) {
|
|
let k = `${key}.columns.${option.dataIndex}`
|
|
if (option.i18n) {
|
|
k = option.i18n
|
|
}
|
|
//如果没有key也没有option.i18n, 说明没有i18n前缀, 直接返回
|
|
if (!option.i18n && !key) {
|
|
return option.title || option.label
|
|
}
|
|
return t(k, (option.title || option.label) as string)
|
|
}
|
|
|
|
if (!key) {
|
|
return option as unknown as string
|
|
}
|
|
return t(`${key}`, option as unknown as string)
|
|
}
|
|
|
|
export {
|
|
useTranslation, t
|
|
}
|
|
export default i18n
|