@hamur/i18n 0.0.0-stage → 0.0.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.
Files changed (155) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +38 -2
  3. package/dist/chunks/bun-index-1ve1hj66.js +744 -0
  4. package/dist/chunks/bun-index-8bw4xpv8.js +93 -0
  5. package/dist/chunks/bun-index-ej6vjnjs.js +297 -0
  6. package/dist/chunks/bun-index-pdepkyy1.js +474 -0
  7. package/dist/src/adapters/angular.js +67 -0
  8. package/dist/src/adapters/html.js +34 -0
  9. package/dist/src/adapters/preact.js +23 -0
  10. package/dist/src/adapters/react.js +23 -0
  11. package/dist/src/adapters/solid.js +48 -0
  12. package/dist/src/adapters/svelte.js +74 -0
  13. package/dist/src/adapters/vue.js +48 -0
  14. package/dist/src/cli.js +1898 -0
  15. package/dist/src/index.js +2755 -0
  16. package/dist/src/load.js +35 -0
  17. package/dist/src/server/index.js +395 -0
  18. package/dist/types/src/adapters/angular.d.ts +17 -0
  19. package/dist/types/src/adapters/bind.d.ts +44 -0
  20. package/dist/types/src/adapters/html.d.ts +5 -0
  21. package/dist/types/src/adapters/preact.d.ts +6 -0
  22. package/dist/types/src/adapters/react.d.ts +6 -0
  23. package/dist/types/src/adapters/solid.d.ts +9 -0
  24. package/dist/types/src/adapters/svelte.d.ts +12 -0
  25. package/dist/types/src/adapters/vue.d.ts +8 -0
  26. package/dist/types/src/build/catalog.d.ts +5 -0
  27. package/dist/types/src/build/drop.d.ts +12 -0
  28. package/dist/types/src/build/extract.d.ts +11 -0
  29. package/dist/types/src/build/head.d.ts +4 -0
  30. package/dist/types/src/build/messages.d.ts +27 -0
  31. package/dist/types/src/build/prerender.d.ts +10 -0
  32. package/dist/types/src/build/routes.d.ts +14 -0
  33. package/dist/types/src/build/split.d.ts +37 -0
  34. package/dist/types/src/build/types.d.ts +15 -0
  35. package/dist/types/src/cli.d.ts +12 -0
  36. package/dist/types/src/compiler/compile.d.ts +17 -0
  37. package/dist/types/src/compiler/mf2-parse.d.ts +62 -0
  38. package/dist/types/src/compiler/mf2.d.ts +3 -0
  39. package/dist/types/src/compiler/parse.d.ts +58 -0
  40. package/dist/types/src/compiler/skeleton.d.ts +7 -0
  41. package/dist/types/src/index.d.ts +2 -0
  42. package/dist/types/src/load.d.ts +7 -0
  43. package/dist/types/src/module.d.ts +15 -0
  44. package/dist/types/src/options.d.ts +7 -0
  45. package/dist/types/src/runtime/catalog-client.d.ts +3 -0
  46. package/dist/types/src/runtime/catalog-loader.d.ts +15 -0
  47. package/dist/types/src/runtime/catalog-server.d.ts +5 -0
  48. package/dist/types/src/runtime/catalog.d.ts +60 -0
  49. package/dist/types/src/runtime/chain.d.ts +3 -0
  50. package/dist/types/src/runtime/format.d.ts +27 -0
  51. package/dist/types/src/runtime/head.d.ts +9 -0
  52. package/dist/types/src/runtime/interpret.d.ts +20 -0
  53. package/dist/types/src/runtime/link.d.ts +8 -0
  54. package/dist/types/src/runtime/locale.d.ts +73 -0
  55. package/dist/types/src/runtime/match.d.ts +54 -0
  56. package/dist/types/src/runtime/messages.d.ts +42 -0
  57. package/dist/types/src/runtime/middleware.d.ts +2 -0
  58. package/dist/types/src/runtime/negotiate.d.ts +9 -0
  59. package/dist/types/src/runtime/plugin.d.ts +4 -0
  60. package/dist/types/src/runtime/redirect.d.ts +32 -0
  61. package/dist/types/src/runtime/rich.d.ts +5 -0
  62. package/dist/types/src/runtime/translate.d.ts +21 -0
  63. package/dist/types/src/server/catalog.d.ts +2 -0
  64. package/dist/types/src/server/context.d.ts +48 -0
  65. package/dist/types/src/server/door-all-catalog.d.ts +3 -0
  66. package/dist/types/src/server/door-all.d.ts +2 -0
  67. package/dist/types/src/server/door-catalog.d.ts +3 -0
  68. package/dist/types/src/server/door-root-catalog.d.ts +3 -0
  69. package/dist/types/src/server/door-root.d.ts +2 -0
  70. package/dist/types/src/server/door.d.ts +2 -0
  71. package/dist/types/src/server/index.d.ts +7 -0
  72. package/dist/types/src/server/preferred.d.ts +6 -0
  73. package/dist/types/src/server/redirect.d.ts +3 -0
  74. package/dist/types/src/server/setup.d.ts +2 -0
  75. package/dist/types/src/types.d.ts +163 -0
  76. package/package.json +122 -4
  77. package/src/adapters/angular.ts +65 -0
  78. package/src/adapters/bind.ts +144 -0
  79. package/src/adapters/html.ts +35 -0
  80. package/src/adapters/preact.ts +18 -0
  81. package/src/adapters/react.ts +18 -0
  82. package/src/adapters/solid.ts +44 -0
  83. package/src/adapters/svelte.ts +75 -0
  84. package/src/adapters/test/SveltePage.svelte +18 -0
  85. package/src/adapters/test/SvelteReporting.svelte +20 -0
  86. package/src/adapters/test/SvelteRich.svelte +11 -0
  87. package/src/adapters/test/SvelteRichPlain.svelte +11 -0
  88. package/src/adapters/test/peer.ts +7 -0
  89. package/src/adapters/test/preact-client.ts +43 -0
  90. package/src/adapters/test/preact-page.ts +34 -0
  91. package/src/adapters/test/preact-rich.ts +9 -0
  92. package/src/adapters/test/react-client.ts +49 -0
  93. package/src/adapters/test/react-page.ts +34 -0
  94. package/src/adapters/test/react-rich.ts +15 -0
  95. package/src/adapters/test/runtime.ts +104 -0
  96. package/src/adapters/test/solid-client.ts +33 -0
  97. package/src/adapters/test/solid-page.ts +30 -0
  98. package/src/adapters/test/solid-rich.ts +5 -0
  99. package/src/adapters/test/svelte-client.ts +51 -0
  100. package/src/adapters/test/svelte-log.ts +2 -0
  101. package/src/adapters/test/svelte-plugin.ts +20 -0
  102. package/src/adapters/test/vue-page.ts +26 -0
  103. package/src/adapters/test/vue-rich.ts +16 -0
  104. package/src/adapters/vue.ts +44 -0
  105. package/src/build/catalog.ts +44 -0
  106. package/src/build/drop.ts +36 -0
  107. package/src/build/extract.ts +207 -0
  108. package/src/build/head.ts +10 -0
  109. package/src/build/messages.ts +347 -0
  110. package/src/build/prerender.ts +48 -0
  111. package/src/build/routes.ts +269 -0
  112. package/src/build/split.ts +240 -0
  113. package/src/build/types.ts +156 -0
  114. package/src/cli.ts +25 -0
  115. package/src/compiler/compile.ts +190 -0
  116. package/src/compiler/mf2-parse.ts +469 -0
  117. package/src/compiler/mf2.ts +393 -0
  118. package/src/compiler/parse.ts +265 -0
  119. package/src/compiler/skeleton.ts +85 -0
  120. package/src/env.d.ts +55 -0
  121. package/src/index.ts +24 -0
  122. package/src/load.ts +41 -0
  123. package/src/module.ts +66 -0
  124. package/src/options.ts +198 -0
  125. package/src/runtime/catalog-client.ts +28 -0
  126. package/src/runtime/catalog-loader.ts +28 -0
  127. package/src/runtime/catalog-server.ts +36 -0
  128. package/src/runtime/catalog.ts +190 -0
  129. package/src/runtime/chain.ts +9 -0
  130. package/src/runtime/format.ts +102 -0
  131. package/src/runtime/head.ts +57 -0
  132. package/src/runtime/interpret.ts +504 -0
  133. package/src/runtime/link.ts +36 -0
  134. package/src/runtime/locale.ts +392 -0
  135. package/src/runtime/match.ts +334 -0
  136. package/src/runtime/messages.ts +129 -0
  137. package/src/runtime/middleware.ts +32 -0
  138. package/src/runtime/negotiate.ts +61 -0
  139. package/src/runtime/plugin.ts +69 -0
  140. package/src/runtime/redirect.ts +191 -0
  141. package/src/runtime/rich.ts +21 -0
  142. package/src/runtime/translate.ts +131 -0
  143. package/src/server/catalog.ts +11 -0
  144. package/src/server/context.ts +191 -0
  145. package/src/server/door-all-catalog.ts +7 -0
  146. package/src/server/door-all.ts +5 -0
  147. package/src/server/door-catalog.ts +6 -0
  148. package/src/server/door-root-catalog.ts +7 -0
  149. package/src/server/door-root.ts +5 -0
  150. package/src/server/door.ts +4 -0
  151. package/src/server/index.ts +11 -0
  152. package/src/server/preferred.ts +12 -0
  153. package/src/server/redirect.ts +26 -0
  154. package/src/server/setup.ts +14 -0
  155. package/src/types.ts +182 -0
