import Vue from 'vue'
import VueI18n from 'vue-i18n'
import customZhCn from './lang/zh-CN'
import customZhTw from './lang/zh-TW'
import customEnUs from './lang/en-US'

import zhLocale from 'view-design/src/locale/lang/zh-CN'
import enLocale from 'view-design/src/locale/lang/en-US'
import twLocale from 'view-design/src/locale/lang/zh-TW'

import enEl from 'element-ui/lib/locale/lang/en'
import zhEl from 'element-ui/lib/locale/lang/zh-CN'
import twEl from 'element-ui/lib/locale/lang/zh-TW'
import ElementLocale from 'element-ui/lib/locale'

ElementLocale.i18n((key, value) => i18n.t(key, value))

Vue.use(VueI18n)

// 自动根据浏览器系统语言设置语言
const curLang  = localStorage.getItem('cloudSetting') ? JSON.parse(localStorage.getItem('cloudSetting')).curLang : null
const navLang = curLang || navigator.language.toLowerCase()
const localLang = (navLang === 'zh' || navLang === 'zh-tw' || navLang === 'zh-cn' || navLang === 'zh-hk') ? navLang : false
let lang = localLang || 'en-us'
localStorage.setItem('local', lang)
Vue.config.lang = lang

Vue.locale = () => { }
const messages = {
    'zh-cn': Object.assign(customZhCn, zhLocale, zhEl),
    'zh': Object.assign(customZhCn, zhLocale,zhEl),
    'zh-tw': Object.assign(customZhTw, twLocale,twEl),
    'zh-hk': Object.assign(customZhTw, twLocale, twEl),
    'en-us': Object.assign(customEnUs, enLocale, enEl)
}
const i18n = new VueI18n({
  locale: lang,
  messages
})

export default i18n
