@vielzeug/lingua 1.1.3 → 2.0.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.
Files changed (110) hide show
  1. package/README.md +15 -54
  2. package/dist/_bounded-cache.cjs +2 -0
  3. package/dist/_bounded-cache.cjs.map +1 -0
  4. package/dist/_bounded-cache.d.ts +2 -0
  5. package/dist/_bounded-cache.d.ts.map +1 -0
  6. package/dist/_bounded-cache.js +10 -0
  7. package/dist/_bounded-cache.js.map +1 -0
  8. package/dist/_catalog.cjs +1 -1
  9. package/dist/_catalog.cjs.map +1 -1
  10. package/dist/_catalog.d.ts +13 -16
  11. package/dist/_catalog.d.ts.map +1 -1
  12. package/dist/_catalog.js +43 -36
  13. package/dist/_catalog.js.map +1 -1
  14. package/dist/_dev.cjs +1 -1
  15. package/dist/_dev.cjs.map +1 -1
  16. package/dist/_dev.js +3 -8
  17. package/dist/_dev.js.map +1 -1
  18. package/dist/_locale.cjs +2 -0
  19. package/dist/_locale.cjs.map +1 -0
  20. package/dist/_locale.d.ts +5 -0
  21. package/dist/_locale.d.ts.map +1 -0
  22. package/dist/_locale.js +26 -0
  23. package/dist/_locale.js.map +1 -0
  24. package/dist/_resources.cjs +2 -0
  25. package/dist/_resources.cjs.map +1 -0
  26. package/dist/_resources.d.ts +10 -0
  27. package/dist/_resources.d.ts.map +1 -0
  28. package/dist/_resources.js +51 -0
  29. package/dist/_resources.js.map +1 -0
  30. package/dist/_template.cjs +2 -0
  31. package/dist/_template.cjs.map +1 -0
  32. package/dist/_template.d.ts +8 -0
  33. package/dist/_template.d.ts.map +1 -0
  34. package/dist/_template.js +32 -0
  35. package/dist/_template.js.map +1 -0
  36. package/dist/errors.cjs +1 -1
  37. package/dist/errors.cjs.map +1 -1
  38. package/dist/errors.d.ts +6 -18
  39. package/dist/errors.d.ts.map +1 -1
  40. package/dist/errors.js +3 -9
  41. package/dist/errors.js.map +1 -1
  42. package/dist/format.cjs +1 -1
  43. package/dist/format.cjs.map +1 -1
  44. package/dist/format.d.ts +0 -9
  45. package/dist/format.d.ts.map +1 -1
  46. package/dist/format.js +40 -46
  47. package/dist/format.js.map +1 -1
  48. package/dist/i18n.cjs +1 -1
  49. package/dist/i18n.cjs.map +1 -1
  50. package/dist/i18n.d.ts +25 -39
  51. package/dist/i18n.d.ts.map +1 -1
  52. package/dist/i18n.js +65 -218
  53. package/dist/i18n.js.map +1 -1
  54. package/dist/index.cjs +1 -1
  55. package/dist/index.d.ts +4 -12
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +4 -4
  58. package/dist/lingua.cjs +1 -1
  59. package/dist/lingua.cjs.map +1 -1
  60. package/dist/lingua.iife.js +1 -1
  61. package/dist/lingua.iife.js.map +1 -1
  62. package/dist/lingua.js +1 -1
  63. package/dist/lingua.js.map +1 -1
  64. package/dist/translator.cjs +2 -0
  65. package/dist/translator.cjs.map +1 -0
  66. package/dist/translator.d.ts +20 -0
  67. package/dist/translator.d.ts.map +1 -0
  68. package/dist/translator.js +61 -0
  69. package/dist/translator.js.map +1 -0
  70. package/dist/types.d.ts +53 -0
  71. package/dist/types.d.ts.map +1 -0
  72. package/dist/validate.cjs +1 -1
  73. package/dist/validate.cjs.map +1 -1
  74. package/dist/validate.d.ts +5 -36
  75. package/dist/validate.d.ts.map +1 -1
  76. package/dist/validate.js +21 -52
  77. package/dist/validate.js.map +1 -1
  78. package/package.json +4 -4
  79. package/dist/_catalog-store.cjs +0 -2
  80. package/dist/_catalog-store.cjs.map +0 -1
  81. package/dist/_catalog-store.d.ts +0 -33
  82. package/dist/_catalog-store.d.ts.map +0 -1
  83. package/dist/_catalog-store.js +0 -74
  84. package/dist/_catalog-store.js.map +0 -1
  85. package/dist/_chain.cjs +0 -2
  86. package/dist/_chain.cjs.map +0 -1
  87. package/dist/_chain.d.ts +0 -21
  88. package/dist/_chain.d.ts.map +0 -1
  89. package/dist/_chain.js +0 -45
  90. package/dist/_chain.js.map +0 -1
  91. package/dist/_constants.cjs +0 -2
  92. package/dist/_constants.cjs.map +0 -1
  93. package/dist/_constants.d.ts +0 -3
  94. package/dist/_constants.d.ts.map +0 -1
  95. package/dist/_constants.js +0 -17
  96. package/dist/_constants.js.map +0 -1
  97. package/dist/_namespace-store.cjs +0 -2
  98. package/dist/_namespace-store.cjs.map +0 -1
  99. package/dist/_namespace-store.d.ts +0 -31
  100. package/dist/_namespace-store.d.ts.map +0 -1
  101. package/dist/_namespace-store.js +0 -54
  102. package/dist/_namespace-store.js.map +0 -1
  103. package/dist/i18n-types.d.ts +0 -250
  104. package/dist/i18n-types.d.ts.map +0 -1
  105. package/dist/template.cjs +0 -2
  106. package/dist/template.cjs.map +0 -1
  107. package/dist/template.d.ts +0 -15
  108. package/dist/template.d.ts.map +0 -1
  109. package/dist/template.js +0 -49
  110. package/dist/template.js.map +0 -1
