import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'
import zhCNLocale from 'element-ui/lib/locale/lang/zh-CN'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhCN from './zh-CN'
import en from './en'
Vue.use(VueI18n)

export const messages = {
  'zh-CN': {
    '_lang': '简体中文',
    ...zhCN,
    ...zhCNLocale
  },
  'en': {
    '_lang': 'English',
    ...en,
    ...enLocale
  }
}

export function getLanguage() {
  if (Cookies.get('language')) {
    return Cookies.get('language')
  }

  // if has not choose language
  const language = (navigator.language || navigator.browserLanguage)

  const locales = Object.keys(messages)
  for (const locale of locales) {
    if (language.indexOf(locale) > -1) {
      Cookies.set('language', locale)
      return locale
    }
  }
  Cookies.set('language', 'en')
  return 'en'

}

export default new VueI18n({
  // locale: Cookies.get('language') || 'zh-CN',
  locale: getLanguage(), // 先默认中文
  messages
})
