@webc.site/i18n 0.0.0-stage → 0.1.0
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 +20 -4
- package/src/const.js +2 -0
- package/src/state.js +209 -0
- package/src/tpl.js +13 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webc.site/i18n",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Runtime i18n loader and live switching for webc components",
|
|
5
|
+
"license": "MulanPSL-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://atomgit.com/i18n-site/webc.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/state.js",
|
|
17
|
+
"./*": "./src/*"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src"
|
|
21
|
+
]
|
|
22
|
+
}
|
package/src/const.js
ADDED
package/src/state.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { BC_NAME, STORAGE_KEY } from './const.js'
|
|
2
|
+
import { tplBind } from './tpl.js'
|
|
3
|
+
|
|
4
|
+
export let LANG = ''
|
|
5
|
+
export let LANG_LI = []
|
|
6
|
+
|
|
7
|
+
const HOOK_SET = new Set(),
|
|
8
|
+
PROD_CACHE = new Map(),
|
|
9
|
+
DEV_CACHE = new Map(),
|
|
10
|
+
DEV_VER_MAP = new Map(),
|
|
11
|
+
DEV_HOOK_MAP = new Map(),
|
|
12
|
+
CFG = {
|
|
13
|
+
lang_map: {},
|
|
14
|
+
from_lang: '',
|
|
15
|
+
base: '/',
|
|
16
|
+
dev_base: '/_i18n/',
|
|
17
|
+
prod_load: null,
|
|
18
|
+
dev_load: null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let channel = null
|
|
22
|
+
|
|
23
|
+
const channelInit = () => {
|
|
24
|
+
if (channel || typeof globalThis.BroadcastChannel === 'undefined') return
|
|
25
|
+
channel = new globalThis.BroadcastChannel(BC_NAME)
|
|
26
|
+
channel.onmessage = ({ data }) => {
|
|
27
|
+
if (data && data !== LANG) langApply(data, false)
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
storeRead = () => {
|
|
31
|
+
if (typeof globalThis.localStorage === 'undefined') return ''
|
|
32
|
+
return globalThis.localStorage.getItem(STORAGE_KEY) || ''
|
|
33
|
+
},
|
|
34
|
+
storeWrite = (code) => {
|
|
35
|
+
if (typeof globalThis.localStorage === 'undefined') return
|
|
36
|
+
globalThis.localStorage.setItem(STORAGE_KEY, code)
|
|
37
|
+
},
|
|
38
|
+
prodUrl = (code) => {
|
|
39
|
+
const target =
|
|
40
|
+
CFG.lang_map[code] ??
|
|
41
|
+
CFG.lang_map[CFG.from_lang] ??
|
|
42
|
+
CFG.lang_map[LANG_LI[0]]
|
|
43
|
+
if (!target) return ''
|
|
44
|
+
if (
|
|
45
|
+
target.startsWith('http://') ||
|
|
46
|
+
target.startsWith('https://') ||
|
|
47
|
+
target.startsWith('/')
|
|
48
|
+
) {
|
|
49
|
+
return target
|
|
50
|
+
}
|
|
51
|
+
return CFG.base + target
|
|
52
|
+
},
|
|
53
|
+
prodFetch = async (code) => {
|
|
54
|
+
const cached = PROD_CACHE.get(code)
|
|
55
|
+
if (cached) return cached
|
|
56
|
+
const task = (async () => {
|
|
57
|
+
if (CFG.prod_load) {
|
|
58
|
+
const res = await CFG.prod_load(code, CFG.lang_map[code])
|
|
59
|
+
return Array.isArray(res) ? res.map(tplBind) : []
|
|
60
|
+
}
|
|
61
|
+
const url = prodUrl(code)
|
|
62
|
+
if (!url) return []
|
|
63
|
+
const mod = await import(/* @vite-ignore */ /* webpackIgnore: true */ url)
|
|
64
|
+
return (mod.default ?? mod).map(tplBind)
|
|
65
|
+
})()
|
|
66
|
+
PROD_CACHE.set(code, task)
|
|
67
|
+
return task
|
|
68
|
+
},
|
|
69
|
+
devUrlOf = (comp_key, code) => {
|
|
70
|
+
const ver = DEV_VER_MAP.get(comp_key) ?? 0
|
|
71
|
+
return (
|
|
72
|
+
CFG.dev_base +
|
|
73
|
+
comp_key +
|
|
74
|
+
'/' +
|
|
75
|
+
code +
|
|
76
|
+
'.js' +
|
|
77
|
+
(ver > 0 ? '?v=' + ver : '')
|
|
78
|
+
)
|
|
79
|
+
},
|
|
80
|
+
devFetch = async (comp_key, code) => {
|
|
81
|
+
const cache_key = comp_key + ':' + code,
|
|
82
|
+
cached = DEV_CACHE.get(cache_key)
|
|
83
|
+
if (cached) return cached
|
|
84
|
+
const task = (async () => {
|
|
85
|
+
if (CFG.dev_load) {
|
|
86
|
+
const res = await CFG.dev_load(comp_key, code)
|
|
87
|
+
return tplBind(res)
|
|
88
|
+
}
|
|
89
|
+
const url = devUrlOf(comp_key, code),
|
|
90
|
+
mod = await import(/* @vite-ignore */ /* webpackIgnore: true */ url)
|
|
91
|
+
return tplBind(mod.default ?? mod)
|
|
92
|
+
})()
|
|
93
|
+
DEV_CACHE.set(cache_key, task)
|
|
94
|
+
return task
|
|
95
|
+
},
|
|
96
|
+
langApply = (code, broadcast) => {
|
|
97
|
+
LANG = code
|
|
98
|
+
storeWrite(code)
|
|
99
|
+
if (broadcast && channel) channel.postMessage(code)
|
|
100
|
+
HOOK_SET.forEach((fn) => fn(code))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const langConf = ({
|
|
104
|
+
lang_li,
|
|
105
|
+
lang,
|
|
106
|
+
from_lang,
|
|
107
|
+
lang_map,
|
|
108
|
+
base,
|
|
109
|
+
dev_base,
|
|
110
|
+
prod_load,
|
|
111
|
+
dev_load
|
|
112
|
+
}) => {
|
|
113
|
+
if (lang_li) LANG_LI = lang_li
|
|
114
|
+
if (from_lang) CFG.from_lang = from_lang
|
|
115
|
+
else if (lang && !CFG.from_lang) CFG.from_lang = lang
|
|
116
|
+
if (lang_map) CFG.lang_map = lang_map
|
|
117
|
+
if (base !== undefined) CFG.base = base
|
|
118
|
+
if (dev_base !== undefined) CFG.dev_base = dev_base
|
|
119
|
+
if (prod_load !== undefined) CFG.prod_load = prod_load
|
|
120
|
+
if (dev_load !== undefined) CFG.dev_load = dev_load
|
|
121
|
+
channelInit()
|
|
122
|
+
const initial = (lang ?? storeRead()) || LANG_LI[0] || 'zh'
|
|
123
|
+
if (initial && initial !== LANG) langApply(initial, false)
|
|
124
|
+
},
|
|
125
|
+
langSet = (code) => {
|
|
126
|
+
if (!code || code === LANG) return
|
|
127
|
+
langApply(code, true)
|
|
128
|
+
},
|
|
129
|
+
onLang = (fn) => {
|
|
130
|
+
HOOK_SET.add(fn)
|
|
131
|
+
if (LANG) fn(LANG)
|
|
132
|
+
return () => HOOK_SET.delete(fn)
|
|
133
|
+
},
|
|
134
|
+
// Production runtime binding emitted by @webc.site/compile
|
|
135
|
+
i18nReg = (comp_id, apply) => {
|
|
136
|
+
let active = true,
|
|
137
|
+
seq = 0
|
|
138
|
+
const refresh = async (code) => {
|
|
139
|
+
const my_seq = ++seq,
|
|
140
|
+
bundle_li = await prodFetch(code)
|
|
141
|
+
if (!active || my_seq !== seq) return
|
|
142
|
+
apply(bundle_li[comp_id] ?? [])
|
|
143
|
+
}
|
|
144
|
+
const un = onLang(refresh)
|
|
145
|
+
return () => {
|
|
146
|
+
active = false
|
|
147
|
+
un()
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
// Development runtime binding emitted by @webc.site/compile
|
|
151
|
+
i18nDev = (comp_key, apply) => {
|
|
152
|
+
let active = true,
|
|
153
|
+
seq = 0
|
|
154
|
+
const refresh = async (code) => {
|
|
155
|
+
const my_seq = ++seq,
|
|
156
|
+
dict = await devFetch(comp_key, code)
|
|
157
|
+
if (!active || my_seq !== seq) return
|
|
158
|
+
apply(dict)
|
|
159
|
+
}
|
|
160
|
+
let set = DEV_HOOK_MAP.get(comp_key)
|
|
161
|
+
if (!set) {
|
|
162
|
+
set = new Set()
|
|
163
|
+
DEV_HOOK_MAP.set(comp_key, set)
|
|
164
|
+
}
|
|
165
|
+
const rerun = () => refresh(LANG || 'zh')
|
|
166
|
+
set.add(rerun)
|
|
167
|
+
const un = onLang(refresh)
|
|
168
|
+
return () => {
|
|
169
|
+
active = false
|
|
170
|
+
un()
|
|
171
|
+
set.delete(rerun)
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
// Invalidate dev cache for a component and refresh active mounts on HMR
|
|
175
|
+
devUrl = devUrlOf,
|
|
176
|
+
i18nDevDrop = (comp_key) => {
|
|
177
|
+
const prefix = comp_key + ':'
|
|
178
|
+
DEV_VER_MAP.set(comp_key, (DEV_VER_MAP.get(comp_key) ?? 0) + 1)
|
|
179
|
+
for (const key of DEV_CACHE.keys()) {
|
|
180
|
+
if (key.startsWith(prefix)) DEV_CACHE.delete(key)
|
|
181
|
+
}
|
|
182
|
+
DEV_HOOK_MAP.get(comp_key)?.forEach((rerun) => rerun())
|
|
183
|
+
},
|
|
184
|
+
// Push preloaded language data (used by Lynx MTS after BTS fetch)
|
|
185
|
+
i18nApply = (code, bundle_li) => {
|
|
186
|
+
const bound = bundle_li.map(tplBind)
|
|
187
|
+
PROD_CACHE.set(code, Promise.resolve(bound))
|
|
188
|
+
langApply(code, false)
|
|
189
|
+
},
|
|
190
|
+
// Reset runtime state for isolated unit tests
|
|
191
|
+
i18nReset = () => {
|
|
192
|
+
LANG = ''
|
|
193
|
+
LANG_LI = []
|
|
194
|
+
HOOK_SET.clear()
|
|
195
|
+
PROD_CACHE.clear()
|
|
196
|
+
DEV_CACHE.clear()
|
|
197
|
+
DEV_VER_MAP.clear()
|
|
198
|
+
DEV_HOOK_MAP.clear()
|
|
199
|
+
CFG.lang_map = {}
|
|
200
|
+
CFG.from_lang = ''
|
|
201
|
+
CFG.base = '/'
|
|
202
|
+
CFG.dev_base = '/_i18n/'
|
|
203
|
+
CFG.prod_load = null
|
|
204
|
+
CFG.dev_load = null
|
|
205
|
+
},
|
|
206
|
+
// Compile-time marker; transformed by @webc.site/compile
|
|
207
|
+
$i18n = () => {
|
|
208
|
+
throw new Error('$i18n must be compiled by @webc.site/compile')
|
|
209
|
+
}
|
package/src/tpl.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Convert Lynx JSON fragment arrays (e.g. ['Hi ', 0, '!']) into callable interpolators
|
|
2
|
+
export const tplBind = (entry_li) =>
|
|
3
|
+
entry_li.map((item) => {
|
|
4
|
+
if (!Array.isArray(item)) return item
|
|
5
|
+
return (...arg_li) => {
|
|
6
|
+
let out = ''
|
|
7
|
+
for (let i = 0; i < item.length; ++i) {
|
|
8
|
+
const seg = item[i]
|
|
9
|
+
out += typeof seg === 'number' ? (arg_li[seg] ?? '') : seg
|
|
10
|
+
}
|
|
11
|
+
return out
|
|
12
|
+
}
|
|
13
|
+
})
|
package/README.md
DELETED