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.
51 lines
1.4 KiB
51 lines
1.4 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'
|
|
|
|
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 {
|
|
useTranslation, t
|
|
}
|
|
export default i18n
|