i18n.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import Vue from "vue"
  2. import VueI18n from "vue-i18n"
  3. import api from '../api/api';
  4. import ElementLocale from 'element-ui/lib/locale'
  5. import enLocale from 'element-ui/lib/locale/lang/en'
  6. import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
  7. import ruLocale from 'element-ui/lib/locale/lang/ru-RU'
  8. ElementLocale.i18n((key, value) => i18n.t(key, value)) //为了实现element插件的多语言切换
  9. Vue.use(VueI18n); // 全局注册国际化包
  10. let languages = localStorage.getItem('languages') ? JSON.parse(localStorage.getItem('languages')) : {
  11. RU: {},
  12. EN: {}
  13. },
  14. messages = {
  15. 'ZH': {
  16. ...zhLocale
  17. },
  18. 'EN': {
  19. ...enLocale,
  20. ...languages.EN
  21. },
  22. 'RU': {
  23. ...ruLocale,
  24. ...languages.RU
  25. }
  26. };
  27. api.requested({
  28. id: 2024062809072801,
  29. }).then(res => {
  30. if (res.code != 1) return;
  31. languages = res.data;
  32. localStorage.setItem('languages', JSON.stringify(languages))
  33. })
  34. console.log("locale", localStorage.getItem('lang') )
  35. // 准备翻译的语言环境信息
  36. const i18n = new VueI18n({
  37. locale: localStorage.getItem('lang') || "ZH", //将语言标识存入localStorage或sessionStorage中,页面刷新不会默认中文显示
  38. messages,
  39. silentTranslationWarn: true, //解决vue-i18n黄色警告"value of key 'xxx' is not a string"和"cannot translate the value of keypath 'xxx'.use the value of keypath as default",可忽略
  40. globalInjection: true, // 全局注入
  41. fallbackLocale: 'ZH', // 指定的locale没有找到对应的资源或当前语种不存在时,默认设置当前语种为中文
  42. });
  43. export default i18n