i18n.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import Vue from 'vue';
  2. import VueI18n from 'vue-i18n';
  3. import { GetWordPack } from '@/api/app';
  4. import ElementLocale from 'element-ui/lib/locale';
  5. import zhLocal from 'element-ui/lib/locale/lang/zh-CN';
  6. import arLocal from 'element-ui/lib/locale/lang/ar';
  7. import enLocal from 'element-ui/lib/locale/lang/en';
  8. import jaLocal from 'element-ui/lib/locale/lang/ja';
  9. import deLocal from 'element-ui/lib/locale/lang/de';
  10. import ruLocal from 'element-ui/lib/locale/lang/ru-RU';
  11. Vue.use(VueI18n);
  12. // function getBasicI18nMessage() {
  13. // let lang = getI18nLang();
  14. // GetWordPack({
  15. // language_type: lang,
  16. // word_key_list: {}
  17. // });
  18. // }
  19. const i18n = new VueI18n({
  20. locale: localStorage.getItem('lang') || 'ZH',
  21. messages: {
  22. ZH: {
  23. ...zhLocal
  24. },
  25. AR: {
  26. ...arLocal
  27. },
  28. EN: {
  29. ...enLocal
  30. },
  31. JA: {
  32. ...jaLocal
  33. },
  34. DE: {
  35. ...deLocal
  36. },
  37. RU: {
  38. ...ruLocal
  39. }
  40. },
  41. silentTranslationWarn: true
  42. });
  43. ElementLocale.i18n((key, value) => i18n.t(key, value));
  44. export function setI18nLang(lang) {
  45. localStorage.setItem('lang', lang);
  46. i18n.locale = lang;
  47. }
  48. /**
  49. * @description 更新语言列表
  50. * @param {Object} Parameter word_key_list 需要读取的词汇
  51. */
  52. export function updateWordPack(Parameter) {
  53. Parameter.language_type = i18n.locale;
  54. GetWordPack(Parameter).then(data => {
  55. let localWord = i18n.messages[data.language_type];
  56. if (localWord === undefined) {
  57. localWord = {};
  58. }
  59. let wordPack = Object.assign(localWord, data.word_pack);
  60. i18n.setLocaleMessage(data.language_type, wordPack);
  61. });
  62. }
  63. export default i18n;