jhzh
2024-03-23 0d29d9a2bf0d893a67f1263bb9525131a50a2128
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import en from './en.js'
import india from '@/locales/india'
import tw from '@/locales/tw'
 
Vue.use(VueI18n)
 
const DEFAULT_LANG = 'zh-CN'
const LOCALE_KEY = 'language'
 
const locales = {
  'zh-CN': {
    ...en
  },
  en: {
    ...india
  },
  tw: {
    ...tw
  }
}
// en-us  zh-cn
// let langLocale = getCookie(LOCALE_KEY) || 'en';
if (!window.localStorage.getItem(LOCALE_KEY)) {
  window.localStorage.setItem(LOCALE_KEY, DEFAULT_LANG)
}
let langLocale = window.localStorage.getItem(LOCALE_KEY)
  ? window.localStorage.getItem(LOCALE_KEY)
  : 'zh-CN'
const i18n = new VueI18n({
  locale: langLocale,
  messages: locales,
  silentTranslationWarn: true
})
const init = Vue.prototype._init
Vue.prototype._init = function (options) {
  init.call(this, {
    i18n,
    ...options
  })
}
 
export const setup = lang => {
  if (lang === undefined) {
    // lang = window.localStorage.getItem(LOCALE_KEY);
    // const language = getCookie(LOCALE_KEY);
    const language = window.localStorage.getItem(LOCALE_KEY)
    if (language) {
      langLocale = language.replace('-', '_').toLowerCase()
    }
    if (undefined !== langLocale && langLocale !== '') {
      lang = langLocale
    }
    if (locales[lang] === undefined) {
      lang = DEFAULT_LANG
    }
    // console.log(`lang-${lang}`);
  }
 
  Vue.config.lang = lang
  i18n.locale = lang
}
 
setup()
 
export default i18n