File size: 867 Bytes
891fa77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import LanguageDetector from 'i18next-browser-languagedetector'

import zh from './locales/zh.json'
import en from './locales/en.json'

const resources = {
  zh: { translation: zh },
  en: { translation: en },
}

i18n
  .use(LanguageDetector) // 检测浏览器语言
  .use(initReactI18next) // 传递 i18n 实例给 react-i18next
  .init({
    resources,
    fallbackLng: 'en', // 默认语言
    supportedLngs: ['zh', 'en'], // 支持的语言
    detection: {
      // 语言检测顺序: localStorage > 浏览器语言 > 默认语言
      order: ['localStorage', 'navigator'],
      caches: ['localStorage'], // 缓存用户选择
      lookupLocalStorage: 'i18nextLng',
    },
    interpolation: {
      escapeValue: false, // React 已经防止 XSS
    },
  })

export default i18n