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.

50 lines
1.4 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. import { changeLanguage } from '@/store/system.ts'
  2. import i18n, { InitOptions, t } from 'i18next'
  3. import LanguageDetector from 'i18next-browser-languagedetector'
  4. import { initReactI18next, useTranslation, } from 'react-i18next'
  5. import { zh, en } from './locales'
  6. const detectionOptions = {
  7. // 探测器的选项
  8. order: [ 'querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag' ],
  9. lookupQuerystring: 'lng',
  10. lookupCookie: 'i18next',
  11. lookupLocalStorage: 'i18nextLng',
  12. caches: [ 'localStorage', 'cookie' ],
  13. excludeCacheFor: [ 'cimode' ], // 语言探测模式中排除缓存的语言
  14. }
  15. export const initI18n = (options?: InitOptions) => {
  16. i18n.on('initialized', () => {
  17. const currentLanguage = i18n.language
  18. changeLanguage(currentLanguage)
  19. })
  20. return i18n
  21. .use(initReactI18next)
  22. .use(LanguageDetector)
  23. .init({
  24. resources: {
  25. en: {
  26. translation: en.default,
  27. },
  28. zh: {
  29. translation: zh.default,
  30. },
  31. },
  32. fallbackLng: 'zh',
  33. debug: false,
  34. detection: detectionOptions,
  35. interpolation: {
  36. escapeValue: false,
  37. },
  38. ...options,
  39. })
  40. }
  41. export {
  42. useTranslation, t
  43. }
  44. export default i18n