n20-common-lib 2.22.70 → 2.22.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/i18n.json +1563 -0
- package/src/utils/i18n/index.js +61 -25
package/src/utils/i18n/index.js
CHANGED
|
@@ -3,15 +3,67 @@ import langEn from 'element-ui/lib/locale/lang/en'
|
|
|
3
3
|
import langTh from 'element-ui/lib/locale/lang/th'
|
|
4
4
|
import langVi from 'element-ui/lib/locale/lang/vi'
|
|
5
5
|
import langTw from 'element-ui/lib/locale/lang/zh-TW'
|
|
6
|
+
import langEs from 'element-ui/lib/locale/lang/es'
|
|
6
7
|
|
|
7
8
|
import i18n_map_corelib from '../../i18n.json'
|
|
8
9
|
import zhHk from './cn2hk.json' // 简体繁体映射
|
|
9
10
|
// 语言枚举
|
|
10
11
|
// const langEm = { 中文: 'zh', 中文简体: 'zh-cn', 中文繁体: 'zh-hk', 英文: 'en', 德文: 'de', 法文: 'fr' }
|
|
11
12
|
|
|
12
|
-
|
|
13
|
+
/** 安全读取 localStorage,兼容 SSR / 浏览器禁用 storage 的场景 */
|
|
14
|
+
function getStoredLang() {
|
|
15
|
+
try {
|
|
16
|
+
return window.localStorage.getItem('pageLang')
|
|
17
|
+
} catch (e) {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let pageLang = getStoredLang() // 当前页面的语言(切换语言后会整页刷新,初始化读取一次即可)
|
|
13
23
|
let $i18n_map_root = {} // 全局的国际化
|
|
14
24
|
|
|
25
|
+
/**
|
|
26
|
+
* 运行时切换语言(同时持久化到 localStorage;element-ui 语言包需刷新页面后生效)
|
|
27
|
+
*/
|
|
28
|
+
export function setPageLang(lang) {
|
|
29
|
+
pageLang = lang
|
|
30
|
+
try {
|
|
31
|
+
window.localStorage.setItem('pageLang', lang)
|
|
32
|
+
} catch (e) {
|
|
33
|
+
/* storage 不可用时忽略 */
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// element-ui 语言包映射,替代 if/else 链
|
|
38
|
+
const elementLocaleMap = {
|
|
39
|
+
en: langEn,
|
|
40
|
+
th: langTh,
|
|
41
|
+
vi: langVi,
|
|
42
|
+
es: langEs,
|
|
43
|
+
'zh-hk': langTw
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 视为中文的语言(默认、简体、繁体)
|
|
47
|
+
const zhLangSet = new Set([undefined, null, '', 'zh-cn', 'zh-hk'])
|
|
48
|
+
|
|
49
|
+
// 简转繁结果缓存,避免过滤器高频调用时重复逐字转换
|
|
50
|
+
const cn2hkCache = new Map()
|
|
51
|
+
const CN2HK_CACHE_MAX = 5000
|
|
52
|
+
|
|
53
|
+
function cn2hk(zh = '') {
|
|
54
|
+
const cached = cn2hkCache.get(zh)
|
|
55
|
+
if (cached !== undefined) return cached
|
|
56
|
+
|
|
57
|
+
let _zh = ''
|
|
58
|
+
for (const ch of zh) {
|
|
59
|
+
_zh += zhHk[ch] || ch
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (cn2hkCache.size >= CN2HK_CACHE_MAX) cn2hkCache.clear() // 防止动态文案导致缓存无限增长
|
|
63
|
+
cn2hkCache.set(zh, _zh)
|
|
64
|
+
return _zh
|
|
65
|
+
}
|
|
66
|
+
|
|
15
67
|
export function $l(zh, map = {}, arg) {
|
|
16
68
|
let val = ''
|
|
17
69
|
if (!pageLang || pageLang === 'zh-cn') {
|
|
@@ -19,26 +71,17 @@ export function $l(zh, map = {}, arg) {
|
|
|
19
71
|
} else if (pageLang === 'zh-hk') {
|
|
20
72
|
val = cn2hk(zh)
|
|
21
73
|
} else {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
val = valMap[key] !== undefined ? valMap[key] : zh
|
|
74
|
+
const valMap = map[zh] || $i18n_map_root[zh]
|
|
75
|
+
val = valMap && valMap[pageLang] !== undefined ? valMap[pageLang] : zh
|
|
25
76
|
}
|
|
26
77
|
if (arg && val) {
|
|
27
|
-
|
|
28
|
-
|
|
78
|
+
// 用函数形式替换,避免替换值中含 $&、$1 等特殊替换模式时结果被破坏
|
|
79
|
+
for (const k of Object.keys(arg)) {
|
|
80
|
+
val = val.replace(k, () => arg[k])
|
|
29
81
|
}
|
|
30
82
|
}
|
|
31
83
|
return val
|
|
32
84
|
}
|
|
33
|
-
function cn2hk(zh = '') {
|
|
34
|
-
let _zh = ''
|
|
35
|
-
for (let i = 0; i < zh.length; i++) {
|
|
36
|
-
let v = zh[i]
|
|
37
|
-
let v_hk = zhHk[v]
|
|
38
|
-
v_hk ? (_zh += v_hk) : (_zh += v)
|
|
39
|
-
}
|
|
40
|
-
return _zh
|
|
41
|
-
}
|
|
42
85
|
|
|
43
86
|
export function $lc(zh, map) {
|
|
44
87
|
return $l(zh, map || i18n_map_corelib)
|
|
@@ -46,19 +89,12 @@ export function $lc(zh, map) {
|
|
|
46
89
|
|
|
47
90
|
const directive = {}
|
|
48
91
|
directive.install = (Vue, map = {}) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
} else if (pageLang === 'zh-hk') {
|
|
52
|
-
locale.use(langTw)
|
|
53
|
-
} else if (pageLang === 'th') {
|
|
54
|
-
locale.use(langTh)
|
|
55
|
-
} else if (pageLang === 'vi') {
|
|
56
|
-
locale.use(langVi)
|
|
57
|
-
}
|
|
92
|
+
const elementLocale = elementLocaleMap[pageLang]
|
|
93
|
+
if (elementLocale) locale.use(elementLocale)
|
|
58
94
|
|
|
59
95
|
$i18n_map_root = map
|
|
60
96
|
|
|
61
|
-
Vue.prototype._lang =
|
|
97
|
+
Vue.prototype._lang = zhLangSet.has(pageLang) ? 'zh' : pageLang
|
|
62
98
|
Vue.prototype.$l = $l
|
|
63
99
|
Vue.filter('$l', $l)
|
|
64
100
|
|