@shuo-li/i18n 1.0.3 → 1.0.5
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/dist/{cacheEvents-CDBRPnHU.d.cts → cacheEvents-CSwgzbob.d.cts} +6 -15
- package/dist/{cacheEvents-CDBRPnHU.d.ts → cacheEvents-CSwgzbob.d.ts} +6 -15
- package/dist/{chunk-K5VX3COH.cjs → chunk-JPKSBSEO.cjs} +136 -129
- package/dist/{chunk-IWP4252C.js → chunk-X6D2MZ7M.js} +124 -117
- package/dist/index.cjs +7 -46
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +4 -43
- package/dist/mp/index.cjs +9 -43
- package/dist/mp/index.d.cts +2 -2
- package/dist/mp/index.d.ts +2 -2
- package/dist/mp/index.js +7 -41
- package/dist/native/index.cjs +10 -44
- package/dist/native/index.d.cts +2 -2
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +7 -41
- package/dist/workers/preload-worker-rn.cjs +12 -55
- package/dist/workers/preload-worker-rn.js +12 -55
- package/dist/workers/preload-worker.cjs +4 -128
- package/dist/workers/preload-worker.js +3 -127
- package/package.json +1 -1
|
@@ -119,6 +119,56 @@ var SSEClient = class {
|
|
|
119
119
|
}
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
+
// src/core/worker-manager.ts
|
|
123
|
+
var WorkerManager = class {
|
|
124
|
+
constructor() {
|
|
125
|
+
this.worker = null;
|
|
126
|
+
this.pendingCallbacks = /* @__PURE__ */ new Set();
|
|
127
|
+
this.finalized = false;
|
|
128
|
+
}
|
|
129
|
+
start(createWorker, payload, callbacks) {
|
|
130
|
+
this.terminate();
|
|
131
|
+
this.finalized = false;
|
|
132
|
+
this.pendingCallbacks = /* @__PURE__ */ new Set();
|
|
133
|
+
if (payload.tasks.length === 0) {
|
|
134
|
+
callbacks.onDone();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const worker = createWorker();
|
|
138
|
+
this.worker = worker;
|
|
139
|
+
worker.onMessage((msg) => {
|
|
140
|
+
if (msg.type === "rawData") {
|
|
141
|
+
const p = callbacks.onRawData(msg.task, msg.raw).catch(() => {
|
|
142
|
+
});
|
|
143
|
+
this.pendingCallbacks.add(p);
|
|
144
|
+
p.finally(() => this.pendingCallbacks.delete(p));
|
|
145
|
+
} else if (msg.type === "moduleError") {
|
|
146
|
+
callbacks.onModuleError?.(msg);
|
|
147
|
+
} else if (msg.type === "done" || msg.type === "error") {
|
|
148
|
+
if (msg.type === "error") callbacks.onError?.();
|
|
149
|
+
this.finalize(callbacks);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
worker.postMessage({ type: "start", payload });
|
|
153
|
+
}
|
|
154
|
+
terminate() {
|
|
155
|
+
this.worker?.terminate();
|
|
156
|
+
this.worker = null;
|
|
157
|
+
}
|
|
158
|
+
isRunning() {
|
|
159
|
+
return this.worker !== null;
|
|
160
|
+
}
|
|
161
|
+
finalize(callbacks) {
|
|
162
|
+
if (this.finalized) return;
|
|
163
|
+
this.finalized = true;
|
|
164
|
+
const pending = [...this.pendingCallbacks];
|
|
165
|
+
Promise.allSettled(pending).then(() => {
|
|
166
|
+
this.terminate();
|
|
167
|
+
callbacks.onDone();
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
122
172
|
// src/core/utils.ts
|
|
123
173
|
function buildKey(moduleCode, langCode, store) {
|
|
124
174
|
return store.cacheGroupKey ? `${moduleCode}_${langCode}_${store.cacheGroupKey}` : `${moduleCode}_${langCode}`;
|
|
@@ -174,89 +224,7 @@ function toQueryString(params) {
|
|
|
174
224
|
).toString();
|
|
175
225
|
}
|
|
176
226
|
|
|
177
|
-
// src/core/worker-manager.ts
|
|
178
|
-
var WorkerManager = class {
|
|
179
|
-
constructor() {
|
|
180
|
-
this.worker = null;
|
|
181
|
-
this.pendingCallbacks = /* @__PURE__ */ new Set();
|
|
182
|
-
this.finalized = false;
|
|
183
|
-
}
|
|
184
|
-
start(createWorker, payload, callbacks, storage) {
|
|
185
|
-
this.terminate();
|
|
186
|
-
this.finalized = false;
|
|
187
|
-
this.pendingCallbacks = /* @__PURE__ */ new Set();
|
|
188
|
-
if (payload.tasks.length === 0) {
|
|
189
|
-
callbacks.onDone();
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const worker = createWorker();
|
|
193
|
-
this.worker = worker;
|
|
194
|
-
worker.onMessage((msg) => {
|
|
195
|
-
if (msg.type === "moduleLoaded") {
|
|
196
|
-
const m = msg;
|
|
197
|
-
const p = callbacks.onModuleLoaded({
|
|
198
|
-
storeName: m.storeName,
|
|
199
|
-
langCode: m.langCode,
|
|
200
|
-
moduleCode: m.moduleCode,
|
|
201
|
-
version: m.version
|
|
202
|
-
});
|
|
203
|
-
this.pendingCallbacks.add(p);
|
|
204
|
-
p.finally(() => this.pendingCallbacks.delete(p));
|
|
205
|
-
} else if (msg.type === "dataReady") {
|
|
206
|
-
if (!storage) return;
|
|
207
|
-
const p = (async () => {
|
|
208
|
-
const existing = await storage.getRecord(msg.storeName, msg.record.key);
|
|
209
|
-
if (existing && msg.record.version <= existing.version) return;
|
|
210
|
-
let resources;
|
|
211
|
-
if (msg.storeIndex === 0) {
|
|
212
|
-
resources = deepMerge(
|
|
213
|
-
existing?.resources ?? {},
|
|
214
|
-
msg.record.resources
|
|
215
|
-
);
|
|
216
|
-
} else {
|
|
217
|
-
resources = { ...existing?.resources ?? {}, ...msg.record.resources };
|
|
218
|
-
}
|
|
219
|
-
const mergedRecord = { ...msg.record, resources };
|
|
220
|
-
await storage.putRecord(msg.storeName, mergedRecord);
|
|
221
|
-
await callbacks.onModuleLoaded({
|
|
222
|
-
storeName: msg.task.storeName,
|
|
223
|
-
langCode: mergedRecord.langCode,
|
|
224
|
-
moduleCode: mergedRecord.moduleCode,
|
|
225
|
-
version: mergedRecord.version
|
|
226
|
-
});
|
|
227
|
-
})().catch(() => {
|
|
228
|
-
});
|
|
229
|
-
this.pendingCallbacks.add(p);
|
|
230
|
-
p.finally(() => this.pendingCallbacks.delete(p));
|
|
231
|
-
} else if (msg.type === "moduleError") {
|
|
232
|
-
callbacks.onModuleError?.(msg);
|
|
233
|
-
} else if (msg.type === "done" || msg.type === "error") {
|
|
234
|
-
if (msg.type === "error") callbacks.onError?.();
|
|
235
|
-
this.finalize(callbacks);
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
worker.postMessage({ type: "start", payload });
|
|
239
|
-
}
|
|
240
|
-
terminate() {
|
|
241
|
-
this.worker?.terminate();
|
|
242
|
-
this.worker = null;
|
|
243
|
-
}
|
|
244
|
-
isRunning() {
|
|
245
|
-
return this.worker !== null;
|
|
246
|
-
}
|
|
247
|
-
finalize(callbacks) {
|
|
248
|
-
if (this.finalized) return;
|
|
249
|
-
this.finalized = true;
|
|
250
|
-
const pending = [...this.pendingCallbacks];
|
|
251
|
-
Promise.allSettled(pending).then(() => {
|
|
252
|
-
this.terminate();
|
|
253
|
-
callbacks.onDone();
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
|
|
258
227
|
// src/core/manager.ts
|
|
259
|
-
var DB_NAME = "i18n_cache";
|
|
260
228
|
var PRIORITY_MODULES = ["ENUMS", "CUSTOMER"];
|
|
261
229
|
var currentLang = "zh_CN";
|
|
262
230
|
var currentSig = null;
|
|
@@ -301,7 +269,7 @@ async function initI18n(options) {
|
|
|
301
269
|
currentSig = sig;
|
|
302
270
|
initOptions = options;
|
|
303
271
|
const p = _initI18n(options).then(() => {
|
|
304
|
-
if (!options.apiContext.isLoggedIn
|
|
272
|
+
if (!options.apiContext.isLoggedIn) {
|
|
305
273
|
currentPromise = null;
|
|
306
274
|
}
|
|
307
275
|
}).catch((err) => {
|
|
@@ -362,11 +330,11 @@ async function getAllRecordsByModule(moduleCode) {
|
|
|
362
330
|
async function _initI18n(options) {
|
|
363
331
|
const { storage } = managerOptions;
|
|
364
332
|
const { apiContext } = options;
|
|
365
|
-
currentLang = options.
|
|
333
|
+
currentLang = options.langCode;
|
|
366
334
|
resolvedStores = options.stores;
|
|
367
|
-
resolvedLanguages = options.languages ?? [options.
|
|
335
|
+
resolvedLanguages = options.languages ?? [options.langCode];
|
|
368
336
|
resolvedModules = options.modules ?? [];
|
|
369
|
-
await storage.
|
|
337
|
+
await storage.ensureStores(resolvedStores);
|
|
370
338
|
await checkCacheVersion(storage, options);
|
|
371
339
|
for (const lang of resolvedLanguages) {
|
|
372
340
|
for (const m of resolvedModules) {
|
|
@@ -375,7 +343,7 @@ async function _initI18n(options) {
|
|
|
375
343
|
}
|
|
376
344
|
await injectCurrentLanguageModules(currentLang);
|
|
377
345
|
await i18n.changeLanguage(currentLang);
|
|
378
|
-
if (!apiContext.isLoggedIn
|
|
346
|
+
if (!apiContext.isLoggedIn) {
|
|
379
347
|
if (!unloginPullStarted && apiContext.unloginPull) {
|
|
380
348
|
unloginPullStarted = true;
|
|
381
349
|
await doUnloginPull(storage, options);
|
|
@@ -394,41 +362,55 @@ async function _initI18n(options) {
|
|
|
394
362
|
startSSE(options);
|
|
395
363
|
}
|
|
396
364
|
}
|
|
365
|
+
var CACHE_VERSION_KEY = "__meta__:cache_version";
|
|
397
366
|
async function checkCacheVersion(storage, options) {
|
|
398
367
|
if (options.version == null) return;
|
|
368
|
+
const baseStoreName = resolvedStores[0].name;
|
|
399
369
|
const current = String(options.version);
|
|
400
|
-
const
|
|
370
|
+
const record = await storage.getRecord(baseStoreName, CACHE_VERSION_KEY).catch(() => void 0);
|
|
371
|
+
const stored = record?.resources?.["value"];
|
|
401
372
|
if (stored === current) return;
|
|
402
373
|
for (const store of options.stores) {
|
|
403
374
|
await storage.clearStore(store.name);
|
|
404
375
|
}
|
|
405
|
-
await storage.
|
|
376
|
+
await storage.putRecord(baseStoreName, {
|
|
377
|
+
key: CACHE_VERSION_KEY,
|
|
378
|
+
moduleCode: "__meta__",
|
|
379
|
+
langCode: "__meta__",
|
|
380
|
+
version: 0,
|
|
381
|
+
resources: { value: current }
|
|
382
|
+
});
|
|
406
383
|
}
|
|
407
384
|
async function doUnloginPull(storage, options) {
|
|
408
385
|
const { apiContext } = options;
|
|
409
386
|
if (!apiContext.unloginPull) return;
|
|
410
387
|
const baseStore = resolvedStores[0];
|
|
388
|
+
let blocks;
|
|
411
389
|
try {
|
|
412
|
-
|
|
413
|
-
for (const block of blocks) {
|
|
414
|
-
for (const mod of block.modules ?? []) {
|
|
415
|
-
const key = buildKey(mod.moduleCode, block.langCode, baseStore);
|
|
416
|
-
const existing = await storage.getRecord(baseStore.name, key);
|
|
417
|
-
const parsed = parseI18nValues(mod.i18nValues ?? []);
|
|
418
|
-
const resources = deepMerge(
|
|
419
|
-
existing?.resources ?? {},
|
|
420
|
-
flatToNested(parsed)
|
|
421
|
-
);
|
|
422
|
-
await storage.putRecord(baseStore.name, {
|
|
423
|
-
key,
|
|
424
|
-
moduleCode: mod.moduleCode,
|
|
425
|
-
langCode: block.langCode,
|
|
426
|
-
version: mod.version ?? 0,
|
|
427
|
-
resources
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
390
|
+
blocks = await doFetch(apiContext.unloginPull, options);
|
|
431
391
|
} catch {
|
|
392
|
+
await injectCurrentLanguageModules(currentLang);
|
|
393
|
+
await i18n.changeLanguage(currentLang);
|
|
394
|
+
emitI18nResourcesUpdated();
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
for (const block of blocks) {
|
|
398
|
+
for (const mod of block.modules ?? []) {
|
|
399
|
+
const key = buildKey(mod.moduleCode, block.langCode, baseStore);
|
|
400
|
+
const existing = await storage.getRecord(baseStore.name, key);
|
|
401
|
+
const parsed = parseI18nValues(mod.i18nValues ?? []);
|
|
402
|
+
const resources = deepMerge(
|
|
403
|
+
existing?.resources ?? {},
|
|
404
|
+
flatToNested(parsed)
|
|
405
|
+
);
|
|
406
|
+
await storage.putRecord(baseStore.name, {
|
|
407
|
+
key,
|
|
408
|
+
moduleCode: mod.moduleCode,
|
|
409
|
+
langCode: block.langCode,
|
|
410
|
+
version: mod.version ?? 0,
|
|
411
|
+
resources
|
|
412
|
+
});
|
|
413
|
+
}
|
|
432
414
|
}
|
|
433
415
|
await injectCurrentLanguageModules(currentLang);
|
|
434
416
|
await i18n.changeLanguage(currentLang);
|
|
@@ -455,7 +437,7 @@ async function startWorkerFull(options) {
|
|
|
455
437
|
function runWorker(options, tasks, pull) {
|
|
456
438
|
return new Promise((resolve, reject) => {
|
|
457
439
|
const { apiContext } = options;
|
|
458
|
-
const
|
|
440
|
+
const storage = managerOptions.storage;
|
|
459
441
|
workerManager.start(
|
|
460
442
|
managerOptions.createWorker,
|
|
461
443
|
{
|
|
@@ -463,20 +445,45 @@ function runWorker(options, tasks, pull) {
|
|
|
463
445
|
headers: apiContext.getHeaders(),
|
|
464
446
|
pullPath: pull.path,
|
|
465
447
|
pullMethod: pull.method ?? "GET",
|
|
466
|
-
|
|
467
|
-
tasks,
|
|
468
|
-
dbName: DB_NAME
|
|
448
|
+
tasks
|
|
469
449
|
},
|
|
470
450
|
{
|
|
471
|
-
|
|
472
|
-
|
|
451
|
+
onRawData: async (task, raw) => {
|
|
452
|
+
const blocks = pull.transform ? pull.transform(raw) : raw;
|
|
453
|
+
const store = resolvedStores.find((s) => s.name === task.storeName);
|
|
454
|
+
if (!store) return;
|
|
455
|
+
const storeIndex = resolvedStores.indexOf(store);
|
|
456
|
+
for (const block of blocks) {
|
|
457
|
+
for (const mod of block.modules ?? []) {
|
|
458
|
+
const key = buildKey(mod.moduleCode, block.langCode, store);
|
|
459
|
+
const existing = await storage.getRecord(store.name, key);
|
|
460
|
+
if (existing && mod.version <= existing.version) continue;
|
|
461
|
+
const parsed = parseI18nValues(mod.i18nValues ?? []);
|
|
462
|
+
const resources = storeIndex === 0 ? deepMerge(
|
|
463
|
+
existing?.resources ?? {},
|
|
464
|
+
flatToNested(parsed)
|
|
465
|
+
) : { ...existing?.resources ?? {}, ...parsed };
|
|
466
|
+
await storage.putRecord(store.name, {
|
|
467
|
+
key,
|
|
468
|
+
moduleCode: mod.moduleCode,
|
|
469
|
+
langCode: block.langCode,
|
|
470
|
+
version: mod.version,
|
|
471
|
+
resources
|
|
472
|
+
});
|
|
473
|
+
await onModuleLoaded({
|
|
474
|
+
storeName: task.storeName,
|
|
475
|
+
langCode: block.langCode,
|
|
476
|
+
moduleCode: mod.moduleCode,
|
|
477
|
+
version: mod.version
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
}
|
|
473
481
|
},
|
|
474
482
|
onModuleError: (_data) => {
|
|
475
483
|
},
|
|
476
484
|
onDone: resolve,
|
|
477
485
|
onError: () => reject(new Error("[i18n] Worker fatal error"))
|
|
478
|
-
}
|
|
479
|
-
workerWritesDB ? void 0 : managerOptions.storage
|
|
486
|
+
}
|
|
480
487
|
);
|
|
481
488
|
});
|
|
482
489
|
}
|
|
@@ -645,7 +652,7 @@ async function doFetch(config, options, internalParams) {
|
|
|
645
652
|
}
|
|
646
653
|
function buildSig(options) {
|
|
647
654
|
const cgKeys = options.stores.map((s) => s.cacheGroupKey ?? "").join(",");
|
|
648
|
-
return [options.apiContext.baseURL, options.
|
|
655
|
+
return [options.apiContext.baseURL, options.langCode, cgKeys].join("|");
|
|
649
656
|
}
|
|
650
657
|
function sortedModules() {
|
|
651
658
|
const priority = PRIORITY_MODULES.filter((m) => resolvedModules.includes(m));
|
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
@@ -7,7 +7,7 @@ var _chunkAJJKJPNBcjs = require('./chunk-AJJKJPNB.cjs');
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var _chunkJPKSBSEOcjs = require('./chunk-JPKSBSEO.cjs');
|
|
11
11
|
|
|
12
12
|
// src/index.ts
|
|
13
13
|
var _i18next = require('i18next'); var _i18next2 = _interopRequireDefault(_i18next);
|
|
@@ -16,7 +16,6 @@ var _reacti18next = require('react-i18next');
|
|
|
16
16
|
// src/storage/indexeddb.ts
|
|
17
17
|
var DB_NAME = "i18n_cache";
|
|
18
18
|
var SCHEMA_VERSION = 2;
|
|
19
|
-
var META_STORE = "i18n_meta";
|
|
20
19
|
function resourceStoreName(name) {
|
|
21
20
|
return `i18n_resources_${name}`;
|
|
22
21
|
}
|
|
@@ -36,9 +35,6 @@ function openDB(storeNames) {
|
|
|
36
35
|
s.createIndex("by_language", "langCode");
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
|
-
if (!db.objectStoreNames.contains(META_STORE)) {
|
|
40
|
-
db.createObjectStore(META_STORE, { keyPath: "key" });
|
|
41
|
-
}
|
|
42
38
|
};
|
|
43
39
|
request.onsuccess = () => {
|
|
44
40
|
const db = request.result;
|
|
@@ -86,26 +82,9 @@ async function withRetry(fn) {
|
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
84
|
var IndexedDBAdapter = class {
|
|
89
|
-
async
|
|
85
|
+
async ensureStores(stores) {
|
|
90
86
|
dbStoreNames = stores.map((s) => s.name);
|
|
91
|
-
|
|
92
|
-
const stored = await this.getMeta("schema_version");
|
|
93
|
-
const storedVersion = Number(_nullishCoalesce(stored, () => ( 0)));
|
|
94
|
-
if (storedVersion === 0) {
|
|
95
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
96
|
-
} else if (storedVersion < SCHEMA_VERSION) {
|
|
97
|
-
db.close();
|
|
98
|
-
dbInstance = null;
|
|
99
|
-
await new Promise((resolve, reject) => {
|
|
100
|
-
const req = indexedDB.deleteDatabase(DB_NAME);
|
|
101
|
-
req.onsuccess = () => resolve();
|
|
102
|
-
req.onerror = () => reject(req.error);
|
|
103
|
-
});
|
|
104
|
-
dbInstance = await openDB(dbStoreNames);
|
|
105
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
106
|
-
} else if (storedVersion > SCHEMA_VERSION) {
|
|
107
|
-
throw new Error(`[i18n] DB schema version ${storedVersion} > current ${SCHEMA_VERSION}`);
|
|
108
|
-
}
|
|
87
|
+
await getDB(dbStoreNames);
|
|
109
88
|
}
|
|
110
89
|
async getRecord(storeName, key) {
|
|
111
90
|
return withRetry(async () => {
|
|
@@ -138,23 +117,6 @@ var IndexedDBAdapter = class {
|
|
|
138
117
|
await idbRequest(tx.objectStore(resourceStoreName(storeName)).clear());
|
|
139
118
|
});
|
|
140
119
|
}
|
|
141
|
-
async getMeta(key) {
|
|
142
|
-
return withRetry(async () => {
|
|
143
|
-
const db = await getDB();
|
|
144
|
-
const tx = db.transaction(META_STORE, "readonly");
|
|
145
|
-
const result = await idbRequest(
|
|
146
|
-
tx.objectStore(META_STORE).get(key)
|
|
147
|
-
);
|
|
148
|
-
return _nullishCoalesce(_optionalChain([result, 'optionalAccess', _ => _.value]), () => ( null));
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
async setMeta(key, value) {
|
|
152
|
-
await withRetry(async () => {
|
|
153
|
-
const db = await getDB();
|
|
154
|
-
const tx = db.transaction(META_STORE, "readwrite");
|
|
155
|
-
await idbRequest(tx.objectStore(META_STORE).put({ key, value }));
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
120
|
async hasLoginResources(stores) {
|
|
159
121
|
return withRetry(async () => {
|
|
160
122
|
const db = await getDB();
|
|
@@ -171,7 +133,7 @@ var IndexedDBAdapter = class {
|
|
|
171
133
|
return;
|
|
172
134
|
}
|
|
173
135
|
const record = cursor.value;
|
|
174
|
-
if (!record.key.startsWith("UNLOGIN_")) {
|
|
136
|
+
if (!record.key.startsWith("UNLOGIN_") && !record.key.startsWith("__meta__:")) {
|
|
175
137
|
resolve(true);
|
|
176
138
|
return;
|
|
177
139
|
}
|
|
@@ -203,9 +165,8 @@ if (!_i18next2.default.isInitialized) {
|
|
|
203
165
|
}
|
|
204
166
|
});
|
|
205
167
|
}
|
|
206
|
-
var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } =
|
|
168
|
+
var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } = _chunkJPKSBSEOcjs.createI18nManager.call(void 0, {
|
|
207
169
|
storage: new IndexedDBAdapter(),
|
|
208
|
-
workerWritesDB: true,
|
|
209
170
|
createWorker: () => new (0, _chunkAJJKJPNBcjs.WebWorkerAdapter)(
|
|
210
171
|
new Worker(new URL("./workers/preload-worker.js", import.meta.url), { type: "module" })
|
|
211
172
|
)
|
|
@@ -220,4 +181,4 @@ var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } =
|
|
|
220
181
|
|
|
221
182
|
|
|
222
183
|
|
|
223
|
-
exports.I18N_RESOURCES_UPDATED_EVENT =
|
|
184
|
+
exports.I18N_RESOURCES_UPDATED_EVENT = _chunkJPKSBSEOcjs.I18N_RESOURCES_UPDATED_EVENT; exports.closeSSE = closeSSE; exports.emitI18nResourcesUpdated = _chunkJPKSBSEOcjs.emitI18nResourcesUpdated; exports.ensureModules = ensureModules; exports.getAllRecordsByModule = getAllRecordsByModule; exports.getResource = getResource; exports.initI18n = initI18n; exports.useDict = _chunkAJJKJPNBcjs.useDict; exports.useTranslation = _chunkAJJKJPNBcjs.useTranslation;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from './cacheEvents-
|
|
2
|
-
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, W as WorkerLike, h as emitI18nResourcesUpdated } from './cacheEvents-
|
|
1
|
+
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from './cacheEvents-CSwgzbob.cjs';
|
|
2
|
+
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, W as WorkerLike, h as emitI18nResourcesUpdated } from './cacheEvents-CSwgzbob.cjs';
|
|
3
3
|
export { u as useDict } from './hooks-ClO29Chr.cjs';
|
|
4
4
|
export { useTranslation } from 'react-i18next';
|
|
5
5
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from './cacheEvents-
|
|
2
|
-
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, W as WorkerLike, h as emitI18nResourcesUpdated } from './cacheEvents-
|
|
1
|
+
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from './cacheEvents-CSwgzbob.js';
|
|
2
|
+
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, W as WorkerLike, h as emitI18nResourcesUpdated } from './cacheEvents-CSwgzbob.js';
|
|
3
3
|
export { u as useDict } from './hooks-ClO29Chr.js';
|
|
4
4
|
export { useTranslation } from 'react-i18next';
|
|
5
5
|
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
I18N_RESOURCES_UPDATED_EVENT,
|
|
8
8
|
createI18nManager,
|
|
9
9
|
emitI18nResourcesUpdated
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-X6D2MZ7M.js";
|
|
11
11
|
|
|
12
12
|
// src/index.ts
|
|
13
13
|
import i18n from "i18next";
|
|
@@ -16,7 +16,6 @@ import { initReactI18next } from "react-i18next";
|
|
|
16
16
|
// src/storage/indexeddb.ts
|
|
17
17
|
var DB_NAME = "i18n_cache";
|
|
18
18
|
var SCHEMA_VERSION = 2;
|
|
19
|
-
var META_STORE = "i18n_meta";
|
|
20
19
|
function resourceStoreName(name) {
|
|
21
20
|
return `i18n_resources_${name}`;
|
|
22
21
|
}
|
|
@@ -36,9 +35,6 @@ function openDB(storeNames) {
|
|
|
36
35
|
s.createIndex("by_language", "langCode");
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
|
-
if (!db.objectStoreNames.contains(META_STORE)) {
|
|
40
|
-
db.createObjectStore(META_STORE, { keyPath: "key" });
|
|
41
|
-
}
|
|
42
38
|
};
|
|
43
39
|
request.onsuccess = () => {
|
|
44
40
|
const db = request.result;
|
|
@@ -86,26 +82,9 @@ async function withRetry(fn) {
|
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
84
|
var IndexedDBAdapter = class {
|
|
89
|
-
async
|
|
85
|
+
async ensureStores(stores) {
|
|
90
86
|
dbStoreNames = stores.map((s) => s.name);
|
|
91
|
-
|
|
92
|
-
const stored = await this.getMeta("schema_version");
|
|
93
|
-
const storedVersion = Number(stored ?? 0);
|
|
94
|
-
if (storedVersion === 0) {
|
|
95
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
96
|
-
} else if (storedVersion < SCHEMA_VERSION) {
|
|
97
|
-
db.close();
|
|
98
|
-
dbInstance = null;
|
|
99
|
-
await new Promise((resolve, reject) => {
|
|
100
|
-
const req = indexedDB.deleteDatabase(DB_NAME);
|
|
101
|
-
req.onsuccess = () => resolve();
|
|
102
|
-
req.onerror = () => reject(req.error);
|
|
103
|
-
});
|
|
104
|
-
dbInstance = await openDB(dbStoreNames);
|
|
105
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
106
|
-
} else if (storedVersion > SCHEMA_VERSION) {
|
|
107
|
-
throw new Error(`[i18n] DB schema version ${storedVersion} > current ${SCHEMA_VERSION}`);
|
|
108
|
-
}
|
|
87
|
+
await getDB(dbStoreNames);
|
|
109
88
|
}
|
|
110
89
|
async getRecord(storeName, key) {
|
|
111
90
|
return withRetry(async () => {
|
|
@@ -138,23 +117,6 @@ var IndexedDBAdapter = class {
|
|
|
138
117
|
await idbRequest(tx.objectStore(resourceStoreName(storeName)).clear());
|
|
139
118
|
});
|
|
140
119
|
}
|
|
141
|
-
async getMeta(key) {
|
|
142
|
-
return withRetry(async () => {
|
|
143
|
-
const db = await getDB();
|
|
144
|
-
const tx = db.transaction(META_STORE, "readonly");
|
|
145
|
-
const result = await idbRequest(
|
|
146
|
-
tx.objectStore(META_STORE).get(key)
|
|
147
|
-
);
|
|
148
|
-
return result?.value ?? null;
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
async setMeta(key, value) {
|
|
152
|
-
await withRetry(async () => {
|
|
153
|
-
const db = await getDB();
|
|
154
|
-
const tx = db.transaction(META_STORE, "readwrite");
|
|
155
|
-
await idbRequest(tx.objectStore(META_STORE).put({ key, value }));
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
120
|
async hasLoginResources(stores) {
|
|
159
121
|
return withRetry(async () => {
|
|
160
122
|
const db = await getDB();
|
|
@@ -171,7 +133,7 @@ var IndexedDBAdapter = class {
|
|
|
171
133
|
return;
|
|
172
134
|
}
|
|
173
135
|
const record = cursor.value;
|
|
174
|
-
if (!record.key.startsWith("UNLOGIN_")) {
|
|
136
|
+
if (!record.key.startsWith("UNLOGIN_") && !record.key.startsWith("__meta__:")) {
|
|
175
137
|
resolve(true);
|
|
176
138
|
return;
|
|
177
139
|
}
|
|
@@ -205,7 +167,6 @@ if (!i18n.isInitialized) {
|
|
|
205
167
|
}
|
|
206
168
|
var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } = createI18nManager({
|
|
207
169
|
storage: new IndexedDBAdapter(),
|
|
208
|
-
workerWritesDB: true,
|
|
209
170
|
createWorker: () => new WebWorkerAdapter(
|
|
210
171
|
new Worker(new URL("./workers/preload-worker.js", import.meta.url), { type: "module" })
|
|
211
172
|
)
|
package/dist/mp/index.cjs
CHANGED
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunkJPKSBSEOcjs = require('../chunk-JPKSBSEO.cjs');
|
|
6
6
|
|
|
7
7
|
// src/storage/wx-sqlite.ts
|
|
8
8
|
var DB_NAME = "i18n_cache";
|
|
9
|
-
var SCHEMA_VERSION = 2;
|
|
10
9
|
function resourceTable(name) {
|
|
11
10
|
return `i18n_resources_${name}`;
|
|
12
11
|
}
|
|
@@ -40,40 +39,17 @@ function exec(db, sql, args = []) {
|
|
|
40
39
|
});
|
|
41
40
|
}
|
|
42
41
|
var WxSQLiteAdapter = class {
|
|
43
|
-
async
|
|
44
|
-
const db = await getDB();
|
|
45
|
-
await exec(db, `
|
|
46
|
-
CREATE TABLE IF NOT EXISTS i18n_meta (
|
|
47
|
-
key TEXT PRIMARY KEY,
|
|
48
|
-
value TEXT NOT NULL
|
|
49
|
-
)
|
|
50
|
-
`);
|
|
51
|
-
const stored = await this.getMeta("schema_version");
|
|
52
|
-
const storedVersion = Number(_nullishCoalesce(stored, () => ( 0)));
|
|
53
|
-
if (storedVersion === 0) {
|
|
54
|
-
await this.createTables(stores);
|
|
55
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
56
|
-
} else if (storedVersion < SCHEMA_VERSION) {
|
|
57
|
-
for (const store of stores) {
|
|
58
|
-
await exec(db, `DROP TABLE IF EXISTS ${resourceTable(store.name)}`);
|
|
59
|
-
}
|
|
60
|
-
await this.createTables(stores);
|
|
61
|
-
await this.setMeta("schema_version", String(SCHEMA_VERSION));
|
|
62
|
-
} else if (storedVersion > SCHEMA_VERSION) {
|
|
63
|
-
throw new Error(`[i18n] wx.openDatabase schema version ${storedVersion} > current ${SCHEMA_VERSION}`);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async createTables(stores) {
|
|
42
|
+
async ensureStores(stores) {
|
|
67
43
|
const db = await getDB();
|
|
68
44
|
for (const store of stores) {
|
|
69
45
|
const table = resourceTable(store.name);
|
|
70
46
|
await exec(db, `
|
|
71
47
|
CREATE TABLE IF NOT EXISTS ${table} (
|
|
72
|
-
key
|
|
48
|
+
key TEXT PRIMARY KEY,
|
|
73
49
|
module_code TEXT NOT NULL,
|
|
74
|
-
lang_code
|
|
75
|
-
version
|
|
76
|
-
resources
|
|
50
|
+
lang_code TEXT NOT NULL,
|
|
51
|
+
version INTEGER NOT NULL DEFAULT 0,
|
|
52
|
+
resources TEXT NOT NULL
|
|
77
53
|
)
|
|
78
54
|
`);
|
|
79
55
|
await exec(
|
|
@@ -129,23 +105,13 @@ var WxSQLiteAdapter = class {
|
|
|
129
105
|
const db = await getDB();
|
|
130
106
|
await exec(db, `DELETE FROM ${resourceTable(storeName)}`);
|
|
131
107
|
}
|
|
132
|
-
async getMeta(key) {
|
|
133
|
-
const db = await getDB();
|
|
134
|
-
const result = await exec(db, "SELECT value FROM i18n_meta WHERE key = ?", [key]);
|
|
135
|
-
if (result.rows.length === 0) return null;
|
|
136
|
-
return result.rows.item(0).value;
|
|
137
|
-
}
|
|
138
|
-
async setMeta(key, value) {
|
|
139
|
-
const db = await getDB();
|
|
140
|
-
await exec(db, "INSERT OR REPLACE INTO i18n_meta (key, value) VALUES (?, ?)", [key, value]);
|
|
141
|
-
}
|
|
142
108
|
async hasLoginResources(stores) {
|
|
143
109
|
const db = await getDB();
|
|
144
110
|
for (const store of stores) {
|
|
145
111
|
const result = await exec(
|
|
146
112
|
db,
|
|
147
113
|
`SELECT key FROM ${resourceTable(store.name)}
|
|
148
|
-
WHERE key NOT LIKE 'UNLOGIN_%' LIMIT 1`
|
|
114
|
+
WHERE key NOT LIKE 'UNLOGIN_%' AND key NOT LIKE '__meta__:%' LIMIT 1`
|
|
149
115
|
);
|
|
150
116
|
if (result.rows.length > 0) return true;
|
|
151
117
|
}
|
|
@@ -215,7 +181,7 @@ function wxFetch(input, init) {
|
|
|
215
181
|
}
|
|
216
182
|
|
|
217
183
|
// src/mp/index.ts
|
|
218
|
-
var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } =
|
|
184
|
+
var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } = _chunkJPKSBSEOcjs.createI18nManager.call(void 0, {
|
|
219
185
|
storage: new WxSQLiteAdapter(),
|
|
220
186
|
createWorker: () => new WxWorkerAdapter(wx.createWorker("workers/preload-worker-mp.js")),
|
|
221
187
|
fetchFn: wxFetch
|
|
@@ -228,4 +194,4 @@ var { initI18n, closeSSE, ensureModules, getResource, getAllRecordsByModule } =
|
|
|
228
194
|
|
|
229
195
|
|
|
230
196
|
|
|
231
|
-
exports.I18N_RESOURCES_UPDATED_EVENT =
|
|
197
|
+
exports.I18N_RESOURCES_UPDATED_EVENT = _chunkJPKSBSEOcjs.I18N_RESOURCES_UPDATED_EVENT; exports.closeSSE = closeSSE; exports.emitI18nResourcesUpdated = _chunkJPKSBSEOcjs.emitI18nResourcesUpdated; exports.ensureModules = ensureModules; exports.getAllRecordsByModule = getAllRecordsByModule; exports.getResource = getResource; exports.initI18n = initI18n;
|
package/dist/mp/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from '../cacheEvents-
|
|
2
|
-
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, h as emitI18nResourcesUpdated } from '../cacheEvents-
|
|
1
|
+
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from '../cacheEvents-CSwgzbob.cjs';
|
|
2
|
+
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, h as emitI18nResourcesUpdated } from '../cacheEvents-CSwgzbob.cjs';
|
|
3
3
|
|
|
4
4
|
declare const initI18n: typeof initI18n$1;
|
|
5
5
|
declare const closeSSE: typeof closeSSE$1;
|
package/dist/mp/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from '../cacheEvents-
|
|
2
|
-
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, h as emitI18nResourcesUpdated } from '../cacheEvents-
|
|
1
|
+
import { c as closeSSE$1, e as ensureModules$1, g as getAllRecordsByModule$1, a as getResource$1, i as initI18n$1 } from '../cacheEvents-CSwgzbob.js';
|
|
2
|
+
export { I as I18N_RESOURCES_UPDATED_EVENT, b as I18nInitOptions, P as PullLangBlock, R as ResourceRecord, S as SSEMessage, d as StandardPullParams, f as StoreConfig, h as emitI18nResourcesUpdated } from '../cacheEvents-CSwgzbob.js';
|
|
3
3
|
|
|
4
4
|
declare const initI18n: typeof initI18n$1;
|
|
5
5
|
declare const closeSSE: typeof closeSSE$1;
|