import Taro from '@tarojs/taro';
import LanguageDetector from 'i18next-browser-languagedetector';
import i18n from 'i18next';
import zh from './zh'
import en from './en'
import {initReactI18next} from 'react-i18next';

Taro.getStorage({ key: 'language' }).then(res => {
  // initLocale(res.data)
  initLocale('zh')
}).catch(err => {
  initLocale('zh')
})

function initLocale(strLocale: string) {
  i18n.use(LanguageDetector) //嗅探当前浏览器语言
  .use(initReactI18next) //init i18next
  .init({
    //引入资源文件
    resources: {
      en: {
        translation: en,
      },
      zh: {
        translation: zh,
      },
    },
    //选择默认语言，选择内容为上述配置中的key，即en/zh
    lng: strLocale,//Taro.getStorageSync('language') ? Taro.getStorageSync('language') : 'en',
    debug: false,
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
  })
}



export default i18n;