@ubean/i18n 0.2.2 → 0.3.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/dist/browser.d.ts +3 -0
- package/dist/browser.js +3 -0
- package/dist/detect-BIURd4aL.js +145 -0
- package/dist/head-BzEFwnlD.js +84 -0
- package/dist/head-CT8nH37d.d.ts +36 -0
- package/dist/index.d.ts +48 -85
- package/dist/index.js +4 -447
- package/dist/paths-CJVBLGJS.d.ts +59 -0
- package/dist/routing-D4Y0l8jV.js +330 -0
- package/dist/routing.d.ts +4 -22
- package/dist/routing.js +3 -129
- package/package.json +11 -3
package/dist/index.js
CHANGED
|
@@ -1,447 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!g[I18N_STATE_KEY]) g[I18N_STATE_KEY] = {
|
|
6
|
-
registeredLocales: /* @__PURE__ */ new Map(),
|
|
7
|
-
messageCache: /* @__PURE__ */ new Map(),
|
|
8
|
-
currentLocale: "en",
|
|
9
|
-
fallbackLocale: "en",
|
|
10
|
-
localeListeners: /* @__PURE__ */ new Set(),
|
|
11
|
-
missingKeyHandlers: /* @__PURE__ */ new Set(),
|
|
12
|
-
missingKeyWarned: /* @__PURE__ */ new Set(),
|
|
13
|
-
i18nConfig: {
|
|
14
|
-
defaultLocale: "en",
|
|
15
|
-
strategy: "prefix_except_default",
|
|
16
|
-
locales: []
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
return g[I18N_STATE_KEY];
|
|
20
|
-
}
|
|
21
|
-
function notifyLocaleChange(locale) {
|
|
22
|
-
for (const fn of getI18nState().localeListeners) fn(locale);
|
|
23
|
-
}
|
|
24
|
-
function addLocaleListener(callback) {
|
|
25
|
-
getI18nState().localeListeners.add(callback);
|
|
26
|
-
return () => getI18nState().localeListeners.delete(callback);
|
|
27
|
-
}
|
|
28
|
-
function addMissingKeyHandler(handler) {
|
|
29
|
-
getI18nState().missingKeyHandlers.add(handler);
|
|
30
|
-
return () => getI18nState().missingKeyHandlers.delete(handler);
|
|
31
|
-
}
|
|
32
|
-
function notifyMissingKey(locale, key) {
|
|
33
|
-
const state = getI18nState();
|
|
34
|
-
const cacheKey = `${locale}:${key}`;
|
|
35
|
-
if (!state.missingKeyWarned.has(cacheKey)) {
|
|
36
|
-
state.missingKeyWarned.add(cacheKey);
|
|
37
|
-
for (const fn of state.missingKeyHandlers) fn(locale, key);
|
|
38
|
-
if (typeof process !== "undefined" && process.env && process.env.NODE_ENV === "development") console.warn(`[i18n] Missing key "${key}" for locale "${locale}"`);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
function invalidateCache(locale) {
|
|
42
|
-
getI18nState().messageCache.delete(locale);
|
|
43
|
-
}
|
|
44
|
-
function deepMerge(target, source) {
|
|
45
|
-
const result = { ...target };
|
|
46
|
-
for (const key of Object.keys(source)) {
|
|
47
|
-
const sourceVal = source[key];
|
|
48
|
-
const targetVal = result[key];
|
|
49
|
-
if (sourceVal && typeof sourceVal === "object" && !Array.isArray(sourceVal) && targetVal && typeof targetVal === "object" && !Array.isArray(targetVal)) result[key] = deepMerge(targetVal, sourceVal);
|
|
50
|
-
else result[key] = sourceVal;
|
|
51
|
-
}
|
|
52
|
-
return result;
|
|
53
|
-
}
|
|
54
|
-
function flattenMessages(messages, prefix = "") {
|
|
55
|
-
const result = {};
|
|
56
|
-
for (const [key, value] of Object.entries(messages)) {
|
|
57
|
-
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
58
|
-
if (typeof value === "string") result[fullKey] = value;
|
|
59
|
-
else if (value && typeof value === "object") Object.assign(result, flattenMessages(value, fullKey));
|
|
60
|
-
}
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
function getFlatMessages(locale) {
|
|
64
|
-
const state = getI18nState();
|
|
65
|
-
const cached = state.messageCache.get(locale);
|
|
66
|
-
if (cached) return cached;
|
|
67
|
-
const localeData = state.registeredLocales.get(locale);
|
|
68
|
-
if (!localeData) return {};
|
|
69
|
-
const flat = flattenMessages(localeData.messages);
|
|
70
|
-
state.messageCache.set(locale, flat);
|
|
71
|
-
return flat;
|
|
72
|
-
}
|
|
73
|
-
function getPluralCategory(count, locale) {
|
|
74
|
-
try {
|
|
75
|
-
return new Intl.PluralRules(locale).select(count);
|
|
76
|
-
} catch {
|
|
77
|
-
return count === 0 ? "zero" : count === 1 ? "one" : "other";
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
function selectPlural(template, count, locale) {
|
|
81
|
-
const parts = template.split("|").map((p) => p.trim());
|
|
82
|
-
if (parts.length === 1) return template;
|
|
83
|
-
for (const part of parts) {
|
|
84
|
-
const eqMatch = part.match(/^=(\d+)\s*/);
|
|
85
|
-
if (eqMatch) {
|
|
86
|
-
if (parseInt(eqMatch[1], 10) === count) return part.replace(/^=\d+\s*/, "");
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
const explicitCategories = [];
|
|
90
|
-
const plainParts = [];
|
|
91
|
-
for (const part of parts) {
|
|
92
|
-
if (/^=\d+\s*/.test(part)) continue;
|
|
93
|
-
const catMatch = part.match(/^(\w+):\s*/);
|
|
94
|
-
if (catMatch) explicitCategories.push({
|
|
95
|
-
cat: catMatch[1],
|
|
96
|
-
text: part.replace(/^\w+:\s*/, "")
|
|
97
|
-
});
|
|
98
|
-
else plainParts.push(part);
|
|
99
|
-
}
|
|
100
|
-
for (const { cat, text } of explicitCategories) {
|
|
101
|
-
if (cat === "zero" && count === 0) return text;
|
|
102
|
-
if (cat === "one" && count === 1) return text;
|
|
103
|
-
if (cat === getPluralCategory(count, locale)) return text;
|
|
104
|
-
}
|
|
105
|
-
if (plainParts.length === 2) return count === 1 ? plainParts[0] : plainParts[1];
|
|
106
|
-
if (plainParts.length >= 3) {
|
|
107
|
-
if (count === 0) return plainParts[0];
|
|
108
|
-
if (count === 1) return plainParts[1];
|
|
109
|
-
return plainParts[plainParts.length - 1];
|
|
110
|
-
}
|
|
111
|
-
if (plainParts.length === 1) return plainParts[0];
|
|
112
|
-
const category = getPluralCategory(count, locale);
|
|
113
|
-
const categoryIndex = [
|
|
114
|
-
"zero",
|
|
115
|
-
"one",
|
|
116
|
-
"two",
|
|
117
|
-
"few",
|
|
118
|
-
"many",
|
|
119
|
-
"other"
|
|
120
|
-
].indexOf(category);
|
|
121
|
-
if (categoryIndex >= 0 && categoryIndex < parts.length) return parts[categoryIndex];
|
|
122
|
-
return parts[parts.length - 1];
|
|
123
|
-
}
|
|
124
|
-
function resolveLinkedMessages(template, flat, visited = /* @__PURE__ */ new Set()) {
|
|
125
|
-
return template.replace(/@(?::([\w.]+)|{([\w.]+)})/g, (_match, colonKey, braceKey) => {
|
|
126
|
-
const key = colonKey || braceKey;
|
|
127
|
-
if (!key || visited.has(key)) return _match;
|
|
128
|
-
visited.add(key);
|
|
129
|
-
const linked = flat[key];
|
|
130
|
-
if (linked === void 0) return _match;
|
|
131
|
-
const resolved = resolveLinkedMessages(linked, flat, visited);
|
|
132
|
-
visited.delete(key);
|
|
133
|
-
return resolved;
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
function interpolate(template, params, locale, flat) {
|
|
137
|
-
let result = template;
|
|
138
|
-
if (flat) result = resolveLinkedMessages(result, flat);
|
|
139
|
-
if (params && typeof params.count === "number") result = selectPlural(result, params.count, locale || "en");
|
|
140
|
-
if (params) result = result.replace(/\{(\w+)\}/g, (_, key) => {
|
|
141
|
-
const val = params[key];
|
|
142
|
-
return val !== void 0 ? String(val) : `{${key}}`;
|
|
143
|
-
});
|
|
144
|
-
return result;
|
|
145
|
-
}
|
|
146
|
-
function getMessage(locale, key, params) {
|
|
147
|
-
const flat = getFlatMessages(locale);
|
|
148
|
-
const msg = flat[key];
|
|
149
|
-
if (msg === void 0) return void 0;
|
|
150
|
-
return interpolate(msg, params, locale, flat);
|
|
151
|
-
}
|
|
152
|
-
function defineLocale(definition) {
|
|
153
|
-
const state = getI18nState();
|
|
154
|
-
const locale = {
|
|
155
|
-
code: definition.code,
|
|
156
|
-
messages: definition.messages,
|
|
157
|
-
name: definition.name,
|
|
158
|
-
dir: definition.dir || "ltr",
|
|
159
|
-
isDefault: definition.isDefault
|
|
160
|
-
};
|
|
161
|
-
state.registeredLocales.set(definition.code, locale);
|
|
162
|
-
invalidateCache(definition.code);
|
|
163
|
-
if (definition.isDefault || state.registeredLocales.size === 1) {
|
|
164
|
-
state.fallbackLocale = definition.code;
|
|
165
|
-
if (!state.currentLocale || state.currentLocale === "en") state.currentLocale = definition.code;
|
|
166
|
-
}
|
|
167
|
-
return definition;
|
|
168
|
-
}
|
|
169
|
-
function setI18nConfig(config) {
|
|
170
|
-
const state = getI18nState();
|
|
171
|
-
state.i18nConfig = {
|
|
172
|
-
...state.i18nConfig,
|
|
173
|
-
...config
|
|
174
|
-
};
|
|
175
|
-
if (config.defaultLocale) state.fallbackLocale = config.defaultLocale;
|
|
176
|
-
}
|
|
177
|
-
function getI18nConfig() {
|
|
178
|
-
const state = getI18nState();
|
|
179
|
-
return {
|
|
180
|
-
...state.i18nConfig,
|
|
181
|
-
locales: Array.from(state.registeredLocales.keys())
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
function getDefaultLocale() {
|
|
185
|
-
const state = getI18nState();
|
|
186
|
-
for (const [code, loc] of state.registeredLocales) if (loc.isDefault) return code;
|
|
187
|
-
return state.i18nConfig.defaultLocale;
|
|
188
|
-
}
|
|
189
|
-
function localizePath(path, locale) {
|
|
190
|
-
const state = getI18nState();
|
|
191
|
-
const targetLocale = locale || state.currentLocale;
|
|
192
|
-
const defaultLocale = getDefaultLocale();
|
|
193
|
-
const strategy = state.i18nConfig.strategy;
|
|
194
|
-
const cleanPath = path.replace(/^\/+/, "/").replace(/\/+$/, "") || "/";
|
|
195
|
-
if (strategy === "no_prefix") return cleanPath;
|
|
196
|
-
const pathParts = cleanPath.split("/").filter(Boolean);
|
|
197
|
-
const firstSegment = pathParts[0] || "";
|
|
198
|
-
const isLocalePrefix = state.registeredLocales.has(firstSegment);
|
|
199
|
-
let pathWithoutPrefix = cleanPath;
|
|
200
|
-
if (isLocalePrefix) {
|
|
201
|
-
const rest = pathParts.slice(1).join("/");
|
|
202
|
-
pathWithoutPrefix = rest ? `/${rest}` : "/";
|
|
203
|
-
}
|
|
204
|
-
if ((strategy === "prefix_except_default" || strategy === "prefix_and_default") && targetLocale === defaultLocale) return pathWithoutPrefix;
|
|
205
|
-
return `/${targetLocale}${pathWithoutPrefix === "/" ? "" : pathWithoutPrefix}`;
|
|
206
|
-
}
|
|
207
|
-
function switchLocalePath(newLocale, currentPath) {
|
|
208
|
-
const _global = globalThis;
|
|
209
|
-
return localizePath(currentPath || (typeof _global.window !== "undefined" ? _global.window.location.pathname : "/"), newLocale);
|
|
210
|
-
}
|
|
211
|
-
function extractLocaleFromPath(path) {
|
|
212
|
-
const state = getI18nState();
|
|
213
|
-
const pathParts = path.split("/").filter(Boolean);
|
|
214
|
-
const firstSegment = pathParts[0] || "";
|
|
215
|
-
if (state.registeredLocales.has(firstSegment)) {
|
|
216
|
-
const rest = pathParts.slice(1).join("/");
|
|
217
|
-
return {
|
|
218
|
-
locale: firstSegment,
|
|
219
|
-
pathWithoutLocale: rest ? `/${rest}` : "/"
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
return {
|
|
223
|
-
locale: null,
|
|
224
|
-
pathWithoutLocale: path
|
|
225
|
-
};
|
|
226
|
-
}
|
|
227
|
-
function useI18n() {
|
|
228
|
-
return {
|
|
229
|
-
get locale() {
|
|
230
|
-
return getI18nState().currentLocale;
|
|
231
|
-
},
|
|
232
|
-
get fallbackLocale() {
|
|
233
|
-
return getI18nState().fallbackLocale;
|
|
234
|
-
},
|
|
235
|
-
get availableLocales() {
|
|
236
|
-
return Array.from(getI18nState().registeredLocales.keys());
|
|
237
|
-
},
|
|
238
|
-
t(key, params) {
|
|
239
|
-
const state = getI18nState();
|
|
240
|
-
let message = getMessage(state.currentLocale, key, params);
|
|
241
|
-
if (message === void 0 && state.currentLocale !== state.fallbackLocale) message = getMessage(state.fallbackLocale, key, params);
|
|
242
|
-
if (message === void 0) {
|
|
243
|
-
notifyMissingKey(state.currentLocale, key);
|
|
244
|
-
return key;
|
|
245
|
-
}
|
|
246
|
-
return message;
|
|
247
|
-
},
|
|
248
|
-
d(value, style = "short", options) {
|
|
249
|
-
try {
|
|
250
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
251
|
-
if (isNaN(date.getTime())) throw new Error("Invalid date");
|
|
252
|
-
return new Intl.DateTimeFormat(getI18nState().currentLocale, {
|
|
253
|
-
dateStyle: style,
|
|
254
|
-
...options
|
|
255
|
-
}).format(date);
|
|
256
|
-
} catch {
|
|
257
|
-
const date = value instanceof Date ? value : new Date(value);
|
|
258
|
-
if (isNaN(date.getTime())) return String(value);
|
|
259
|
-
return date.toISOString().split("T")[0];
|
|
260
|
-
}
|
|
261
|
-
},
|
|
262
|
-
n(value, style = "decimal", options) {
|
|
263
|
-
try {
|
|
264
|
-
return new Intl.NumberFormat(getI18nState().currentLocale, {
|
|
265
|
-
style,
|
|
266
|
-
...options
|
|
267
|
-
}).format(value);
|
|
268
|
-
} catch {
|
|
269
|
-
return String(value);
|
|
270
|
-
}
|
|
271
|
-
},
|
|
272
|
-
c(value, currency, options) {
|
|
273
|
-
try {
|
|
274
|
-
return new Intl.NumberFormat(getI18nState().currentLocale, {
|
|
275
|
-
style: "currency",
|
|
276
|
-
currency,
|
|
277
|
-
...options
|
|
278
|
-
}).format(value);
|
|
279
|
-
} catch {
|
|
280
|
-
return `${currency} ${value}`;
|
|
281
|
-
}
|
|
282
|
-
},
|
|
283
|
-
relativeTime(value, unit, options) {
|
|
284
|
-
try {
|
|
285
|
-
return new Intl.RelativeTimeFormat(getI18nState().currentLocale, options).format(value, unit);
|
|
286
|
-
} catch {
|
|
287
|
-
return `${value >= 0 ? "in " : ""}${Math.abs(value)} ${unit}${value === 1 ? "" : "s"}${value < 0 ? " ago" : ""}`;
|
|
288
|
-
}
|
|
289
|
-
},
|
|
290
|
-
list(items, style = "conjunction", options) {
|
|
291
|
-
try {
|
|
292
|
-
return new Intl.ListFormat(getI18nState().currentLocale, {
|
|
293
|
-
type: style,
|
|
294
|
-
...options
|
|
295
|
-
}).format(items);
|
|
296
|
-
} catch {
|
|
297
|
-
return items.join(", ");
|
|
298
|
-
}
|
|
299
|
-
},
|
|
300
|
-
setLocale(locale) {
|
|
301
|
-
const state = getI18nState();
|
|
302
|
-
if (state.registeredLocales.has(locale) && locale !== state.currentLocale) {
|
|
303
|
-
state.currentLocale = locale;
|
|
304
|
-
notifyLocaleChange(locale);
|
|
305
|
-
}
|
|
306
|
-
},
|
|
307
|
-
getLocale() {
|
|
308
|
-
return getI18nState().currentLocale;
|
|
309
|
-
},
|
|
310
|
-
addLocale(code, messages, options) {
|
|
311
|
-
const state = getI18nState();
|
|
312
|
-
const existing = state.registeredLocales.get(code);
|
|
313
|
-
if (existing) {
|
|
314
|
-
existing.messages = deepMerge(existing.messages, messages);
|
|
315
|
-
if (options?.name) existing.name = options.name;
|
|
316
|
-
if (options?.dir) existing.dir = options.dir;
|
|
317
|
-
} else state.registeredLocales.set(code, {
|
|
318
|
-
code,
|
|
319
|
-
messages,
|
|
320
|
-
name: options?.name,
|
|
321
|
-
dir: options?.dir || "ltr"
|
|
322
|
-
});
|
|
323
|
-
invalidateCache(code);
|
|
324
|
-
state.missingKeyWarned = /* @__PURE__ */ new Set();
|
|
325
|
-
},
|
|
326
|
-
mergeLocale(code, messages) {
|
|
327
|
-
const state = getI18nState();
|
|
328
|
-
const existing = state.registeredLocales.get(code);
|
|
329
|
-
if (existing) existing.messages = deepMerge(existing.messages, messages);
|
|
330
|
-
else state.registeredLocales.set(code, {
|
|
331
|
-
code,
|
|
332
|
-
messages,
|
|
333
|
-
dir: "ltr"
|
|
334
|
-
});
|
|
335
|
-
invalidateCache(code);
|
|
336
|
-
state.missingKeyWarned = /* @__PURE__ */ new Set();
|
|
337
|
-
},
|
|
338
|
-
detectLocale(acceptLanguage) {
|
|
339
|
-
const state = getI18nState();
|
|
340
|
-
if (!acceptLanguage) return state.fallbackLocale;
|
|
341
|
-
const requested = acceptLanguage.split(",").map((lang) => {
|
|
342
|
-
const [code, q = "q=1.0"] = lang.trim().split(";");
|
|
343
|
-
const quality = parseFloat(q.replace("q=", "")) || 0;
|
|
344
|
-
return {
|
|
345
|
-
code: code.trim().toLowerCase(),
|
|
346
|
-
quality
|
|
347
|
-
};
|
|
348
|
-
}).sort((a, b) => b.quality - a.quality);
|
|
349
|
-
for (const { code } of requested) for (const registered of state.registeredLocales.keys()) if (code === registered.toLowerCase() || code.startsWith(`${registered.toLowerCase()}-`)) return registered;
|
|
350
|
-
return state.fallbackLocale;
|
|
351
|
-
},
|
|
352
|
-
onLocaleChange: addLocaleListener,
|
|
353
|
-
onMissingKey: addMissingKeyHandler,
|
|
354
|
-
getLocaleDir(locale) {
|
|
355
|
-
const state = getI18nState();
|
|
356
|
-
const code = locale || state.currentLocale;
|
|
357
|
-
return state.registeredLocales.get(code)?.dir || "ltr";
|
|
358
|
-
},
|
|
359
|
-
getLocaleName(locale) {
|
|
360
|
-
const state = getI18nState();
|
|
361
|
-
const code = locale || state.currentLocale;
|
|
362
|
-
return state.registeredLocales.get(code)?.name;
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
}
|
|
366
|
-
function t(key, params) {
|
|
367
|
-
return useI18n().t(key, params);
|
|
368
|
-
}
|
|
369
|
-
function setLocale(locale) {
|
|
370
|
-
useI18n().setLocale(locale);
|
|
371
|
-
}
|
|
372
|
-
function getLocale() {
|
|
373
|
-
return useI18n().getLocale();
|
|
374
|
-
}
|
|
375
|
-
function getRegisteredLocales() {
|
|
376
|
-
return Array.from(getI18nState().registeredLocales.keys());
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Returns metadata for all registered locales (without messages).
|
|
380
|
-
* Used by SSR to serialize the full locale list so the client can
|
|
381
|
-
* register all available locales during hydration — preventing
|
|
382
|
-
* `availableLocales` hydration mismatches.
|
|
383
|
-
*/
|
|
384
|
-
function getRegisteredLocalesMeta() {
|
|
385
|
-
const state = getI18nState();
|
|
386
|
-
return Array.from(state.registeredLocales.values()).map((loc) => ({
|
|
387
|
-
code: loc.code,
|
|
388
|
-
name: loc.name,
|
|
389
|
-
dir: loc.dir,
|
|
390
|
-
isDefault: loc.isDefault
|
|
391
|
-
}));
|
|
392
|
-
}
|
|
393
|
-
function getLocaleMessages(locale) {
|
|
394
|
-
const state = getI18nState();
|
|
395
|
-
const code = locale || state.currentLocale;
|
|
396
|
-
return state.registeredLocales.get(code)?.messages;
|
|
397
|
-
}
|
|
398
|
-
function clearLocales() {
|
|
399
|
-
const state = getI18nState();
|
|
400
|
-
state.registeredLocales.clear();
|
|
401
|
-
state.localeListeners.clear();
|
|
402
|
-
state.missingKeyHandlers.clear();
|
|
403
|
-
state.messageCache.clear();
|
|
404
|
-
state.missingKeyWarned = /* @__PURE__ */ new Set();
|
|
405
|
-
state.currentLocale = "en";
|
|
406
|
-
state.fallbackLocale = "en";
|
|
407
|
-
}
|
|
408
|
-
function onLocaleChange(callback) {
|
|
409
|
-
return useI18n().onLocaleChange(callback);
|
|
410
|
-
}
|
|
411
|
-
function getLocaleDir(locale) {
|
|
412
|
-
return useI18n().getLocaleDir(locale);
|
|
413
|
-
}
|
|
414
|
-
function getLocaleName(locale) {
|
|
415
|
-
return useI18n().getLocaleName(locale);
|
|
416
|
-
}
|
|
417
|
-
function detectLocale(acceptLanguage) {
|
|
418
|
-
return useI18n().detectLocale(acceptLanguage);
|
|
419
|
-
}
|
|
420
|
-
function addLocale(code, messages, options) {
|
|
421
|
-
useI18n().addLocale(code, messages, options);
|
|
422
|
-
}
|
|
423
|
-
function mergeLocale(code, messages) {
|
|
424
|
-
useI18n().mergeLocale(code, messages);
|
|
425
|
-
}
|
|
426
|
-
function detectBrowserLocale() {
|
|
427
|
-
const i18n = useI18n();
|
|
428
|
-
if (typeof navigator !== "undefined" && navigator.language) return i18n.detectLocale(navigator.language);
|
|
429
|
-
return getI18nState().fallbackLocale;
|
|
430
|
-
}
|
|
431
|
-
function formatDate(value, style, options) {
|
|
432
|
-
return useI18n().d(value, style, options);
|
|
433
|
-
}
|
|
434
|
-
function formatNumber(value, style, options) {
|
|
435
|
-
return useI18n().n(value, style, options);
|
|
436
|
-
}
|
|
437
|
-
function formatCurrency(value, currency, options) {
|
|
438
|
-
return useI18n().c(value, currency, options);
|
|
439
|
-
}
|
|
440
|
-
function formatRelativeTime(value, unit, options) {
|
|
441
|
-
return useI18n().relativeTime(value, unit, options);
|
|
442
|
-
}
|
|
443
|
-
function formatList(items, style, options) {
|
|
444
|
-
return useI18n().list(items, style, options);
|
|
445
|
-
}
|
|
446
|
-
//#endregion
|
|
447
|
-
export { addLocale, clearLocales, defineLocale, detectBrowserLocale, detectLocale, extractLocaleFromPath, formatCurrency, formatDate, formatList, formatNumber, formatRelativeTime, getDefaultLocale, getI18nConfig, getLocale, getLocaleDir, getLocaleMessages, getLocaleName, getRegisteredLocales, getRegisteredLocalesMeta, localizePath, mergeLocale, onLocaleChange, setI18nConfig, setLocale, switchLocalePath, t, useI18n };
|
|
1
|
+
import { a as extractLocaleFromPath, c as switchLocalePath, i as compileLocalePaths, l as toVueRouterLocalePath, n as parseLocaleCookie, o as getVueLocaleParam, r as serializeLocaleCookie, s as localizePath, t as detectLocaleFromAcceptLanguage } from "./detect-BIURd4aL.js";
|
|
2
|
+
import { C as t, S as setLocaleMeta, _ as n, a as d, b as setFallbackLocale, c as getI18nScope, d as getLocaleMeta, f as getLocaleName, g as mergeLocaleMessages, h as listLocaleCodes, i as createRequestContext, l as getLocaleDir, m as getRequestLocale, n as getPathWithoutLocale, o as ensureLocaleMessages, p as getRegisteredLocalesMeta, r as getRequestLocale$1, s as getFallbackLocale, t as createI18nMiddleware, u as getLocaleMessages, v as registerLocaleLoader, x as setLocaleMessages, y as runWithI18n } from "./routing-D4Y0l8jV.js";
|
|
3
|
+
import { t as buildLocaleHead } from "./head-BzEFwnlD.js";
|
|
4
|
+
export { buildLocaleHead, compileLocalePaths, createI18nMiddleware, createRequestContext, d, detectLocaleFromAcceptLanguage, ensureLocaleMessages, extractLocaleFromPath, getRequestLocale as getAlsLocale, getFallbackLocale, getI18nScope, getLocaleDir, getLocaleMessages, getLocaleMeta, getLocaleName, getPathWithoutLocale, getRegisteredLocalesMeta, getRequestLocale$1 as getRequestLocale, getVueLocaleParam, listLocaleCodes, localizePath, mergeLocaleMessages, n, parseLocaleCookie, registerLocaleLoader, runWithI18n, serializeLocaleCookie, setFallbackLocale, setLocaleMessages, setLocaleMeta, switchLocalePath, t, toVueRouterLocalePath };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
type I18nRoutingStrategy = 'prefix' | 'prefix_except_default' | 'prefix_and_default' | 'no_prefix';
|
|
3
|
+
interface LocaleRoutingConfig {
|
|
4
|
+
defaultLocale: string;
|
|
5
|
+
locales: string[];
|
|
6
|
+
strategy: I18nRoutingStrategy;
|
|
7
|
+
}
|
|
8
|
+
interface HonoLocalePath {
|
|
9
|
+
path: string;
|
|
10
|
+
locale: string;
|
|
11
|
+
isDefault: boolean;
|
|
12
|
+
}
|
|
13
|
+
interface CompiledLocalePath {
|
|
14
|
+
/** vue-router path, may contain `:locale(zh)?` */
|
|
15
|
+
vuePath: string;
|
|
16
|
+
/** Concrete Hono mount paths */
|
|
17
|
+
hono: HonoLocalePath[];
|
|
18
|
+
}
|
|
19
|
+
interface I18nDetectOptions {
|
|
20
|
+
cookieName: string;
|
|
21
|
+
redirectOn: 'root' | 'all';
|
|
22
|
+
alwaysRedirect: boolean;
|
|
23
|
+
}
|
|
24
|
+
interface I18nMiddlewareOptions {
|
|
25
|
+
defaultLocale: string;
|
|
26
|
+
locales: string[];
|
|
27
|
+
strategy: I18nRoutingStrategy;
|
|
28
|
+
detectBrowserLanguage?: false | Partial<I18nDetectOptions>;
|
|
29
|
+
loadMessages?: (locale: string, fallback: string) => Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
interface I18nLocaleMeta {
|
|
32
|
+
code: string;
|
|
33
|
+
language?: string;
|
|
34
|
+
name?: string;
|
|
35
|
+
dir?: 'ltr' | 'rtl';
|
|
36
|
+
isDefault?: boolean;
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/paths.d.ts
|
|
40
|
+
/**
|
|
41
|
+
* vue-router locale param segment for the given strategy.
|
|
42
|
+
*
|
|
43
|
+
* - `prefix_except_default`: `:locale(zh)?` (default unprefixed; `/en/about` 不匹配)
|
|
44
|
+
* - `prefix`: `:locale(en|zh)` (required)
|
|
45
|
+
* - `prefix_and_default`: `:locale(en|zh)?` (`/about` 与 `/en/about` 都匹配)
|
|
46
|
+
* - `no_prefix`: empty
|
|
47
|
+
*/
|
|
48
|
+
declare function getVueLocaleParam(cfg: LocaleRoutingConfig): string;
|
|
49
|
+
/** Apply a vue-router locale param to a page path (`/` / `/about` / catch-all). */
|
|
50
|
+
declare function toVueRouterLocalePath(pagePath: string, localeParam: string): string;
|
|
51
|
+
declare function extractLocaleFromPath(path: string, localeCodes: string[]): {
|
|
52
|
+
locale: string | null;
|
|
53
|
+
pathWithoutLocale: string;
|
|
54
|
+
};
|
|
55
|
+
declare function localizePath(path: string, locale: string, cfg: LocaleRoutingConfig): string;
|
|
56
|
+
declare function switchLocalePath(locale: string, currentPath: string, cfg: LocaleRoutingConfig): string;
|
|
57
|
+
declare function compileLocalePaths(pagePath: string, cfg: LocaleRoutingConfig): CompiledLocalePath;
|
|
58
|
+
//#endregion
|
|
59
|
+
export { switchLocalePath as a, HonoLocalePath as c, I18nRoutingStrategy as d, LocaleRoutingConfig as f, localizePath as i, I18nLocaleMeta as l, extractLocaleFromPath as n, toVueRouterLocalePath as o, getVueLocaleParam as r, CompiledLocalePath as s, compileLocalePaths as t, I18nMiddlewareOptions as u };
|