This repository has been archived by the owner on Jan 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (82 loc) · 2.31 KB
/
index.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import request from 'platojs/request'
import template from 'string-template'
import createStore from './create-store'
export default ({ Vue }, options = {}) => {
options = {
scope: 'i18n',
lang: (navigator.language || navigator.browserLanguage).toLowerCase().split('-')[0],
translations: {},
...options
}
const {
scope,
fallbackLang = 'zh',
urlPattern = './i18n/{lang}.json'
} = options
function parseKeys (keys, scope) {
switch (keys.indexOf('/')) {
case 0: // 以 `/` 开头,说明是从全局里查找匹配
console.warn('[I18N] 斜杠开头的规则已废弃,请直接使用`scope/k.e.y.s`')
const arr1 = keys.split('.')
return {
scope: arr1[0].slice(1),
keyArray: arr1.slice(1)
}
case -1:
return {
scope,
keyArray: keys.split('.')
}
default:
const arr2 = keys.match(/(^\w+)\/(.+$)/)
return {
scope: arr2[1],
keyArray: arr2[2].split('.')
}
}
}
/**
* I18n
*/
Vue.prototype.__ = Vue.prototype.$translate = function (keys, ...args) {
if (!keys) {
return keys
}
const parsed = parseKeys(keys, this.$scope)
const { translations } = this.$store.state[scope]
// keys 以 `.` 作为分隔符
return template(parsed.keyArray.reduce((res, key) => {
if (res && typeof res === 'object' && res.hasOwnProperty(key)) {
return res[key]
}
return keys
}, parsed.scope ? translations[parsed.scope] : translations), ...args)
}
return [{
store: createStore(options),
options
}, ({ dispatch, subscribe }) => {
let fallbackEnabled = false
function fetchTranslations (lang) {
// add `dir="..."` to `<html>`
document.documentElement.dir = lang === 'ar' ? 'rtl' : 'ltr'
// request json data
request(template(urlPattern, { lang }))
.then(translations => {
dispatch('setI18n', { translations })
})
.catch(() => {
if (fallbackEnabled) {
// 确保只执行一次,避免无限循环
fallbackEnabled = false
fetchTranslations(fallbackLang)
}
})
}
// vm for watching i18n
subscribe('lang', lang => {
fallbackEnabled = true
fetchTranslations(lang)
})
}]
}