@@ -0,0 +1,474 @@
1
+ // @bun
2
+ import {
3
+ fallbackChain,
4
+ createMatcher
5
+ } from "./bun-index-ej6vjnjs.js";
6
+
7
+ // packages/i18n/src/runtime/catalog-loader.ts
8
+ var serverLoader;
9
+ var serverCatalog = () => serverLoader;
10
+ function installServerCatalog(loader) {
11
+ serverLoader = loader;
12
+ }
13
+
14
+ // packages/i18n/src/runtime/locale.ts
15
+ import { siteConfigOf } from "hamur/app";
16
+
17
+ // packages/i18n/src/runtime/negotiate.ts
18
+ var RANGE = /^[A-Za-z]{1,8}(-[A-Za-z0-9]{1,8})*$/;
19
+ var QUALITY = /^q=(0(\.\d{0,3})?|1(\.0{0,3})?)$/i;
20
+ var MAX_ENTRIES = 32;
21
+ function subtag(tag) {
22
+ try {
23
+ return new Intl.Locale(tag).language;
24
+ } catch {
25
+ return;
26
+ }
27
+ }
28
+ function pickLocale(ranges, locales) {
29
+ for (const range of ranges) {
30
+ const lower = range.toLowerCase();
31
+ const exact = locales.find((locale) => locale.language.toLowerCase() === lower || locale.code === lower);
32
+ if (exact)
33
+ return exact;
34
+ const language = subtag(range);
35
+ const near = language && locales.find((locale) => subtag(locale.language) === language);
36
+ if (near)
37
+ return near;
38
+ }
39
+ return;
40
+ }
41
+ function acceptedRanges(header) {
42
+ if (!header)
43
+ return [];
44
+ const entries = [];
45
+ header.split(",").slice(0, MAX_ENTRIES).forEach((entry, index) => {
46
+ const [range = "", ...params] = entry.split(";").map((part) => part.trim());
47
+ if (!RANGE.test(range))
48
+ return;
49
+ let q = 1;
50
+ for (const param of params) {
51
+ if (!QUALITY.test(param))
52
+ return;
53
+ q = Number(param.slice(2));
54
+ }
55
+ if (q > 0)
56
+ entries.push({ range, q, index });
57
+ });
58
+ return entries.sort((a, b) => b.q - a.q || a.index - b.index).map((entry) => entry.range);
59
+ }
60
+ function negotiateAll(header, locales) {
61
+ const found = [];
62
+ for (const range of acceptedRanges(header)) {
63
+ const locale = pickLocale([range], locales);
64
+ if (locale && !found.includes(locale))
65
+ found.push(locale);
66
+ }
67
+ return found;
68
+ }
69
+ function negotiate(header, locales) {
70
+ return negotiateAll(header, locales)[0];
71
+ }
72
+
73
+ // packages/i18n/src/runtime/messages.ts
74
+ function createMessageStore() {
75
+ let loaders = {};
76
+ let chunks = {};
77
+ let routes = {};
78
+ const parts = new Map;
79
+ const catalogs = new Map;
80
+ const fills = new Map;
81
+ const pending = new Map;
82
+ const remotes = new Map;
83
+ const store = {
84
+ load(code) {
85
+ if (catalogs.has(code))
86
+ return Promise.resolve();
87
+ let promise = pending.get(code);
88
+ if (!promise) {
89
+ const loader = loaders[code];
90
+ if (!loader)
91
+ return Promise.reject(new Error(`i18n: no messages for locale "${code}"`));
92
+ promise = loader().then((module) => {
93
+ catalogs.set(code, module.default);
94
+ if (module.fallbacks?.length)
95
+ fills.set(code, new Set(module.fallbacks));
96
+ pending.delete(code);
97
+ }, (error) => {
98
+ pending.delete(code);
99
+ throw error;
100
+ });
101
+ pending.set(code, promise);
102
+ }
103
+ return promise;
104
+ },
105
+ async loadRoute(code, base) {
106
+ const own = routes[code];
107
+ if (!own)
108
+ return store.load(code);
109
+ const names = ["_shared", ...base !== undefined && own[base] ? [base] : []];
110
+ const held = parts.get(code) ?? new Set;
111
+ parts.set(code, held);
112
+ const wanted = names.filter((name) => !held.has(name));
113
+ if (wanted.length === 0 && catalogs.has(code))
114
+ return;
115
+ const loaded = await Promise.all(wanted.map((name) => own[name]().then((module) => [name, module.default])));
116
+ const catalog = catalogs.get(code) ?? {};
117
+ for (const [name, messages] of loaded) {
118
+ held.add(name);
119
+ Object.assign(catalog, messages);
120
+ }
121
+ catalogs.set(code, catalog);
122
+ },
123
+ async loadAll() {
124
+ await Promise.all(Object.keys(loaders).map((code) => store.load(code)));
125
+ },
126
+ get: (code) => catalogs.get(code),
127
+ filled: (code) => fills.get(code),
128
+ install(next, urls = {}, split = {}) {
129
+ loaders = next;
130
+ chunks = urls;
131
+ routes = split;
132
+ },
133
+ chunksOf(code, base) {
134
+ const own = chunks[code];
135
+ if (own === undefined)
136
+ return [];
137
+ if (typeof own === "string")
138
+ return [own];
139
+ return [own._shared, base === undefined ? undefined : own[base]].filter((href) => href !== undefined);
140
+ },
141
+ remote: (code) => remotes.get(code),
142
+ setRemote(code, catalog) {
143
+ if (catalog)
144
+ remotes.set(code, catalog);
145
+ else
146
+ remotes.delete(code);
147
+ }
148
+ };
149
+ return store;
150
+ }
151
+ var messageStore = createMessageStore();
152
+ var loadMessages = (code) => messageStore.load(code);
153
+
154
+ // packages/i18n/src/runtime/format.ts
155
+ var MAX_ENTRIES2 = 256;
156
+ function cached(cache, key, make) {
157
+ let value = cache.get(key);
158
+ if (value === undefined) {
159
+ if (cache.size >= MAX_ENTRIES2)
160
+ cache.clear();
161
+ value = make();
162
+ cache.set(key, value);
163
+ }
164
+ return value;
165
+ }
166
+ var numberFormats = new Map;
167
+ var dateFormats = new Map;
168
+ var pluralRules = new Map;
169
+ var numberFormat = (language, options = {}) => cached(numberFormats, `${language}
170
+ ${JSON.stringify(options)}`, () => new Intl.NumberFormat(language, options));
171
+ var dateFormat = (language, options) => cached(dateFormats, `${language}
172
+ ${JSON.stringify(options)}`, () => new Intl.DateTimeFormat(language, options));
173
+ var pluralRule = (language, type) => cached(pluralRules, `${language}
174
+ ${type}`, () => new Intl.PluralRules(language, { type }));
175
+ var keyOf = (value) => Object.is(value, -0) ? "-0" : String(value);
176
+ function recorded(texts, table, value, make) {
177
+ if (!texts)
178
+ return make();
179
+ const known = texts.read()?.[table]?.[value];
180
+ if (known !== undefined)
181
+ return known;
182
+ const text = make();
183
+ if (texts.write) {
184
+ const all = texts.read() ?? texts.create?.();
185
+ if (all)
186
+ (all[table] ??= {})[value] = text;
187
+ }
188
+ return text;
189
+ }
190
+ var run = (value, ...args) => typeof value === "function" ? value(...args) : value;
191
+ function createFormatters(language, timeZone, texts) {
192
+ const f = {
193
+ n(value, options) {
194
+ if (typeof value !== "number" && typeof value !== "bigint")
195
+ return String(value);
196
+ return recorded(texts, `n|${language}|${JSON.stringify(options ?? {})}`, keyOf(value), () => numberFormat(language, options).format(value));
197
+ },
198
+ d(value, options) {
199
+ if (!(value instanceof Date) && typeof value !== "number")
200
+ return String(value);
201
+ const resolved = { timeZone, ...options };
202
+ const at = value instanceof Date ? value.getTime() : value;
203
+ return recorded(texts, `d|${language}|${JSON.stringify(resolved)}`, String(at), () => dateFormat(language, resolved).format(value));
204
+ },
205
+ p(value, offset, cases, ordinal) {
206
+ const count = typeof value === "bigint" ? Number(value) : value;
207
+ if (typeof count !== "number" || Number.isNaN(count))
208
+ return run(cases.other, String(value));
209
+ const exact = cases[`=${count}`];
210
+ const shifted = count - offset;
211
+ const type = ordinal ? "ordinal" : "cardinal";
212
+ const category = exact === undefined ? recorded(texts, `p|${language}|${type}`, keyOf(shifted), () => pluralRule(language, type).select(shifted)) : undefined;
213
+ const chosen = exact ?? cases[category] ?? cases.other;
214
+ return typeof chosen === "function" ? chosen(f.n(shifted)) : chosen;
215
+ },
216
+ s: (value, cases) => run(Object.hasOwn(cases, String(value)) ? cases[String(value)] : cases.other)
217
+ };
218
+ return f;
219
+ }
220
+
221
+ // packages/i18n/src/runtime/rich.ts
222
+ function richText(parts) {
223
+ let text = "";
224
+ for (const part of parts)
225
+ text += typeof part === "string" ? part : richText(part.children);
226
+ return text;
227
+ }
228
+ function renderRich(parts, handlers, join) {
229
+ const out = parts.map((part) => {
230
+ if (typeof part === "string")
231
+ return part;
232
+ const chunks = renderRich(part.children, handlers, join);
233
+ const handler = Object.hasOwn(handlers, part.tag) ? handlers[part.tag] : undefined;
234
+ return { node: handler ? handler(chunks) : chunks };
235
+ });
236
+ return join(out);
237
+ }
238
+
239
+ // packages/i18n/src/runtime/translate.ts
240
+ var messageOf = (catalog, key) => catalog && Object.hasOwn(catalog, key) ? catalog[key] : undefined;
241
+ function createTranslators(settings) {
242
+ const { defaultLocale, fallback, timeZone, dev, store } = settings;
243
+ const byCode = new Map((settings.locales ?? [defaultLocale]).map((info) => [info.code, info]));
244
+ const shared = { formatters: new Map, translators: new Map };
245
+ const scoped = new WeakMap;
246
+ const reported = new Set;
247
+ const report = (text, ...parts) => {
248
+ if (!dev || reported.has(parts.join("\x00")))
249
+ return;
250
+ reported.add(parts.join("\x00"));
251
+ console.warn(`[hamur] i18n: ${text}`);
252
+ };
253
+ return (locale, scope) => {
254
+ if (!store.get(locale.code))
255
+ throw new Error(`i18n: locale "${locale.code}" is not loaded`);
256
+ let set = scope ? scoped.get(scope.owner) : shared;
257
+ if (!set)
258
+ scoped.set(scope.owner, set = { formatters: new Map, translators: new Map });
259
+ const { formatters, translators } = set;
260
+ const formattersOf = (info) => {
261
+ let f = formatters.get(info.language);
262
+ if (!f)
263
+ formatters.set(info.language, f = createFormatters(info.language, timeZone, scope?.texts));
264
+ return f;
265
+ };
266
+ const known = translators.get(locale.code);
267
+ if (known)
268
+ return known;
269
+ const own = formattersOf(locale);
270
+ const chain = fallbackChain(fallback, defaultLocale.code, locale.code);
271
+ const render = (key, message, f, rest) => {
272
+ if (typeof message === "string")
273
+ return message;
274
+ const given = rest[0] ?? {};
275
+ const args = dev ? new Proxy(given, {
276
+ has(target, name) {
277
+ const present = Reflect.has(target, name) && target[name] !== undefined;
278
+ if (!present)
279
+ report(`${JSON.stringify(key)} in ${locale.code} needs the argument ${JSON.stringify(name)}`, "arg", key, locale.code, String(name));
280
+ return present;
281
+ }
282
+ }) : given;
283
+ return message(args, f);
284
+ };
285
+ const fromRemote = (code, key, f, rest) => {
286
+ const message = messageOf(store.remote(code), key);
287
+ if (message === undefined)
288
+ return;
289
+ try {
290
+ return render(key, message, f, rest);
291
+ } catch {
292
+ return;
293
+ }
294
+ };
295
+ const lookup = (key, rest) => {
296
+ const remote = fromRemote(locale.code, key, own, rest);
297
+ if (remote !== undefined)
298
+ return remote;
299
+ const message = messageOf(store.get(locale.code), key);
300
+ if (message !== undefined) {
301
+ if (store.filled(locale.code)?.has(key))
302
+ report(`missing ${JSON.stringify(key)} in ${locale.code}`, "key", key, locale.code);
303
+ return render(key, message, own, rest);
304
+ }
305
+ report(`missing ${JSON.stringify(key)} in ${locale.code}`, "key", key, locale.code);
306
+ for (const code of chain) {
307
+ const info = byCode.get(code);
308
+ if (!info)
309
+ continue;
310
+ const f = formattersOf(info);
311
+ const fallbackRemote = fromRemote(code, key, f, rest);
312
+ if (fallbackRemote !== undefined)
313
+ return fallbackRemote;
314
+ const fallbackMessage = messageOf(store.get(code), key);
315
+ if (fallbackMessage !== undefined)
316
+ return render(key, fallbackMessage, f, rest);
317
+ }
318
+ return key;
319
+ };
320
+ const translator = {
321
+ locale,
322
+ t(key, ...rest) {
323
+ const out = lookup(key, rest);
324
+ return typeof out === "string" ? out : richText(out);
325
+ },
326
+ dynamic(key, args) {
327
+ const out = lookup(key, [args]);
328
+ return typeof out === "string" ? out : richText(out);
329
+ },
330
+ rich(key, args, handlers, join) {
331
+ const out = lookup(key, [args]);
332
+ return renderRich(typeof out === "string" ? [out] : out, handlers, join);
333
+ },
334
+ n: (value, options) => own.n(value, options),
335
+ d: (value, options) => own.d(value, options)
336
+ };
337
+ translators.set(locale.code, translator);
338
+ return translator;
339
+ };
340
+ }
341
+
342
+ // packages/i18n/src/runtime/locale.ts
343
+ var LOCALE_VARIANT = "locale:";
344
+ var configs = new WeakMap;
345
+ var matchers = new WeakMap;
346
+ var routeLoads = new WeakMap;
347
+ var scopedTranslators = new WeakMap;
348
+ function configOf(runtime) {
349
+ const config = configs.get(runtime);
350
+ if (!config)
351
+ throw new Error("i18n: not an I18nRuntime of @hamur/i18n");
352
+ return config;
353
+ }
354
+ var routeLoadOf = (runtime) => routeLoads.get(runtime);
355
+ function contextLocale(runtime, context) {
356
+ if (!context)
357
+ return;
358
+ const byCode = (code) => runtime.locales.find((locale) => locale.code === code);
359
+ const variant = context.cacheVariant;
360
+ return byCode(context.locale) ?? (typeof variant === "string" && variant.startsWith(LOCALE_VARIANT) ? byCode(variant.slice(LOCALE_VARIANT.length)) : undefined);
361
+ }
362
+ function readsRequestLocale(runtime) {
363
+ const config = configs.get(runtime);
364
+ return config?.strategy === "no_prefix" || config?.domains !== undefined;
365
+ }
366
+ function missingCopyOf(runtime, path) {
367
+ const { missing } = configOf(runtime);
368
+ if (!missing || Object.keys(missing).length === 0)
369
+ return;
370
+ const match = matchers.get(runtime)?.matchPath(path);
371
+ return match && missing[match.name]?.includes(match.code) ? match : undefined;
372
+ }
373
+ var copyMissing = (runtime, name, code) => configOf(runtime).missing?.[name]?.includes(code) === true;
374
+ function queryString(query) {
375
+ const search = new URLSearchParams;
376
+ for (const [key, value] of Object.entries(query ?? {}))
377
+ for (const item of Array.isArray(value) ? value : [value])
378
+ search.append(key, item);
379
+ const text = search.toString();
380
+ return text ? `?${text}` : "";
381
+ }
382
+ var hashOf = (hash) => !hash ? "" : hash.startsWith("#") ? hash : `#${hash}`;
383
+ function splitHref(href) {
384
+ const index = href.search(/[?#]/);
385
+ return index === -1 ? { path: href, rest: "" } : { path: href.slice(0, index), rest: href.slice(index) };
386
+ }
387
+ var routeBaseName = (name) => {
388
+ const index = name.indexOf("___");
389
+ return index === -1 ? name : name.slice(0, index);
390
+ };
391
+ var loadRuntime = {};
392
+ function createI18nRuntime(config) {
393
+ const matcher = createMatcher(config);
394
+ const byCode = new Map(config.locales.map((locale) => [locale.code, locale]));
395
+ const defaultLocale = byCode.get(config.defaultLocale);
396
+ const copies = new Set(config.copies);
397
+ const single = config.strategy === "no_prefix";
398
+ const prefixes = (code) => copies.has(code) && !(config.strategy === "prefix_and_default" && code === config.defaultLocale);
399
+ const translatorOf = createTranslators({
400
+ defaultLocale,
401
+ fallback: config.fallback ?? "default",
402
+ locales: config.locales,
403
+ timeZone: config.timeZone ?? "UTC",
404
+ dev: import.meta.dev === true,
405
+ store: messageStore
406
+ });
407
+ const localeFor = (code) => {
408
+ const locale = byCode.get(code);
409
+ if (!locale)
410
+ throw new Error(`i18n: "${code}" is not in i18n.locales`);
411
+ return locale;
412
+ };
413
+ const firstSegment = (path) => /^\/?([^/?#]*)/.exec(path)[1];
414
+ const runtime = {
415
+ locales: config.locales,
416
+ defaultLocale,
417
+ timeZone: config.timeZone ?? "UTC",
418
+ locale: defaultLocale,
419
+ async setLocale(code) {
420
+ localeFor(code);
421
+ throw new Error("i18n: setLocale needs the app's i18n (useApp().i18n)");
422
+ },
423
+ localeOf(path) {
424
+ const segment = firstSegment(path);
425
+ return copies.has(segment) ? byCode.get(segment) : defaultLocale;
426
+ },
427
+ load: (code) => loadMessages(localeFor(code).code),
428
+ translator: (code) => translatorOf(localeFor(code)),
429
+ localePath(to, code) {
430
+ localeFor(code);
431
+ if (typeof to === "string") {
432
+ if (single)
433
+ return to;
434
+ const { path, rest } = splitHref(to);
435
+ const match = matcher.matchPath(path);
436
+ return match ? matcher.buildPath(match.name, code, match.params) + rest : to;
437
+ }
438
+ const location2 = to;
439
+ const name = routeBaseName(location2.name);
440
+ if (!config.routes[name])
441
+ throw new Error(`i18n: no route named "${location2.name}"`);
442
+ return matcher.buildPath(name, single ? config.defaultLocale : code, location2.params) + queryString(location2.query) + hashOf(location2.hash);
443
+ },
444
+ switchLocalePath(route, code) {
445
+ localeFor(code);
446
+ const rest = queryString(route.query) + hashOf(route.hash);
447
+ if (single)
448
+ return route.path + rest;
449
+ const name = route.name ? routeBaseName(route.name) : undefined;
450
+ if (name && config.routes[name])
451
+ return matcher.buildPath(name, code, route.params) + rest;
452
+ const segments = route.path.split("/").filter(Boolean);
453
+ if (copies.has(segments[0]))
454
+ segments.shift();
455
+ if (prefixes(code))
456
+ segments.unshift(code);
457
+ return `/${segments.join("/")}${rest}`;
458
+ },
459
+ absoluteLocalePath() {
460
+ throw new Error("i18n: absoluteLocalePath needs the app's i18n (useApp().i18n)");
461
+ },
462
+ allLocalePaths() {
463
+ throw new Error("i18n: allLocalePaths needs the app's i18n (useApp().i18n)");
464
+ },
465
+ routeBaseName,
466
+ preferredLocaleList: () => negotiateAll((globalThis.navigator?.languages ?? []).join(","), config.locales)
467
+ };
468
+ configs.set(runtime, config);
469
+ matchers.set(runtime, matcher);
470
+ scopedTranslators.set(runtime, (code, scope) => translatorOf(localeFor(code), scope));
471
+ return runtime;
472
+ }
473
+
474
+ export { serverCatalog, installServerCatalog, negotiateAll, negotiate, messageStore, LOCALE_VARIANT, configOf, routeLoadOf, contextLocale, readsRequestLocale, missingCopyOf, copyMissing, routeBaseName, loadRuntime, createI18nRuntime };
@@ -0,0 +1,67 @@
1
+ // @bun
2
+ import"../../chunks/bun-index-pdepkyy1.js";
3
+ import {
4
+ onSetLocale,
5
+ currentLocale,
6
+ i18nOf,
7
+ bindFunctions
8
+ } from "../../chunks/bun-index-8bw4xpv8.js";
9
+
10
+ // packages/i18n/src/adapters/angular.ts
11
+ import { Pipe, assertInInjectionContext, computed, signal } from "@angular/core";
12
+ import { injectApp, injectRoute } from "@hamur/adapter-angular";
13
+ import * as i0 from "@angular/core";
14
+ var ESCAPES = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
15
+ var escapeHtml = (text) => text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
16
+ var join = (parts) => parts.map((part) => typeof part === "string" ? escapeHtml(part) : part.node).join("");
17
+ var holders = new WeakMap;
18
+ function holderOf(i18n) {
19
+ let holder = holders.get(i18n);
20
+ if (!holder) {
21
+ const created = signal(i18n.locale, ...ngDevMode ? [{ debugName: "created" }] : []);
22
+ holders.set(i18n, holder = created);
23
+ onSetLocale(i18n, () => created.set(i18n.locale));
24
+ }
25
+ return holder;
26
+ }
27
+ function injectLocale() {
28
+ assertInInjectionContext(injectLocale);
29
+ const i18n = i18nOf(injectApp());
30
+ const route = injectRoute();
31
+ const holder = holderOf(i18n);
32
+ return computed(() => {
33
+ holder();
34
+ return currentLocale(i18n, route().path);
35
+ });
36
+ }
37
+ function injectI18n() {
38
+ assertInInjectionContext(injectI18n);
39
+ const i18n = i18nOf(injectApp());
40
+ const route = injectRoute();
41
+ const holder = holderOf(i18n);
42
+ return {
43
+ locale: injectLocale(),
44
+ ...bindFunctions(() => {
45
+ holder();
46
+ return { i18n, route: route() };
47
+ }, join)
48
+ };
49
+ }
50
+
51
+ class TPipe {
52
+ translate = injectI18n().t;
53
+ transform(key, ...args) {
54
+ return this.translate(key, ...args);
55
+ }
56
+ static \u{275}fac = i0.\u{275}\u{275}ngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, deps: [], target: i0.\u{275}\u{275}FactoryTarget.Pipe });
57
+ static \u{275}pipe = i0.\u{275}\u{275}ngDeclarePipe({ minVersion: "14.0.0", version: "22.3.2", ngImport: i0, type: TPipe, isStandalone: true, name: "t", pure: false });
58
+ }
59
+ i0.\u{275}\u{275}ngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: TPipe, decorators: [{
60
+ type: Pipe,
61
+ args: [{ name: "t", pure: false }]
62
+ }] });
63
+ export {
64
+ TPipe,
65
+ injectI18n,
66
+ injectLocale
67
+ };
@@ -0,0 +1,34 @@
1
+ // @bun
2
+ import"../../chunks/bun-index-pdepkyy1.js";
3
+ import {
4
+ currentLocale,
5
+ i18nOf,
6
+ bindFunctions
7
+ } from "../../chunks/bun-index-8bw4xpv8.js";
8
+
9
+ // packages/i18n/src/adapters/html.ts
10
+ import { useApp, useRoute } from "@hamur/adapter-html";
11
+ var ESCAPES = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
12
+ var escapeHtml = (text) => text.replace(/[&<>"']/g, (char) => ESCAPES[char]);
13
+ var join = (parts) => parts.map((part) => typeof part === "string" ? escapeHtml(part) : part.node).join("");
14
+ function useLocale() {
15
+ return currentLocale(i18nOf(useApp()), useRoute().path);
16
+ }
17
+ function useI18n() {
18
+ const i18n = i18nOf(useApp());
19
+ const route = useRoute();
20
+ const bound = bindFunctions(() => ({ i18n, route }), join);
21
+ return {
22
+ ...bound,
23
+ t: Object.assign((key, ...args) => escapeHtml(bound.t(key, ...args)), {
24
+ rich: bound.t.rich,
25
+ dynamic: (key, args) => escapeHtml(bound.t.dynamic(key, args))
26
+ }),
27
+ n: (value, options) => escapeHtml(bound.n(value, options)),
28
+ d: (value, options) => escapeHtml(bound.d(value, options))
29
+ };
30
+ }
31
+ export {
32
+ useI18n,
33
+ useLocale
34
+ };
@@ -0,0 +1,23 @@
1
+ // @bun
2
+ import"../../chunks/bun-index-pdepkyy1.js";
3
+ import {
4
+ currentLocale,
5
+ i18nOf,
6
+ sharedFunctions
7
+ } from "../../chunks/bun-index-8bw4xpv8.js";
8
+
9
+ // packages/i18n/src/adapters/preact.ts
10
+ import { useApp, useRoute } from "@hamur/adapter-preact";
11
+ import { createElement, Fragment } from "preact";
12
+ var join = (parts) => parts.map((part, index) => typeof part === "string" ? part : createElement(Fragment, { key: index }, part.node));
13
+ function useLocale() {
14
+ return currentLocale(i18nOf(useApp()), useRoute().path);
15
+ }
16
+ function useI18n() {
17
+ const app = useApp();
18
+ return sharedFunctions(app, i18nOf(app), useRoute(), join);
19
+ }
20
+ export {
21
+ useI18n,
22
+ useLocale
23
+ };
@@ -0,0 +1,23 @@
1
+ // @bun
2
+ import"../../chunks/bun-index-pdepkyy1.js";
3
+ import {
4
+ currentLocale,
5
+ i18nOf,
6
+ sharedFunctions
7
+ } from "../../chunks/bun-index-8bw4xpv8.js";
8
+
9
+ // packages/i18n/src/adapters/react.ts
10
+ import { useApp, useRoute } from "@hamur/adapter-react";
11
+ import { createElement, Fragment } from "react";
12
+ var join = (parts) => parts.map((part, index) => typeof part === "string" ? part : createElement(Fragment, { key: index }, part.node));
13
+ function useLocale() {
14
+ return currentLocale(i18nOf(useApp()), useRoute().path);
15
+ }
16
+ function useI18n() {
17
+ const app = useApp();
18
+ return sharedFunctions(app, i18nOf(app), useRoute(), join);
19
+ }
20
+ export {
21
+ useI18n,
22
+ useLocale
23
+ };
@@ -0,0 +1,48 @@
1
+ // @bun
2
+ import"../../chunks/bun-index-pdepkyy1.js";
3
+ import {
4
+ onSetLocale,
5
+ currentLocale,
6
+ i18nOf,
7
+ bindFunctions
8
+ } from "../../chunks/bun-index-8bw4xpv8.js";
9
+
10
+ // packages/i18n/src/adapters/solid.ts
11
+ import { createMemo, createSignal } from "solid-js";
12
+ import { useApp, useRoute } from "@hamur/adapter-solid";
13
+ var join = (parts) => parts.map((part) => typeof part === "string" ? part : part.node);
14
+ var holders = new WeakMap;
15
+ function holderOf(i18n) {
16
+ let holder = holders.get(i18n);
17
+ if (!holder) {
18
+ const [read, write] = createSignal(i18n.locale);
19
+ holders.set(i18n, holder = read);
20
+ onSetLocale(i18n, () => void write(i18n.locale));
21
+ }
22
+ return holder;
23
+ }
24
+ function useLocale() {
25
+ const i18n = i18nOf(useApp());
26
+ const route = useRoute();
27
+ const holder = holderOf(i18n);
28
+ return createMemo(() => {
29
+ holder();
30
+ return currentLocale(i18n, route().path);
31
+ });
32
+ }
33
+ function useI18n() {
34
+ const i18n = i18nOf(useApp());
35
+ const route = useRoute();
36
+ const holder = holderOf(i18n);
37
+ return {
38
+ locale: useLocale(),
39
+ ...bindFunctions(() => {
40
+ holder();
41
+ return { i18n, route: route() };
42
+ }, join)
43
+ };
44
+ }
45
+ export {
46
+ useI18n,
47
+ useLocale
48
+ };