@@ -1,250 +0,0 @@
1
- import type { Messages } from './_catalog';
2
- import type { LocaleSource } from './_catalog-store';
3
- import type { NamespaceFactory } from './_namespace-store';
4
- import type { Formatter } from './format';
5
- export type Locale = string;
6
- export type Unsubscribe = () => void;
7
- export type { Messages } from './_catalog';
8
- export type { Loader, LocaleSource } from './_catalog-store';
9
- export type { NamespaceFactory } from './_namespace-store';
10
- export type TranslateVars = Record<string, unknown>;
11
- /**
12
- * A snapshot of the i18n instance state at a point in time.
13
- * Object identity changes on every observable change (locale switch, catalog load).
14
- * `t` and `tp` are bound to the locale captured in this snapshot.
15
- */
16
- export type I18nSnapshot = {
17
- readonly locale: Locale;
18
- /** @security Returns raw, unsanitized strings. Sanitize before `innerHTML` insertion. */
19
- readonly t: (key: string, vars?: TranslateVars) => string;
20
- readonly tp: (key: string, count: number, options?: TpOptions) => string;
21
- };
22
- /** Shape of the serialised state produced by `serializeI18n()`. Pass to `hydrateI18n()` on the client. */
23
- export type I18nState = {
24
- readonly catalogs: Record<Locale, Record<string, string>>;
25
- readonly locale: Locale;
26
- };
27
- export type SubscribeOptions = {
28
- immediate?: boolean;
29
- /** AbortSignal — automatically unsubscribes when the signal is aborted. */
30
- signal?: AbortSignal;
31
- };
32
- export type TpOptions = {
33
- /** Use ordinal plural rules (1st, 2nd, 3rd) instead of cardinal (default: `false`). */
34
- ordinal?: boolean;
35
- /** Inject additional interpolation variables alongside the automatically injected `count`. */
36
- vars?: TranslateVars;
37
- };
38
- export type ScopedI18n = {
39
- /** Intl formatter inherited from the parent instance. Follows locale changes automatically. */
40
- readonly fmt: Formatter;
41
- has(key: string): boolean;
42
- t(key: string, vars?: TranslateVars): string;
43
- tp(key: string, count: number, options?: TpOptions): string;
44
- };
45
- type Depth = [never, 0, 1, 2, 3, 4, 5, 6];
46
- export type MessageLeafKeys<T, P extends string = '', D extends number = 7> = [D] extends [0] ? never : T extends string ? P : T extends Record<string, unknown> ? {
47
- [K in string & keyof T]: MessageLeafKeys<T[K], P extends '' ? K : `${P}.${K}`, Depth[D]>;
48
- }[string & keyof T] : never;
49
- export type MessageBranchKeys<T, P extends string = '', D extends number = 7> = [D] extends [0] ? never : T extends Record<string, unknown> ? {
50
- [K in string & keyof T]: T[K] extends string ? never : (P extends '' ? K : `${P}.${K}`) | MessageBranchKeys<T[K], P extends '' ? K : `${P}.${K}`, Depth[D]>;
51
- }[string & keyof T] : never;
52
- export type I18nOptions<M extends Messages = Messages> = {
53
- /** Locale registry. Values can be static message objects or async loaders. */
54
- catalogs?: Record<Locale, LocaleSource<M>>;
55
- /** Locale(s) to search when the active locale is missing a key. Subtags are expanded automatically (e.g. `en-US` → `en`). */
56
- fallback?: Locale | Locale[];
57
- /** Initial active locale. Defaults to `"en"`. Canonicalized via `Intl.getCanonicalLocales`. */
58
- locale?: Locale;
59
- /**
60
- * Called when a translation key is missing.
61
- * Defaults to returning the key string.
62
- *
63
- * @security The default handler returns `key` verbatim. Do not render the return value as HTML
64
- * if keys are constructed from untrusted user input.
65
- */
66
- onMissingKey?: (key: string, locale: Locale) => string;
67
- /**
68
- * Called when an interpolation variable is missing.
69
- * Defaults to returning `{varName}`.
70
- */
71
- onMissingVar?: (varName: string, key: string, locale: Locale) => string;
72
- /**
73
- * Called when a subscriber callback throws. Defaults to `console.error`.
74
- * Override in production to route errors to a structured logger rather than the browser console.
75
- */
76
- onSubscriberError?: (error: unknown) => void;
77
- };
78
- export type I18n<M extends Messages = Messages> = {
79
- /** Delegates to `dispose()`. Enables `using` declarations. */
80
- [Symbol.dispose](): void;
81
- /** `AbortSignal` aborted when `dispose()` is called. Use to tie external lifetimes to this instance. */
82
- readonly disposalSignal: AbortSignal;
83
- /**
84
- * Disposes this i18n instance: removes all subscribers and clears catalog, loader, and namespace state.
85
- * After disposal, all mutation methods throw `LinguaDisposedError` and translation methods
86
- * fall back to `onMissingKey` for every key. Idempotent.
87
- */
88
- dispose(): void;
89
- /** `true` after `dispose()` has been called. */
90
- readonly disposed: boolean;
91
- /**
92
- * Registers a namespace factory and immediately starts loading it for the given locale
93
- * (defaults to the active locale). Deduplicates concurrent and repeated calls.
94
- *
95
- * @remarks
96
- * Call `registerNamespace()` first if you only want to register without loading.
97
- * `extend()` is a convenience that does both in one call.
98
- *
99
- * @throws `LinguaDisposedError` if called on a disposed instance.
100
- *
101
- * @example
102
- * await i18n.extend('settings', (locale) =>
103
- * import(`./locales/${locale}/settings.json`).then((m) => m.default),
104
- * );
105
- */
106
- extend(ns: string, factory: NamespaceFactory, locale?: Locale): Promise<void>;
107
- /** Intl formatter bound to this instance's locale. Follows locale changes automatically. */
108
- readonly fmt: Formatter;
109
- /**
110
- * Creates a derived instance that inherits the current catalog snapshot, loaders,
111
- * namespace registry, and loaded-namespace markers, but has its own locale, fallback chain,
112
- * and subscribers. Catalog mutations on the fork do not affect the parent.
113
- *
114
- * Resolved catalog entries are shared by reference (no template re-compilation), making
115
- * `fork()` cheap for SSR fork-per-request patterns with large catalogs.
116
- *
117
- * @example
118
- * // SSR: per-request locale without touching the shared instance
119
- * const reqI18n = i18n.fork({ locale: req.locale });
120
- */
121
- fork(overrides?: Omit<I18nOptions<M>, 'catalogs'>): I18n<M>;
122
- /** Returns the current snapshot. Object identity changes on every observable change. */
123
- getSnapshot(): I18nSnapshot;
124
- /**
125
- * Extracts a serializable snapshot of all loaded catalogs and the active locale.
126
- * Pass the result to `hydrateI18n()` on the client.
127
- *
128
- * **Warning:** Only fully resolved catalogs are included. Loader-only locales not yet
129
- * preloaded are omitted. Use `i18n.isLoaded(locale)` to verify before calling.
130
- *
131
- * **Warning:** The namespace registry is **not** serialized — factory functions cannot
132
- * be converted to JSON. After `hydrateI18n()`, call `extend()` again for each namespace
133
- * before relying on namespace-patched keys.
134
- */
135
- getState(): I18nState;
136
- /**
137
- * Returns all registered locales.
138
- * - Default (no argument): locales in registration order.
139
- * - `getSupportedLocales(true)`: sorted in ascending code-point order.
140
- */
141
- getSupportedLocales(sorted?: boolean): Locale[];
142
- /**
143
- * Returns `true` if the given key exists in the active fallback chain — either as a leaf
144
- * string key or as a plural branch (any key under `key.` prefix).
145
- *
146
- * @example
147
- * i18n.has('inbox') // true for leaf or pipe-plural expanded branch
148
- * i18n.has('inbox.one') // true for explicit sub-key
149
- */
150
- has(key: MessageLeafKeys<M> | MessageBranchKeys<M> | (string & {})): boolean;
151
- /**
152
- * Returns `true` if the catalog for `locale` is fully resolved.
153
- * Returns `false` for locales registered as async loaders not yet preloaded, and for unknown locales.
154
- */
155
- isLoaded(locale: Locale): boolean;
156
- /**
157
- * Returns `true` if the namespace has been fully loaded for the given locale.
158
- * Returns `false` if it is not registered or not yet loaded for this locale.
159
- */
160
- isNamespaceLoaded(ns: string, locale?: Locale): boolean;
161
- /**
162
- * Returns `true` if a namespace factory is registered under the given name.
163
- */
164
- isNamespaceRegistered(ns: string): boolean;
165
- /**
166
- * Returns `true` if `locale` is in the known locale registry — either resolved or pending loader.
167
- * Returns `false` for locales that have never been registered.
168
- */
169
- isRegistered(locale: Locale): boolean;
170
- /**
171
- * Loads a previously registered namespace for the given locale (defaults to the active locale).
172
- * Deduplicates concurrent and repeated calls.
173
- *
174
- * @throws `LinguaNamespaceMissingError` if the namespace has not been registered with `registerNamespace()` first.
175
- * @throws `LinguaDisposedError` if called on a disposed instance.
176
- */
177
- loadNamespace(ns: string, locale?: Locale): Promise<void>;
178
- readonly locale: Locale;
179
- preload(locale: Locale): Promise<void>;
180
- /**
181
- * Registers (or replaces) a locale source. If the source is an async loader, it is loaded
182
- * immediately and this method returns a Promise that resolves when the load is complete.
183
- * If the source is a static message object, it is synchronously registered and the returned
184
- * Promise resolves immediately.
185
- *
186
- * @throws `LinguaDisposedError` if called on a disposed instance.
187
- */
188
- register(locale: Locale, source: LocaleSource<M>): Promise<void>;
189
- /**
190
- * Registers a namespace factory without loading it. Use `loadNamespace()` to trigger loading,
191
- * or use `extend()` to register and load in one call.
192
- *
193
- * @remarks
194
- * Re-registering a namespace updates the factory for future loads but does **not** reload
195
- * the namespace if it is already loaded. The new factory takes effect the next time the
196
- * namespace marker is cleared (by a `register()` or `restoreState()` call).
197
- *
198
- * @throws `LinguaDisposedError` if called on a disposed instance.
199
- */
200
- registerNamespace(ns: string, factory: NamespaceFactory): void;
201
- /**
202
- * Hydrates this instance with pre-loaded state (e.g. from a server-rendered payload).
203
- *
204
- * @remarks The namespace registry is **not** included in `I18nState`. After restoring,
205
- * call `extend()` for each namespace before relying on namespace-patched keys.
206
- *
207
- * @throws `LinguaDisposedError` if called on a disposed instance.
208
- * @throws `LinguaRestoreError` if the state's locale has no catalog.
209
- */
210
- restoreState(state: I18nState): void;
211
- /**
212
- * Returns a scoped translator. All `t()` / `tp()` calls are automatically prefixed with `${prefix}.`.
213
- * The returned object is memoized — calling `scope(prefix)` with the same string always returns the
214
- * same reference.
215
- *
216
- * @example
217
- * const nav = i18n.scope('nav');
218
- * nav.t('home'); // i18n.t('nav.home')
219
- * nav.tp('items', 3); // i18n.tp('nav.items', 3)
220
- */
221
- scope(prefix: MessageBranchKeys<M> | (string & {})): ScopedI18n;
222
- /**
223
- * Switches the active locale. Loads the locale if it is registered as an async loader.
224
- * Last concurrent call wins; stale responses are discarded.
225
- * If loading fails, the active locale is unchanged.
226
- *
227
- * @throws `LinguaMissingLocaleError` if the locale is not registered.
228
- * @throws `LinguaDisposedError` if called on a disposed instance.
229
- */
230
- setLocale(locale: Locale): Promise<void>;
231
- /**
232
- * Subscribes to locale/catalog changes.
233
- * - `{ immediate: true }`: fires immediately and on every change.
234
- * - `{ signal }`: unsubscribes when the AbortSignal fires.
235
- *
236
- * @throws `LinguaDisposedError` if called on a disposed instance.
237
- */
238
- subscribe(callback: (snapshot: I18nSnapshot) => void, options?: SubscribeOptions): Unsubscribe;
239
- /** @security Returns raw, unsanitized strings. Sanitize before `innerHTML` insertion. */
240
- t(key: MessageLeafKeys<M> | (string & {}), vars?: TranslateVars): string;
241
- /**
242
- * Translates a plural branch key. `count` is injected automatically.
243
- *
244
- * @throws `LinguaInvalidCountError` if `count` is not finite.
245
- * @throws `LinguaCountInVarsError` if `options.vars.count` is set.
246
- * @security Returns raw, unsanitized strings.
247
- */
248
- tp(key: MessageBranchKeys<M> | (string & {}), count: number, options?: TpOptions): string;
249
- };
250
- //# sourceMappingURL=i18n-types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"i18n-types.d.ts","sourceRoot":"","sources":["../src/i18n-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC;AAC5B,MAAM,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;AAErC,YAAY,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAC3C,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAC7D,YAAY,EAAE,gBA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AM,EAAE,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAC;IAC/D;;;;;;;;OAQG;IACH,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACrC;;;;;;;;;OASG;IACH,KAAK,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,GAAG,UAAU,CAAC;IAChE;;;;;;;OAOG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,YAAY,KAAK,IAAI,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,WAAW,CAAC;IAC/F,yFAAyF;IACzF,CAAC,CAAC,GAAG,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,EAAE,IAAI,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;IACzE;;;;;;OAMG;IACH,EAAE,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;CAC3F,CAAC"}
package/dist/template.cjs DELETED
@@ -1,2 +0,0 @@
1
- const e=require("./_dev.cjs");var t={2:[`one`,`other`],3:[`zero`,`one`,`other`],6:[`zero`,`one`,`two`,`few`,`many`,`other`]};function n(n){if(n.indexOf(`|`)===-1)return null;let r=n.split(`|`),i=t[r.length];if(!i)return e.warn(`Pipe-plural shorthand with ${r.length} parts is not recognized (valid counts: 2, 3, 6). Treating as a plain string: "${n.slice(0,60)}${n.length>60?`…`:``}"`),null;if(r.some(e=>e.trim()===``))return null;let a={};for(let e=0;e<r.length;e++)a[i[e]]=r[e];return a}var r=/\{([\p{ID_Continue}-]+)\}/gu;function i(e){let t=[],n=0;for(let i of e.matchAll(r)){let{index:r}=i,a=r??0;a>n&&t.push(e.slice(n,a)),t.push({var:i[1]}),n=a+i[0].length}return n<e.length&&t.push(e.slice(n)),t}function a(e,t,n,r,i){let a=``;for(let o of e)if(typeof o==`string`)a+=o;else{let e=t?.[o.var];a+=e==null?i(o.var,n,r):String(e)}return a}exports.INTERPOLATION_PATTERN=r,exports.compileTemplate=i,exports.parsePipePlural=n,exports.renderTemplate=a;
2
- //# sourceMappingURL=template.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"template.cjs","names":[],"sources":["../src/template.ts"],"sourcesContent":["// ─── Template helpers ─────────────────────────────────────────────────────────\n// Shared by i18n.ts — extracted for navigability. Not part of the public API.\n\nimport { warn } from './_dev';\n\ntype Locale = string;\ntype TranslateVars = Record<string, unknown>;\n\ninterface Messages {\n [key: string]: string | Messages;\n}\n\n// ─── Pipe-plural shorthand (F1) ───────────────────────────────────────────────\n// A leaf string containing '|' is parsed as a pipe-delimited plural shorthand.\n// Supported part counts and their form mappings (positional, left-to-right):\n// 2 parts → one | other\n// 3 parts → zero | one | other\n// 6 parts → zero | one | two | few | many | other\n// Any other part count is treated as a plain string with no expansion.\n\nconst PIPE_FORM_MAPS: Readonly<Partial<Record<number, readonly string[]>>> = {\n 2: ['one', 'other'],\n 3: ['zero', 'one', 'other'],\n 6: ['zero', 'one', 'two', 'few', 'many', 'other'],\n};\n\nexport function parsePipePlural(value: string): Messages | null {\n const pipeIdx = value.indexOf('|');\n\n if (pipeIdx === -1) return null;\n\n const parts = value.split('|');\n const forms = PIPE_FORM_MAPS[parts.length];\n\n if (!forms) {\n warn(\n `Pipe-plural shorthand with ${parts.length} parts is not recognized (valid counts: 2, 3, 6). Treating as a plain string: \"${value.slice(0, 60)}${value.length > 60 ? '…' : ''}\"`,\n );\n\n return null;\n }\n\n // An empty part is almost certainly a catalog authoring error.\n // Treat the whole value as a plain string rather than silently producing an empty form.\n if (parts.some((p) => p.trim() === '')) return null;\n\n const result: Messages = {};\n\n for (let i = 0; i < parts.length; i++) {\n result[forms[i]] = parts[i];\n }\n\n return result;\n}\n\n// ─── Template compilation ─────────────────────────────────────────────────────\n// Message strings are parsed once at registration time into a pre-compiled form:\n// an array of static strings and variable names. Rendering a compiled template\n// skips the regex entirely.\n\ntype TemplatePart = string | { var: string };\nexport type CompiledTemplate = TemplatePart[];\n\nexport const INTERPOLATION_PATTERN = /\\{([\\p{ID_Continue}-]+)\\}/gu;\n\nexport function compileTemplate(template: string): CompiledTemplate {\n const parts: CompiledTemplate = [];\n let lastIndex = 0;\n\n for (const match of template.matchAll(INTERPOLATION_PATTERN)) {\n const { index } = match;\n const start = index ?? 0;\n\n if (start > lastIndex) {\n parts.push(template.slice(lastIndex, start));\n }\n\n parts.push({ var: match[1] });\n lastIndex = start + match[0].length;\n }\n\n if (lastIndex < template.length) {\n parts.push(template.slice(lastIndex));\n }\n\n return parts;\n}\n\nexport function renderTemplate(\n parts: CompiledTemplate,\n vars: TranslateVars | undefined,\n key: string,\n locale: Locale,\n onMissingVar: (varName: string, key: string, locale: Locale) => string,\n): string {\n let result = '';\n\n for (const part of parts) {\n if (typeof part === 'string') {\n result += part;\n } else {\n const value = vars?.[part.var];\n\n result += value == null ? onMissingVar(part.var, key, locale) : String(value);\n }\n }\n\n return result;\n}\n"],"mappings":"8BAoBA,IAAM,EAAuE,CAC3E,EAAG,CAAC,MAAO,OAAO,EAClB,EAAG,CAAC,OAAQ,MAAO,OAAO,EAC1B,EAAG,CAAC,OAAQ,MAAO,MAAO,MAAO,OAAQ,OAAO,CAClD,EAEA,SAAgB,EAAgB,EAAgC,CAG9D,GAFgB,EAAM,QAAQ,GAE1B,IAAY,GAAI,OAAO,KAE3B,IAAM,EAAQ,EAAM,MAAM,GAAG,EACvB,EAAQ,EAAe,EAAM,QAEnC,GAAI,CAAC,EAKH,OAJA,EAAA,KACE,8BAA8B,EAAM,OAAO,iFAAiF,EAAM,MAAM,EAAG,EAAE,IAAI,EAAM,OAAS,GAAK,IAAM,GAAG,EAChL,EAEO,KAKT,GAAI,EAAM,KAAM,GAAM,EAAE,KAAK,IAAM,EAAE,EAAG,OAAO,KAE/C,IAAM,EAAmB,CAAC,EAE1B,IAAK,IAAI,EAAI,EAAG,EAAI,EAAM,OAAQ,IAChC,EAAO,EAAM,IAAM,EAAM,GAG3B,OAAO,CACT,CAUA,IAAa,EAAwB,8BAErC,SAAgB,EAAgB,EAAoC,CAClE,IAAM,EAA0B,CAAC,EAC7B,EAAY,EAEhB,IAAK,IAAM,KAAS,EAAS,SAAS,CAAqB,EAAG,CAC5D,GAAM,CAAE,SAAU,EACZ,EAAQ,GAAS,EAEnB,EAAQ,GACV,EAAM,KAAK,EAAS,MAAM,EAAW,CAAK,CAAC,EAG7C,EAAM,KAAK,CAAE,IAAK,EAAM,EAAG,CAAC,EAC5B,EAAY,EAAQ,EAAM,EAAE,CAAC,MAC/B,CAMA,OAJI,EAAY,EAAS,QACvB,EAAM,KAAK,EAAS,MAAM,CAAS,CAAC,EAG/B,CACT,CAEA,SAAgB,EACd,EACA,EACA,EACA,EACA,EACQ,CACR,IAAI,EAAS,GAEb,IAAK,IAAM,KAAQ,EACjB,GAAI,OAAO,GAAS,SAClB,GAAU,MACL,CACL,IAAM,EAAQ,IAAO,EAAK,KAE1B,GAAU,GAAS,KAAO,EAAa,EAAK,IAAK,EAAK,CAAM,EAAI,OAAO,CAAK,CAC9E,CAGF,OAAO,CACT"}
@@ -1,15 +0,0 @@
1
- type Locale = string;
2
- type TranslateVars = Record<string, unknown>;
3
- interface Messages {
4
- [key: string]: string | Messages;
5
- }
6
- export declare function parsePipePlural(value: string): Messages | null;
7
- type TemplatePart = string | {
8
- var: string;
9
- };
10
- export type CompiledTemplate = TemplatePart[];
11
- export declare const INTERPOLATION_PATTERN: RegExp;
12
- export declare function compileTemplate(template: string): CompiledTemplate;
13
- export declare function renderTemplate(parts: CompiledTemplate, vars: TranslateVars | undefined, key: string, locale: Locale, onMissingVar: (varName: string, key: string, locale: Locale) => string): string;
14
- export {};
15
- //# sourceMappingURL=template.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"template.d.ts","sourceRoot":"","sources":["../src/template.ts"],"names":[],"mappings":"AAKA,KAAK,MAAM,GAAG,MAAM,CAAC;AACrB,KAAK,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE7C,UAAU,QAAQ;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;CAClC;AAgBD,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CA2B9D;AAOD,KAAK,YAAY,GAAG,MAAM,GAAG;IAAE,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAC7C,MAAM,MAAM,gBAAgB,GAAG,YAAY,EAAE,CAAC;AAE9C,eAAO,MAAM,qBAAqB,QAAgC,CAAC;AAEnE,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,gBAAgB,CAqBlE;AAED,wBAAgB,cAAc,CAC5B,KAAK,EAAE,gBAAgB,EACvB,IAAI,EAAE,aAAa,GAAG,SAAS,EAC/B,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,GACrE,MAAM,CAcR"}
package/dist/template.js DELETED
@@ -1,49 +0,0 @@
1
- import { warn as e } from "./_dev.js";
2
- //#region src/template.ts
3
- var t = {
4
- 2: ["one", "other"],
5
- 3: [
6
- "zero",
7
- "one",
8
- "other"
9
- ],
10
- 6: [
11
- "zero",
12
- "one",
13
- "two",
14
- "few",
15
- "many",
16
- "other"
17
- ]
18
- };
19
- function n(n) {
20
- if (n.indexOf("|") === -1) return null;
21
- let r = n.split("|"), i = t[r.length];
22
- if (!i) return e(`Pipe-plural shorthand with ${r.length} parts is not recognized (valid counts: 2, 3, 6). Treating as a plain string: "${n.slice(0, 60)}${n.length > 60 ? "…" : ""}"`), null;
23
- if (r.some((e) => e.trim() === "")) return null;
24
- let a = {};
25
- for (let e = 0; e < r.length; e++) a[i[e]] = r[e];
26
- return a;
27
- }
28
- var r = /\{([\p{ID_Continue}-]+)\}/gu;
29
- function i(e) {
30
- let t = [], n = 0;
31
- for (let i of e.matchAll(r)) {
32
- let { index: r } = i, a = r ?? 0;
33
- a > n && t.push(e.slice(n, a)), t.push({ var: i[1] }), n = a + i[0].length;
34
- }
35
- return n < e.length && t.push(e.slice(n)), t;
36
- }
37
- function a(e, t, n, r, i) {
38
- let a = "";
39
- for (let o of e) if (typeof o == "string") a += o;
40
- else {
41
- let e = t?.[o.var];
42
- a += e == null ? i(o.var, n, r) : String(e);
43
- }
44
- return a;
45
- }
46
- //#endregion
47
- export { r as INTERPOLATION_PATTERN, i as compileTemplate, n as parsePipePlural, a as renderTemplate };
48
-
49
- //# sourceMappingURL=template.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"template.js","names":[],"sources":["../src/template.ts"],"sourcesContent":["// ─── Template helpers ─────────────────────────────────────────────────────────\n// Shared by i18n.ts — extracted for navigability. Not part of the public API.\n\nimport { warn } from './_dev';\n\ntype Locale = string;\ntype TranslateVars = Record<string, unknown>;\n\ninterface Messages {\n [key: string]: string | Messages;\n}\n\n// ─── Pipe-plural shorthand (F1) ───────────────────────────────────────────────\n// A leaf string containing '|' is parsed as a pipe-delimited plural shorthand.\n// Supported part counts and their form mappings (positional, left-to-right):\n// 2 parts → one | other\n// 3 parts → zero | one | other\n// 6 parts → zero | one | two | few | many | other\n// Any other part count is treated as a plain string with no expansion.\n\nconst PIPE_FORM_MAPS: Readonly<Partial<Record<number, readonly string[]>>> = {\n 2: ['one', 'other'],\n 3: ['zero', 'one', 'other'],\n 6: ['zero', 'one', 'two', 'few', 'many', 'other'],\n};\n\nexport function parsePipePlural(value: string): Messages | null {\n const pipeIdx = value.indexOf('|');\n\n if (pipeIdx === -1) return null;\n\n const parts = value.split('|');\n const forms = PIPE_FORM_MAPS[parts.length];\n\n if (!forms) {\n warn(\n `Pipe-plural shorthand with ${parts.length} parts is not recognized (valid counts: 2, 3, 6). Treating as a plain string: \"${value.slice(0, 60)}${value.length > 60 ? '…' : ''}\"`,\n );\n\n return null;\n }\n\n // An empty part is almost certainly a catalog authoring error.\n // Treat the whole value as a plain string rather than silently producing an empty form.\n if (parts.some((p) => p.trim() === '')) return null;\n\n const result: Messages = {};\n\n for (let i = 0; i < parts.length; i++) {\n result[forms[i]] = parts[i];\n }\n\n return result;\n}\n\n// ─── Template compilation ─────────────────────────────────────────────────────\n// Message strings are parsed once at registration time into a pre-compiled form:\n// an array of static strings and variable names. Rendering a compiled template\n// skips the regex entirely.\n\ntype TemplatePart = string | { var: string };\nexport type CompiledTemplate = TemplatePart[];\n\nexport const INTERPOLATION_PATTERN = /\\{([\\p{ID_Continue}-]+)\\}/gu;\n\nexport function compileTemplate(template: string): CompiledTemplate {\n const parts: CompiledTemplate = [];\n let lastIndex = 0;\n\n for (const match of template.matchAll(INTERPOLATION_PATTERN)) {\n const { index } = match;\n const start = index ?? 0;\n\n if (start > lastIndex) {\n parts.push(template.slice(lastIndex, start));\n }\n\n parts.push({ var: match[1] });\n lastIndex = start + match[0].length;\n }\n\n if (lastIndex < template.length) {\n parts.push(template.slice(lastIndex));\n }\n\n return parts;\n}\n\nexport function renderTemplate(\n parts: CompiledTemplate,\n vars: TranslateVars | undefined,\n key: string,\n locale: Locale,\n onMissingVar: (varName: string, key: string, locale: Locale) => string,\n): string {\n let result = '';\n\n for (const part of parts) {\n if (typeof part === 'string') {\n result += part;\n } else {\n const value = vars?.[part.var];\n\n result += value == null ? onMissingVar(part.var, key, locale) : String(value);\n }\n }\n\n return result;\n}\n"],"mappings":";;AAoBA,IAAM,IAAuE;CAC3E,GAAG,CAAC,OAAO,OAAO;CAClB,GAAG;EAAC;EAAQ;EAAO;CAAO;CAC1B,GAAG;EAAC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