import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookie from 'js-cookie'
Vue.use(VueI18n)

export default ({ app, store, route }) => {
  const data = {}
  const Locale = store.state.locales // 语言种类
  for (let i = 0; i < Locale.length; i++) {
    data[Locale[i]] = require(`~/locales/${Locale[i]}.json`)
  }

  let lang = null
  if (route.query.lang) {
    Cookie.set('language', route.query.lang)
    lang = route.query.lang
  } else if (Cookie.get('language')) {
    lang = Cookie.get('language')
  } else {
    Cookie.set('language',process.env.LANGUAGE ? process.env.LANGUAGE : 'zh')
    lang = process.env.LANGUAGE ? process.env.LANGUAGE : 'zh'
  }
  store.commit('SET_LANGUAGE', lang)
  // Set i18n instance on app
  // This way we can use it in middleware and pages asyncData/fetch
  app.i18n = new VueI18n({
    locale: lang || store.state.locale, //获取vuex存储数据
    fallbackLocale: 'zh',
    messages: data,
    silentTranslationWarn: true
  })

}
