Blame view

src/locale/index.js 1.51 KB
d33b5143   梁灏   support i18n
1
2
3
4
5
6
7
8
9
10
  import defaultLang from './lang/zh-CN';
  import Vue from 'vue';
  import deepmerge from 'deepmerge';
  import Format from './format';
  
  const format = Format(Vue);
  let lang = defaultLang;
  let merged = false;
  let i18nHandler = function() {
      const vuei18n = Object.getPrototypeOf(this || Vue).$t;
c7e432f7   梁灏   update export & i18n
11
      if (typeof vuei18n === 'function' && !!Vue.locale) {
d33b5143   梁灏   support i18n
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
          if (!merged) {
              merged = true;
              Vue.locale(
                  Vue.config.lang,
                  deepmerge(lang, Vue.locale(Vue.config.lang) || {}, { clone: true })
              );
          }
          return vuei18n.apply(this, arguments);
      }
  };
  
  export const t = function(path, options) {
      let value = i18nHandler.apply(this, arguments);
      if (value !== null && value !== undefined) return value;
  
      const array = path.split('.');
      let current = lang;
  
      for (let i = 0, j = array.length; i < j; i++) {
          const property = array[i];
          value = current[property];
          if (i === j - 1) return format(value, options);
          if (!value) return '';
          current = value;
      }
      return '';
  };
  
  export const use = function(l) {
      lang = l || lang;
  };
  
  export const i18n = function(fn) {
      i18nHandler = fn || i18nHandler;
  };
  
a5dad1af   梁灏   fixed #1090 & clo...
48
49
50
51
52
53
54
55
56
57
58
59
60
  // using with vue-i18n in CDN
  /*eslint-disable */
  export const setLang = function (l) {
      if (typeof window.iview !== 'undefined') {
          if (!('langs' in iview)) {
              iview.langs = {};
          }
          iview.langs[l] = lang;
      }
  };
  /*eslint-enable */
  
  export default { use, t, i18n, setLang };