// 国际化配置
import { createI18n } from 'vue-i18n'

// 导入语言包
import zh from './zh.js'
import en from './en.js'
import es from './es.js'

// 获取系统语言
const getSystemLanguage = () => {
  let language = 'zh'
  
  // #ifdef APP-PLUS || H5
  language = uni.getSystemInfoSync().language
  // #endif
  
  // #ifdef MP-WEIXIN
  language = uni.getSystemInfoSync().language
  // #endif
  
  // 处理语言代码
  if (language.indexOf('zh') !== -1) {
    language = 'zh'
  } else if (language.indexOf('en') !== -1) {
    language = 'en'
  } else if (language.indexOf('es') !== -1) {
    language = 'es'
  } else {
    language = 'zh' // 默认中文
  }
  
  return language
}

// 获取存储的语言设置
const getStorageLanguage = () => {
  try {
    return uni.getStorageSync('language') || getSystemLanguage()
  } catch (e) {
    return getSystemLanguage()
  }
}

const i18n = createI18n({
  locale: getStorageLanguage(),
  fallbackLocale: 'zh',
  messages: {
    zh,
    en,
    es
  }
})

export default i18n