@astroscope/i18n 0.5.0 → 0.6.1
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/babel-plugin-C8O9spUS.js +120 -0
- package/dist/babel-plugin-C8O9spUS.js.map +1 -0
- package/dist/client/directives/idle.d.ts +4 -9
- package/dist/client/directives/idle.d.ts.map +1 -0
- package/dist/client/directives/idle.js +13 -21
- package/dist/client/directives/idle.js.map +1 -0
- package/dist/client/directives/load.d.ts +4 -9
- package/dist/client/directives/load.d.ts.map +1 -0
- package/dist/client/directives/load.js +9 -14
- package/dist/client/directives/load.js.map +1 -0
- package/dist/client/directives/media.d.ts +4 -9
- package/dist/client/directives/media.d.ts.map +1 -0
- package/dist/client/directives/media.js +16 -24
- package/dist/client/directives/media.js.map +1 -0
- package/dist/client/directives/only.d.ts +4 -11
- package/dist/client/directives/only.d.ts.map +1 -0
- package/dist/client/directives/only.js +9 -14
- package/dist/client/directives/only.js.map +1 -0
- package/dist/client/directives/utils.d.ts +3 -4
- package/dist/client/directives/utils.d.ts.map +1 -0
- package/dist/client/directives/utils.js +32 -7
- package/dist/client/directives/utils.js.map +1 -0
- package/dist/client/directives/visible.d.ts +4 -9
- package/dist/client/directives/visible.d.ts.map +1 -0
- package/dist/client/directives/visible.js +22 -29
- package/dist/client/directives/visible.js.map +1 -0
- package/dist/client/translate.d.ts +5 -4
- package/dist/client/translate.d.ts.map +1 -0
- package/dist/client/translate.js +68 -41
- package/dist/client/translate.js.map +1 -0
- package/dist/compiler-CkOVGWU5.js +48 -0
- package/dist/compiler-CkOVGWU5.js.map +1 -0
- package/dist/context-DT91WgEO.js +247 -0
- package/dist/context-DT91WgEO.js.map +1 -0
- package/dist/extraction/index.d.ts +12 -17
- package/dist/extraction/index.d.ts.map +1 -0
- package/dist/extraction/index.js +3 -11
- package/dist/i18n-DzmH9JmE.d.ts +79 -0
- package/dist/i18n-DzmH9JmE.d.ts.map +1 -0
- package/dist/index.d.ts +37 -33
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +163 -148
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +6 -6
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +2 -10
- package/dist/manifest-D883_BIS.js +43 -0
- package/dist/manifest-D883_BIS.js.map +1 -0
- package/dist/rich-CHisXE-A.js +87 -0
- package/dist/rich-CHisXE-A.js.map +1 -0
- package/dist/{rich-CQUhdwBc.d.ts → rich-CqNMP3tk.d.ts} +4 -2
- package/dist/rich-CqNMP3tk.d.ts.map +1 -0
- package/dist/server/translate.d.ts +5 -3
- package/dist/server/translate.d.ts.map +1 -0
- package/dist/server/translate.js +75 -64
- package/dist/server/translate.js.map +1 -0
- package/dist/state-DeudhdeT.js +20 -0
- package/dist/state-DeudhdeT.js.map +1 -0
- package/dist/{types-ChMT6Vbd.d.ts → types-BfyoJsoB.d.ts} +11 -10
- package/dist/types-BfyoJsoB.d.ts.map +1 -0
- package/dist/types-Bo4jMK8g.d.ts +20 -0
- package/dist/types-Bo4jMK8g.d.ts.map +1 -0
- package/dist/{types-DNRFj1XA.d.ts → types-C98CcjtN.d.ts} +13 -11
- package/dist/types-C98CcjtN.d.ts.map +1 -0
- package/dist/url-B8334XSD.js +36 -0
- package/dist/url-B8334XSD.js.map +1 -0
- package/dist/vite-plugin-DVUlcvEH.js +421 -0
- package/dist/vite-plugin-DVUlcvEH.js.map +1 -0
- package/package.json +13 -12
- package/dist/babel-plugin-GOB6FMZ2.js +0 -119
- package/dist/chunk-33JVAFGQ.js +0 -44
- package/dist/chunk-4A5BKHFN.js +0 -16
- package/dist/chunk-7MSGHEST.js +0 -31
- package/dist/chunk-FYKC3DAZ.js +0 -36
- package/dist/chunk-LXW5WYBS.js +0 -298
- package/dist/chunk-PACQPCSW.js +0 -479
- package/dist/chunk-VGPVG5JP.js +0 -73
- package/dist/i18n-B--L7OQk.d.ts +0 -76
- package/dist/types-B_8vAmvv.d.ts +0 -18
- package/dist/types-BeDFfFIL.d.ts +0 -7
package/dist/chunk-33JVAFGQ.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// src/shared/compiler.ts
|
|
2
|
-
import { MessageFormat } from "messageformat";
|
|
3
|
-
import { DraftFunctions } from "messageformat/functions";
|
|
4
|
-
var mfOptions = { functions: DraftFunctions };
|
|
5
|
-
var cache = /* @__PURE__ */ new Map();
|
|
6
|
-
function getOrCreateMessageFormat(locale, message) {
|
|
7
|
-
const cacheKey = `${locale}:${message}`;
|
|
8
|
-
const cached = cache.get(cacheKey);
|
|
9
|
-
if (cached) {
|
|
10
|
-
return cached;
|
|
11
|
-
}
|
|
12
|
-
const mf = new MessageFormat(locale, message, mfOptions);
|
|
13
|
-
cache.set(cacheKey, mf);
|
|
14
|
-
return mf;
|
|
15
|
-
}
|
|
16
|
-
function compileMessage(locale, message) {
|
|
17
|
-
try {
|
|
18
|
-
const mf = getOrCreateMessageFormat(locale, message);
|
|
19
|
-
return (values) => mf.format(values);
|
|
20
|
-
} catch {
|
|
21
|
-
return () => message;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function formatMessageToParts(locale, message, values) {
|
|
25
|
-
try {
|
|
26
|
-
const mf = getOrCreateMessageFormat(locale, message);
|
|
27
|
-
return mf.formatToParts(values);
|
|
28
|
-
} catch {
|
|
29
|
-
return [{ type: "literal", value: message }];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function compileTranslations(locale, raw) {
|
|
33
|
-
const compiled = {};
|
|
34
|
-
for (const [key, message] of Object.entries(raw)) {
|
|
35
|
-
compiled[key] = compileMessage(locale, message);
|
|
36
|
-
}
|
|
37
|
-
return compiled;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export {
|
|
41
|
-
compileMessage,
|
|
42
|
-
formatMessageToParts,
|
|
43
|
-
compileTranslations
|
|
44
|
-
};
|
package/dist/chunk-4A5BKHFN.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// src/shared/url.ts
|
|
2
|
-
function chunkIdToName(chunkId) {
|
|
3
|
-
return chunkId.replace(/^_astro\//, "");
|
|
4
|
-
}
|
|
5
|
-
function componentUrlToChunkName(componentUrl) {
|
|
6
|
-
return componentUrl.replace(/^\/_astro\//, "").replace(/\.js$/, "");
|
|
7
|
-
}
|
|
8
|
-
function buildI18nChunkUrl(locale, chunkId, hash) {
|
|
9
|
-
return `/_i18n/${locale}/${chunkIdToName(chunkId)}.${hash}.js`;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export {
|
|
13
|
-
chunkIdToName,
|
|
14
|
-
componentUrlToChunkName,
|
|
15
|
-
buildI18nChunkUrl
|
|
16
|
-
};
|
package/dist/chunk-7MSGHEST.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
buildI18nChunkUrl,
|
|
3
|
-
componentUrlToChunkName
|
|
4
|
-
} from "./chunk-4A5BKHFN.js";
|
|
5
|
-
|
|
6
|
-
// src/client/directives/utils.ts
|
|
7
|
-
function prefetchTranslation(chunkName, i18n) {
|
|
8
|
-
const hash = i18n.hashes[chunkName];
|
|
9
|
-
if (hash) {
|
|
10
|
-
const url = buildI18nChunkUrl(i18n.locale, chunkName, hash);
|
|
11
|
-
import(
|
|
12
|
-
/* @vite-ignore */
|
|
13
|
-
url
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
function warmUpI18nForChunk(el) {
|
|
18
|
-
const componentUrl = el.getAttribute("component-url");
|
|
19
|
-
if (!componentUrl) return;
|
|
20
|
-
const chunkName = componentUrlToChunkName(componentUrl);
|
|
21
|
-
const i18n = window.__i18n__;
|
|
22
|
-
prefetchTranslation(chunkName, i18n);
|
|
23
|
-
const deps = i18n.imports[chunkName] ?? [];
|
|
24
|
-
for (const dep of deps) {
|
|
25
|
-
prefetchTranslation(dep, i18n);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export {
|
|
30
|
-
warmUpI18nForChunk
|
|
31
|
-
};
|
package/dist/chunk-FYKC3DAZ.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
// src/extraction/manifest.ts
|
|
2
|
-
import path from "path";
|
|
3
|
-
var I18N_MANIFEST_GLOBAL_KEY = "__astroscope_i18n_manifest__";
|
|
4
|
-
function getGlobalState() {
|
|
5
|
-
const g = globalThis;
|
|
6
|
-
if (!g[I18N_MANIFEST_GLOBAL_KEY]) {
|
|
7
|
-
g[I18N_MANIFEST_GLOBAL_KEY] = {
|
|
8
|
-
extractedKeys: [],
|
|
9
|
-
chunkManifest: {},
|
|
10
|
-
importsManifest: {},
|
|
11
|
-
projectRoot: "",
|
|
12
|
-
version: 0
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
return g[I18N_MANIFEST_GLOBAL_KEY];
|
|
16
|
-
}
|
|
17
|
-
function getManifest() {
|
|
18
|
-
const state = getGlobalState();
|
|
19
|
-
const keys = state.extractedKeys.map((key) => ({
|
|
20
|
-
...key,
|
|
21
|
-
// make file paths relative to project root for better readability
|
|
22
|
-
// and avoiding leaking absolute paths from dev or ci environments
|
|
23
|
-
files: key.files.map((fileLocation) => {
|
|
24
|
-
const [filePath, line] = fileLocation.split(":");
|
|
25
|
-
if (!filePath) return fileLocation;
|
|
26
|
-
const relativePath = state.projectRoot ? path.relative(state.projectRoot, filePath) : filePath;
|
|
27
|
-
return line ? `${relativePath}:${line}` : relativePath;
|
|
28
|
-
})
|
|
29
|
-
}));
|
|
30
|
-
return { keys, chunks: state.chunkManifest, imports: state.importsManifest };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
getGlobalState,
|
|
35
|
-
getManifest
|
|
36
|
-
};
|
package/dist/chunk-LXW5WYBS.js
DELETED
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
compileTranslations
|
|
3
|
-
} from "./chunk-33JVAFGQ.js";
|
|
4
|
-
import {
|
|
5
|
-
getGlobalState
|
|
6
|
-
} from "./chunk-FYKC3DAZ.js";
|
|
7
|
-
|
|
8
|
-
// src/extraction/hash.ts
|
|
9
|
-
import { createHash } from "crypto";
|
|
10
|
-
function hashString(str) {
|
|
11
|
-
return createHash("sha256").update(str).digest("hex").substring(0, 8);
|
|
12
|
-
}
|
|
13
|
-
function computeChunkHash(translations, keys) {
|
|
14
|
-
const relevantTranslations = {};
|
|
15
|
-
for (const key of keys.sort()) {
|
|
16
|
-
if (translations[key]) {
|
|
17
|
-
relevantTranslations[key] = translations[key];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return hashString(JSON.stringify(relevantTranslations));
|
|
21
|
-
}
|
|
22
|
-
function computeAllChunkHashes(translations, manifest) {
|
|
23
|
-
const hashes = {};
|
|
24
|
-
for (const [chunkId, keys] of Object.entries(manifest)) {
|
|
25
|
-
hashes[chunkId] = computeChunkHash(translations, keys);
|
|
26
|
-
}
|
|
27
|
-
return hashes;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// src/server/utils.ts
|
|
31
|
-
var LOWERCASE_A = 97;
|
|
32
|
-
function indexToLetter(index) {
|
|
33
|
-
return String.fromCharCode(LOWERCASE_A + index);
|
|
34
|
-
}
|
|
35
|
-
function generateBB26(index) {
|
|
36
|
-
let name = "";
|
|
37
|
-
let remaining = index;
|
|
38
|
-
do {
|
|
39
|
-
name = indexToLetter(remaining % 26) + name;
|
|
40
|
-
remaining = Math.floor(remaining / 26) - 1;
|
|
41
|
-
} while (remaining >= 0);
|
|
42
|
-
return name;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// src/server/i18n.ts
|
|
46
|
-
var I18nSingleton = class {
|
|
47
|
-
// normalized user config with defaults applied
|
|
48
|
-
config = null;
|
|
49
|
-
// locale -> raw translation strings (before ICU compilation)
|
|
50
|
-
rawCache = /* @__PURE__ */ new Map();
|
|
51
|
-
// locale -> raw translations merged with manifest fallbacks
|
|
52
|
-
mergedCache = /* @__PURE__ */ new Map();
|
|
53
|
-
// locale -> compiled ICU MessageFormat functions
|
|
54
|
-
compiledCache = /* @__PURE__ */ new Map();
|
|
55
|
-
// locale -> chunk name -> content hash (for cache busting)
|
|
56
|
-
hashCache = /* @__PURE__ */ new Map();
|
|
57
|
-
// locale -> inline script for I18nScript component
|
|
58
|
-
scriptCache = /* @__PURE__ */ new Map();
|
|
59
|
-
// "locale:chunkName" -> encoded chunk response body
|
|
60
|
-
chunkCache = /* @__PURE__ */ new Map();
|
|
61
|
-
// manifest getter (set by init module, provides live data in dev mode)
|
|
62
|
-
manifestGetter = null;
|
|
63
|
-
// tracks the manifest version to detect when derived caches need invalidation
|
|
64
|
-
manifestVersion = 0;
|
|
65
|
-
async configure(config) {
|
|
66
|
-
if (!config.locales?.length) {
|
|
67
|
-
throw new Error("i18n.configure(): locales array is required and must not be empty");
|
|
68
|
-
}
|
|
69
|
-
for (const locale of config.locales) {
|
|
70
|
-
if (!locale) {
|
|
71
|
-
throw new Error("i18n.configure(): locale cannot be empty");
|
|
72
|
-
}
|
|
73
|
-
if (locale !== locale.trim()) {
|
|
74
|
-
throw new Error(`i18n.configure(): locale "${locale}" has leading or trailing whitespace`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (new Set(config.locales).size !== config.locales.length) {
|
|
78
|
-
throw new Error("i18n.configure(): locales array contains duplicates");
|
|
79
|
-
}
|
|
80
|
-
if (config.defaultLocale !== void 0) {
|
|
81
|
-
if (!config.defaultLocale) {
|
|
82
|
-
throw new Error("i18n.configure(): defaultLocale cannot be empty");
|
|
83
|
-
}
|
|
84
|
-
if (config.defaultLocale !== config.defaultLocale.trim()) {
|
|
85
|
-
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" has leading or trailing whitespace`);
|
|
86
|
-
}
|
|
87
|
-
if (!config.locales.includes(config.defaultLocale)) {
|
|
88
|
-
throw new Error(`i18n.configure(): defaultLocale "${config.defaultLocale}" is not in locales array`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
this.config = {
|
|
92
|
-
locales: config.locales,
|
|
93
|
-
defaultLocale: config.defaultLocale ?? config.locales[0],
|
|
94
|
-
fallback: config.fallback ?? "fallback"
|
|
95
|
-
};
|
|
96
|
-
if (!this.manifestGetter) {
|
|
97
|
-
const m = await import("virtual:@astroscope/i18n/manifest");
|
|
98
|
-
this.manifestGetter = m.getManifest;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
isConfigured() {
|
|
102
|
-
return this.config !== null;
|
|
103
|
-
}
|
|
104
|
-
getConfig() {
|
|
105
|
-
if (!this.config) {
|
|
106
|
-
throw new Error("i18n not configured. Call i18n.configure() first.");
|
|
107
|
-
}
|
|
108
|
-
return this.config;
|
|
109
|
-
}
|
|
110
|
-
setTranslations(locale, raw) {
|
|
111
|
-
this.rawCache.set(locale, raw);
|
|
112
|
-
this.mergedCache.delete(locale);
|
|
113
|
-
this.compiledCache.delete(locale);
|
|
114
|
-
this.hashCache.delete(locale);
|
|
115
|
-
this.scriptCache.delete(locale);
|
|
116
|
-
for (const key of this.chunkCache.keys()) {
|
|
117
|
-
if (key.startsWith(`${locale}:`)) {
|
|
118
|
-
this.chunkCache.delete(key);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
const { chunks } = this.getManifest();
|
|
122
|
-
if (Object.keys(chunks).length > 0) {
|
|
123
|
-
this.hashCache.set(locale, computeAllChunkHashes(raw, chunks));
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
getTranslations(locale) {
|
|
127
|
-
this.invalidateIfManifestChanged();
|
|
128
|
-
const cached = this.mergedCache.get(locale);
|
|
129
|
-
if (cached) {
|
|
130
|
-
return cached;
|
|
131
|
-
}
|
|
132
|
-
const raw = this.rawCache.get(locale) ?? {};
|
|
133
|
-
const manifest = this.manifestGetter?.() ?? { keys: [], chunks: {}, imports: {} };
|
|
134
|
-
if (manifest.keys.length === 0) {
|
|
135
|
-
return raw;
|
|
136
|
-
}
|
|
137
|
-
const withFallbacks = { ...raw };
|
|
138
|
-
for (const { key, meta } of manifest.keys) {
|
|
139
|
-
if (!(key in withFallbacks) && meta.fallback) {
|
|
140
|
-
withFallbacks[key] = meta.fallback;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
this.mergedCache.set(locale, withFallbacks);
|
|
144
|
-
return withFallbacks;
|
|
145
|
-
}
|
|
146
|
-
getCompiledTranslations(locale) {
|
|
147
|
-
const cached = this.compiledCache.get(locale);
|
|
148
|
-
if (cached) {
|
|
149
|
-
return cached;
|
|
150
|
-
}
|
|
151
|
-
const compiled = compileTranslations(locale, this.getTranslations(locale));
|
|
152
|
-
this.compiledCache.set(locale, compiled);
|
|
153
|
-
return compiled;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Get the extraction manifest (keys + chunks).
|
|
157
|
-
* - keys: translation keys extracted from codebase with metadata
|
|
158
|
-
* - chunks: mapping of chunk names to their translation keys
|
|
159
|
-
*/
|
|
160
|
-
getManifest() {
|
|
161
|
-
if (!this.manifestGetter) {
|
|
162
|
-
throw new Error("i18n manifest not initialized. Call i18n.configure() first.");
|
|
163
|
-
}
|
|
164
|
-
return this.manifestGetter();
|
|
165
|
-
}
|
|
166
|
-
getHashes(locale) {
|
|
167
|
-
return this.hashCache.get(locale) ?? {};
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* Get the encoded chunk response body for a locale/chunk combination.
|
|
171
|
-
* Cached to avoid repeated JSON.stringify and encoding on each request.
|
|
172
|
-
* Returns undefined if chunk not found in manifest.
|
|
173
|
-
*/
|
|
174
|
-
getChunkBody(locale, chunkName) {
|
|
175
|
-
const keys = this.getManifest().chunks[chunkName];
|
|
176
|
-
if (!keys) {
|
|
177
|
-
return void 0;
|
|
178
|
-
}
|
|
179
|
-
const cacheKey = `${locale}:${chunkName}`;
|
|
180
|
-
const cached = this.chunkCache.get(cacheKey);
|
|
181
|
-
if (cached) {
|
|
182
|
-
return cached;
|
|
183
|
-
}
|
|
184
|
-
const translations = this.getTranslations(locale);
|
|
185
|
-
const chunkTranslations = {};
|
|
186
|
-
for (const key of keys) {
|
|
187
|
-
if (translations[key]) {
|
|
188
|
-
chunkTranslations[key] = translations[key];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
const js = `/* @astroscope/i18n chunk: ${chunkName} */
|
|
192
|
-
(function() {
|
|
193
|
-
var i = window.__i18n__;
|
|
194
|
-
if (i) Object.assign(i.translations, ${JSON.stringify(chunkTranslations)});
|
|
195
|
-
})();
|
|
196
|
-
`;
|
|
197
|
-
const body = new TextEncoder().encode(js);
|
|
198
|
-
this.chunkCache.set(cacheKey, body);
|
|
199
|
-
return body;
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Get the inline script for I18nScript component.
|
|
203
|
-
* Cached per locale, invalidated when translations change.
|
|
204
|
-
*/
|
|
205
|
-
getClientScript(locale) {
|
|
206
|
-
const cached = this.scriptCache.get(locale);
|
|
207
|
-
if (cached) {
|
|
208
|
-
return cached;
|
|
209
|
-
}
|
|
210
|
-
const script = this.createClientScript(locale);
|
|
211
|
-
this.scriptCache.set(locale, script);
|
|
212
|
-
return script;
|
|
213
|
-
}
|
|
214
|
-
createClientScript(locale) {
|
|
215
|
-
const { chunks, imports } = this.getManifest();
|
|
216
|
-
const hasChunks = Object.keys(chunks).length > 0;
|
|
217
|
-
if (!hasChunks) {
|
|
218
|
-
const raw = this.getTranslations(locale);
|
|
219
|
-
return `window.__i18n__=${JSON.stringify({ locale, hashes: {}, imports: {}, translations: raw })};`;
|
|
220
|
-
}
|
|
221
|
-
const hashes = this.getHashes(locale);
|
|
222
|
-
const allChunks = new Set(Object.keys(hashes));
|
|
223
|
-
for (const deps of Object.values(imports)) {
|
|
224
|
-
deps.forEach((d) => allChunks.add(d));
|
|
225
|
-
}
|
|
226
|
-
const chunkToVar = /* @__PURE__ */ new Map();
|
|
227
|
-
let varIndex = 0;
|
|
228
|
-
for (const chunk of allChunks) {
|
|
229
|
-
chunkToVar.set(chunk, generateBB26(varIndex++));
|
|
230
|
-
}
|
|
231
|
-
const varDecls = [...chunkToVar.entries()].map(([chunk, v]) => `${v}=${JSON.stringify(chunk)}`).join(",");
|
|
232
|
-
const hashesObj = Object.entries(hashes).map(([chunk, hash]) => `[${chunkToVar.get(chunk)}]:${JSON.stringify(hash)}`).join(",");
|
|
233
|
-
const importsObj = Object.entries(imports).filter(([chunk]) => chunkToVar.has(chunk)).map(([chunk, deps]) => {
|
|
234
|
-
const validDeps = deps.filter((d) => chunkToVar.has(d));
|
|
235
|
-
return validDeps.length > 0 ? `[${chunkToVar.get(chunk)}]:[${validDeps.map((d) => chunkToVar.get(d)).join(",")}]` : null;
|
|
236
|
-
}).filter(Boolean).join(",");
|
|
237
|
-
return `(()=>{var ${varDecls};window.__i18n__={locale:${JSON.stringify(locale)},hashes:{${hashesObj}},imports:{${importsObj}},translations:{}};})();`;
|
|
238
|
-
}
|
|
239
|
-
/**
|
|
240
|
-
* Clear translations for specified locale(s).
|
|
241
|
-
* @param locales - Locale(s) to clear, or all if not specified
|
|
242
|
-
*/
|
|
243
|
-
clear(locales) {
|
|
244
|
-
const targets = this.resolveLocales(locales);
|
|
245
|
-
for (const locale of targets) {
|
|
246
|
-
this.rawCache.delete(locale);
|
|
247
|
-
this.mergedCache.delete(locale);
|
|
248
|
-
this.compiledCache.delete(locale);
|
|
249
|
-
this.hashCache.delete(locale);
|
|
250
|
-
this.scriptCache.delete(locale);
|
|
251
|
-
for (const key of this.chunkCache.keys()) {
|
|
252
|
-
if (key.startsWith(`${locale}:`)) {
|
|
253
|
-
this.chunkCache.delete(key);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* invalidate derived caches when the extraction manifest has changed (dev mode HMR)
|
|
260
|
-
*/
|
|
261
|
-
invalidateIfManifestChanged() {
|
|
262
|
-
const { version } = getGlobalState();
|
|
263
|
-
if (version === this.manifestVersion) {
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
this.manifestVersion = version;
|
|
267
|
-
this.mergedCache.clear();
|
|
268
|
-
this.compiledCache.clear();
|
|
269
|
-
this.scriptCache.clear();
|
|
270
|
-
this.chunkCache.clear();
|
|
271
|
-
}
|
|
272
|
-
resolveLocales(locales) {
|
|
273
|
-
if (!locales) {
|
|
274
|
-
return this.config?.locales ?? [];
|
|
275
|
-
}
|
|
276
|
-
if (typeof locales === "string") {
|
|
277
|
-
return [locales];
|
|
278
|
-
}
|
|
279
|
-
return locales;
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
var i18n = new I18nSingleton();
|
|
283
|
-
|
|
284
|
-
// src/server/context.ts
|
|
285
|
-
import { AsyncLocalStorage } from "async_hooks";
|
|
286
|
-
var als = new AsyncLocalStorage();
|
|
287
|
-
function runWithContext(context, fn) {
|
|
288
|
-
return als.run(context, fn);
|
|
289
|
-
}
|
|
290
|
-
function getContext() {
|
|
291
|
-
return als.getStore() ?? null;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
export {
|
|
295
|
-
i18n,
|
|
296
|
-
runWithContext,
|
|
297
|
-
getContext
|
|
298
|
-
};
|