@burojs/i18n-core 0.2.0 → 0.5.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/index.d.ts CHANGED
@@ -1,21 +1,30 @@
1
- import { FallbackLng } from 'i18next';
2
-
1
+ import { FallbackLng } from "i18next";
2
+ //#region src/types/adapter.d.ts
3
3
  interface I18nAdapter {
4
- t: (key: string, params?: Record<string, unknown>) => string;
5
- changeLocale: (locale: string) => Promise<void>;
6
- loadNamespace: (namespace: string, locale: string) => Promise<void>;
7
- getCurrentLocale: () => string;
8
- hasKey: (key: string) => boolean;
9
- /**
10
- * Subscribe to active-locale changes (fired by {@link I18nAdapter.changeLocale}).
11
- * Returns an unsubscribe fn. Optional — adapters that cannot notify omit it,
12
- * and the React layer then degrades to a non-reactive locale read. This is
13
- * what powers the live language switch (no page reload): `useLocale()` wires
14
- * it into `useSyncExternalStore`.
15
- */
16
- onLocaleChange?: (cb: () => void) => () => void;
4
+ t: (key: string, params?: Record<string, unknown>) => string;
5
+ changeLocale: (locale: string) => Promise<void>;
6
+ loadNamespace: (namespace: string, locale: string) => Promise<void>;
7
+ getCurrentLocale: () => string;
8
+ /**
9
+ * True when a USABLE STRING exists at `key` (an object node is not one).
10
+ * Pass `count` to ask about a count family (`<key>_one | _few | _many |
11
+ * _other`): i18next resolves the suffix only when it is asked with a count,
12
+ * so without one a present family reports missing.
13
+ */
14
+ hasKey: (key: string, options?: {
15
+ count?: number;
16
+ }) => boolean;
17
+ /**
18
+ * Subscribe to active-locale changes (fired by {@link I18nAdapter.changeLocale}).
19
+ * Returns an unsubscribe fn. Optional — adapters that cannot notify omit it,
20
+ * and the React layer then degrades to a non-reactive locale read. This is
21
+ * what powers the live language switch (no page reload): `useLocale()` wires
22
+ * it into `useSyncExternalStore`.
23
+ */
24
+ onLocaleChange?: (cb: () => void) => () => void;
17
25
  }
18
-
26
+ //#endregion
27
+ //#region src/types/bundle.d.ts
19
28
  /**
20
29
  * Recursive translation bundle: keys map to a string, a nested bundle, or an
21
30
  * ARRAY of either.
@@ -30,17 +39,18 @@ interface I18nAdapter {
30
39
  * the package that owns the type.
31
40
  */
32
41
  type TranslationBundle = {
33
- [key: string]: string | TranslationBundle | readonly (string | TranslationBundle)[];
42
+ [key: string]: string | TranslationBundle | readonly (string | TranslationBundle)[];
34
43
  };
35
44
  /**
36
45
  * Per-resource bundle: optional async loader returning default-export bundle.
37
46
  */
38
47
  type ResourceBundle = (() => Promise<{
39
- default: TranslationBundle;
48
+ default: TranslationBundle;
40
49
  }>) | Record<string, () => Promise<{
41
- default: TranslationBundle;
50
+ default: TranslationBundle;
42
51
  }>>;
43
-
52
+ //#endregion
53
+ //#region src/types/i18n-keys.d.ts
44
54
  /**
45
55
  * Open interface for opt-in codegen-typed i18n keys.
46
56
  *
@@ -50,14 +60,13 @@ type ResourceBundle = (() => Promise<{
50
60
  * }
51
61
  * }
52
62
  */
53
- interface I18nKeys {
54
- }
63
+ interface I18nKeys {}
55
64
  /**
56
65
  * Open interface for keys requiring interpolation parameters.
57
66
  */
58
- interface I18nKeysWithParams {
59
- }
60
-
67
+ interface I18nKeysWithParams {}
68
+ //#endregion
69
+ //#region src/lookup.d.ts
61
70
  /**
62
71
  * Build candidate keys for fallback lookup.
63
72
  *
@@ -67,26 +76,27 @@ interface I18nKeysWithParams {
67
76
  * 3. common.<key> — global common
68
77
  * 4. <key> — raw fallback (debug only)
69
78
  *
70
- * If `key` is already namespaced (starts with `resources.`, `enums.` or
71
- * `common.`), return it as-is without further scoping.
79
+ * If `key` is already namespaced (starts with `resources.`, `enums.`,
80
+ * `common.` or `blocks.`), return it as-is without further scoping.
72
81
  */
73
82
  declare function resolveKey(key: string, resource?: string): string[];
74
-
83
+ //#endregion
84
+ //#region src/i18next-adapter.d.ts
75
85
  interface I18nextAdapterConfig {
76
- defaultLocale: string;
77
- locales: string[];
78
- resources?: Record<string, Record<string, TranslationBundle>>;
79
- /**
80
- * A single fallback locale (`'en'`) applies uniformly to every locale. A
81
- * `fr → en` fallback for one locale but not another needs i18next's own
82
- * per-locale chain shape instead — `{ fr: ['en'], default: ['en'] }` — so
83
- * this accepts i18next's full `FallbackLng` type rather than just `string`.
84
- * The chain is entirely CONFIG: nothing in this adapter special-cases any
85
- * particular locale code, here or anywhere else.
86
- */
87
- fallbackLocale?: FallbackLng;
88
- keySeparator?: string | false;
89
- nsSeparator?: string | false;
86
+ defaultLocale: string;
87
+ locales: string[];
88
+ resources?: Record<string, Record<string, TranslationBundle>>;
89
+ /**
90
+ * A single fallback locale (`'en'`) applies uniformly to every locale. A
91
+ * `fr → en` fallback for one locale but not another needs i18next's own
92
+ * per-locale chain shape instead — `{ fr: ['en'], default: ['en'] }` — so
93
+ * this accepts i18next's full `FallbackLng` type rather than just `string`.
94
+ * The chain is entirely CONFIG: nothing in this adapter special-cases any
95
+ * particular locale code, here or anywhere else.
96
+ */
97
+ fallbackLocale?: FallbackLng;
98
+ keySeparator?: string | false;
99
+ nsSeparator?: string | false;
90
100
  }
91
101
  /**
92
102
  * Default I18nAdapter implementation backed by i18next.
@@ -94,47 +104,67 @@ interface I18nextAdapterConfig {
94
104
  * (e.g. 'common', 'resources').
95
105
  */
96
106
  declare function createI18nextAdapter(config: I18nextAdapterConfig): Promise<I18nAdapter>;
97
-
107
+ //#endregion
108
+ //#region src/i18n.d.ts
98
109
  interface I18nFacade {
99
- t: (key: string, options?: {
100
- resource?: string;
101
- params?: Record<string, unknown>;
102
- }) => string;
103
- /**
104
- * True when a translation for `key` EXISTS (through the same candidate chain
105
- * as {@link I18nFacade.t}). Distinct from `t`, which returns the raw key on a
106
- * miss — the content model probes presence with `has` to tell "no bundle
107
- * entry" (render nothing) from "bundle entry === key text".
108
- */
109
- has: (key: string, options?: {
110
- resource?: string;
111
- }) => boolean;
112
- changeLocale: (locale: string) => Promise<void>;
113
- getCurrentLocale: () => string;
114
- /**
115
- * Subscribe to active-locale changes. Returns an unsubscribe fn. When the
116
- * adapter can't notify, this is a no-op subscription (returns an unsubscribe
117
- * that does nothing) — the live language switch then falls back to a static
118
- * read. Consumed by `useLocale()` via `useSyncExternalStore`.
119
- */
120
- onLocaleChange: (cb: () => void) => () => void;
121
- loadNamespace: (namespace: string, locale: string) => Promise<void>;
122
- adapter: I18nAdapter;
110
+ /**
111
+ * Translate `key` through the candidate chain. `count` selects a count
112
+ * family's form (`<key>_one | _few | _many | _other`, by the locale's CLDR
113
+ * rules) and interpolates `{{count}}`; a family asked without a count is
114
+ * missing. A `count` inside `params` only interpolates.
115
+ */
116
+ t: (key: string, options?: {
117
+ resource?: string;
118
+ params?: Record<string, unknown>;
119
+ count?: number;
120
+ }) => string;
121
+ /**
122
+ * True when a translation for `key` EXISTS (through the same candidate chain
123
+ * as {@link I18nFacade.t}). Distinct from `t`, which returns the raw key on a
124
+ * miss — the content model probes presence with `has` to tell "no bundle
125
+ * entry" (render nothing) from "bundle entry === key text". Pass `count` to probe a count family.
126
+ */
127
+ has: (key: string, options?: {
128
+ resource?: string;
129
+ count?: number;
130
+ }) => boolean;
131
+ changeLocale: (locale: string) => Promise<void>;
132
+ getCurrentLocale: () => string;
133
+ /**
134
+ * Subscribe to active-locale changes. Returns an unsubscribe fn. When the
135
+ * adapter can't notify, this is a no-op subscription (returns an unsubscribe
136
+ * that does nothing) — the live language switch then falls back to a static
137
+ * read. Consumed by `useLocale()` via `useSyncExternalStore`.
138
+ */
139
+ onLocaleChange: (cb: () => void) => () => void;
140
+ loadNamespace: (namespace: string, locale: string) => Promise<void>;
141
+ /**
142
+ * The locale codes this app can switch to. Optional: a facade built without
143
+ * a list (a hand-rolled one, a test double) simply does not say, and a
144
+ * language control then offers only the active locale. `createI18nFromRegistry`
145
+ * fills it from the registry.
146
+ */
147
+ locales?: () => readonly string[];
148
+ adapter: I18nAdapter;
123
149
  }
124
150
  interface CreateI18nConfig {
125
- adapter: I18nAdapter;
126
- warnOnMissing?: boolean;
151
+ adapter: I18nAdapter;
152
+ warnOnMissing?: boolean;
153
+ /** The switchable locale codes, exposed as {@link I18nFacade.locales}. */
154
+ locales?: () => readonly string[];
127
155
  }
128
156
  declare function createI18n(config: CreateI18nConfig): I18nFacade;
129
-
157
+ //#endregion
158
+ //#region src/resource-context.d.ts
130
159
  interface ResourceI18n {
131
- t: (key: string, options?: {
132
- resource?: string;
133
- params?: Record<string, unknown>;
134
- }) => string;
135
- changeLocale: (locale: string) => Promise<void>;
136
- getCurrentLocale: () => string;
137
- resource: string;
160
+ t: (key: string, options?: {
161
+ resource?: string;
162
+ params?: Record<string, unknown>;
163
+ count?: number;
164
+ }) => string;
165
+ changeLocale: (locale: string) => Promise<void>;
166
+ getCurrentLocale: () => string;
167
+ resource: string;
138
168
  }
139
169
  /**
140
170
  * Wraps an I18nFacade with a default resource scope.
@@ -142,7 +172,8 @@ interface ResourceI18n {
142
172
  * Pass an explicit `resource` option to override the default scope.
143
173
  */
144
174
  declare function createResourceI18n(i18n: I18nFacade, resource: string): ResourceI18n;
145
-
175
+ //#endregion
176
+ //#region src/locale-registry.d.ts
146
177
  /**
147
178
  * One package/app/domain module's offering for one locale.
148
179
  *
@@ -155,54 +186,79 @@ declare function createResourceI18n(i18n: I18nFacade, resource: string): Resourc
155
186
  * twenty-first `case`.
156
187
  */
157
188
  interface LocaleContribution {
158
- /** Locale code this contribution provides strings for (e.g. 'en', 'es', 'fr'). */
159
- locale: string;
160
- /**
161
- * i18next namespace the bundle merges into. Defaults to `'common'` — the
162
- * same default namespace `createI18nextAdapter` uses.
163
- */
164
- namespace?: string;
165
- /**
166
- * Declared merge priority. On a key collision within the same
167
- * (locale, namespace), the contribution with the HIGHER priority wins —
168
- * not whichever happened to register last. Ties fall back to registration
169
- * order (last registered wins), which is why two genuinely competing
170
- * sources must declare distinct priorities rather than relying on the tie
171
- * break.
172
- */
173
- priority: number;
174
- /**
175
- * Where this contribution came from — a package name, `'app'`, an enum
176
- * module name. Diagnostic only (surfaced by {@link LocaleRegistry.contributions}
177
- * for debugging a collision); never branched on.
178
- */
179
- source: string;
180
- bundle: TranslationBundle;
189
+ /** Locale code this contribution provides strings for (e.g. 'en', 'es', 'fr'). */
190
+ locale: string;
191
+ /**
192
+ * i18next namespace the bundle merges into. Defaults to `'common'` — the
193
+ * same default namespace `createI18nextAdapter` uses.
194
+ */
195
+ namespace?: string;
196
+ /**
197
+ * Declared merge priority. On a key collision within the same
198
+ * (locale, namespace), the contribution with the HIGHER priority wins —
199
+ * not whichever happened to register last. Ties fall back to registration
200
+ * order (last registered wins), which is why two genuinely competing
201
+ * sources must declare distinct priorities rather than relying on the tie
202
+ * break.
203
+ */
204
+ priority: number;
205
+ /**
206
+ * Where this contribution came from — a package name, `'app'`, an enum
207
+ * module name. Diagnostic only (surfaced by {@link LocaleRegistry.contributions}
208
+ * for debugging a collision); never branched on.
209
+ */
210
+ source: string;
211
+ bundle: TranslationBundle;
181
212
  }
182
213
  interface LocaleRegistry {
183
- /** Add one contribution. Order of registration does not matter for the merge result — only `priority` does (see {@link LocaleContribution.priority}). */
184
- register(contribution: LocaleContribution): void;
185
- /**
186
- * Every locale code that has at least one contribution, sorted. Derived
187
- * purely from what was registered — nothing enumerates or counts locales
188
- * anywhere else in this module.
189
- */
190
- locales(): string[];
191
- /** Every namespace that has at least one contribution, sorted. */
192
- namespaces(): string[];
193
- /** All registered contributions, in registration order. For diagnostics/tests. */
194
- contributions(): readonly LocaleContribution[];
195
- /**
196
- * Assemble `{ [locale]: { [namespace]: TranslationBundle } }`, ready to
197
- * pass straight as `createI18nextAdapter`'s `resources` config. Within one
198
- * (locale, namespace) group, contributions are deep-merged in ascending
199
- * priority order, so a higher-priority contribution's leaves win over a
200
- * lower-priority one's on any overlapping key while leaving the rest of
201
- * the lower-priority bundle intact.
202
- */
203
- buildResources(): Record<string, Record<string, TranslationBundle>>;
214
+ /**
215
+ * The primary locale, as declared to {@link createLocaleRegistry}: the one
216
+ * whose text is the default in code, and the locale every other one falls
217
+ * back to. The single source of truth for it — `createI18nFromRegistry`
218
+ * defaults both the starting locale and the fallback to it, and
219
+ * `@burojs/testing`'s `checkApp` reads it to know which locale is exempt
220
+ * from `i18n.missing`. Never derived from contributions or their order.
221
+ */
222
+ primary(): string;
223
+ /** Add one contribution. Order of registration does not matter for the merge result — only `priority` does (see {@link LocaleContribution.priority}). */
224
+ register(contribution: LocaleContribution): void;
225
+ /**
226
+ * Every locale code that has at least one contribution, sorted. Derived
227
+ * purely from what was registered — nothing enumerates or counts locales
228
+ * anywhere else in this module.
229
+ */
230
+ locales(): string[];
231
+ /** Every namespace that has at least one contribution, sorted. */
232
+ namespaces(): string[];
233
+ /** All registered contributions, in registration order. For diagnostics/tests. */
234
+ contributions(): readonly LocaleContribution[];
235
+ /**
236
+ * Assemble `{ [locale]: { [namespace]: TranslationBundle } }`, ready to
237
+ * pass straight as `createI18nextAdapter`'s `resources` config. Within one
238
+ * (locale, namespace) group, contributions are deep-merged in ascending
239
+ * priority order, so a higher-priority contribution's leaves win over a
240
+ * lower-priority one's on any overlapping key while leaving the rest of
241
+ * the lower-priority bundle intact.
242
+ *
243
+ * Throws when no contribution is registered for {@link primary}: a
244
+ * fallback chain that ends at a locale with no bundle, and a primary that
245
+ * `locales()` (and so every language switcher built from it) does not
246
+ * list, is a misconfiguration (usually a typo, `'en-US'` for `'en'`, or a
247
+ * glob that missed its file), not a state to render. An app that really
248
+ * has no strings for its primary locale registers an empty bundle for it.
249
+ */
250
+ buildResources(): Record<string, Record<string, TranslationBundle>>;
204
251
  }
205
- declare function createLocaleRegistry(): LocaleRegistry;
252
+ interface LocaleRegistryOptions {
253
+ /**
254
+ * The primary locale: the one whose text is the default in code. Required,
255
+ * and declared here rather than inferred, because nothing in the
256
+ * contributions says which locale it is — registration order and the
257
+ * sorted `locales()` both carry no meaning.
258
+ */
259
+ primary: string;
260
+ }
261
+ declare function createLocaleRegistry(options: LocaleRegistryOptions): LocaleRegistry;
206
262
  /**
207
263
  * A bundler-produced glob module, as `import.meta.glob` (Vite) or an
208
264
  * equivalent walk over `require.context` (webpack) would yield it: a flat
@@ -214,24 +270,24 @@ declare function createLocaleRegistry(): LocaleRegistry;
214
270
  * whatever their bundler offers) and hands the plain object here.
215
271
  */
216
272
  type LocaleGlobModule = TranslationBundle | {
217
- default: TranslationBundle;
273
+ default: TranslationBundle;
218
274
  };
219
275
  interface LocaleContributionsFromGlobOptions {
220
- /** {@link LocaleContribution.source} for every contribution produced. */
221
- source: string;
222
- /** {@link LocaleContribution.priority} for every contribution produced. */
223
- priority: number;
224
- /** {@link LocaleContribution.namespace}. Defaults to `'common'`. */
225
- namespace?: string;
226
- /**
227
- * Extracts the locale code from a glob path. Default matches a bare
228
- * `<locale>.ts`/`<locale>.tsx`/`<locale>.js` filename (`'./locales/en.ts'`
229
- * → `'en'`, `'./locales/fr-CA.ts'` → `'fr-CA'`) — no restriction to a
230
- * known set of codes; whatever the filename says IS the locale code. Paths
231
- * that don't match are skipped rather than guessed at (same discipline as
232
- * `docsFromGlob`).
233
- */
234
- localeOf?: (path: string) => string | undefined;
276
+ /** {@link LocaleContribution.source} for every contribution produced. */
277
+ source: string;
278
+ /** {@link LocaleContribution.priority} for every contribution produced. */
279
+ priority: number;
280
+ /** {@link LocaleContribution.namespace}. Defaults to `'common'`. */
281
+ namespace?: string;
282
+ /**
283
+ * Extracts the locale code from a glob path. Default matches a bare
284
+ * `<locale>.ts`/`<locale>.tsx`/`<locale>.js` filename (`'./locales/en.ts'`
285
+ * → `'en'`, `'./locales/fr-CA.ts'` → `'fr-CA'`) — no restriction to a
286
+ * known set of codes; whatever the filename says IS the locale code. Paths
287
+ * that don't match are skipped rather than guessed at (same discipline as
288
+ * `docsFromGlob`).
289
+ */
290
+ localeOf?: (path: string) => string | undefined;
235
291
  }
236
292
  /**
237
293
  * Normalize a bundler glob of per-locale files into {@link LocaleContribution}s.
@@ -241,21 +297,30 @@ interface LocaleContributionsFromGlobOptions {
241
297
  * mechanism for adding a language.
242
298
  */
243
299
  declare function localeContributionsFromGlob(glob: Record<string, LocaleGlobModule>, options: LocaleContributionsFromGlobOptions): LocaleContribution[];
244
-
245
- type CreateI18nFromRegistryConfig = Pick<I18nextAdapterConfig, 'defaultLocale' | 'fallbackLocale' | 'keySeparator' | 'nsSeparator'> & {
246
- warnOnMissing?: boolean;
300
+ //#endregion
301
+ //#region src/create-i18n-from-registry.d.ts
302
+ type CreateI18nFromRegistryConfig = Partial<Pick<I18nextAdapterConfig, 'defaultLocale' | 'fallbackLocale' | 'keySeparator' | 'nsSeparator'>> & {
303
+ warnOnMissing?: boolean;
247
304
  };
248
305
  /**
249
306
  * Wires a {@link LocaleRegistry} straight into `createI18nextAdapter` +
250
307
  * `createI18n`: `registry.locales()` becomes the adapter's `locales`,
251
- * `registry.buildResources()` becomes its `resources`. This is the ONLY
252
- * place those two need to be threaded together — an app that builds its own
253
- * registry never re-lists its locale codes to hand them to the adapter, it
254
- * just calls this.
308
+ * `registry.buildResources()` becomes its `resources`, and
309
+ * `registry.primary()` is both the starting locale and the fallback unless
310
+ * `defaultLocale` (the starting locale, say a stored choice) or
311
+ * `fallbackLocale` (say a per-locale chain) overrides it. This is the ONLY
312
+ * place those need to be threaded together — an app that builds its own
313
+ * registry never re-lists its locale codes, or restates its primary locale,
314
+ * to hand them to the adapter; it just calls this.
315
+ *
316
+ * Throws when the registry's primary locale has no contribution (see
317
+ * {@link LocaleRegistry.buildResources}).
255
318
  */
256
- declare function createI18nFromRegistry(registry: LocaleRegistry, config: CreateI18nFromRegistryConfig): Promise<I18nFacade>;
257
-
319
+ declare function createI18nFromRegistry(registry: LocaleRegistry, config?: CreateI18nFromRegistryConfig): Promise<I18nFacade>;
320
+ //#endregion
321
+ //#region src/index.d.ts
258
322
  declare const PACKAGE_NAME: "@burojs/i18n-core";
259
323
  declare const PACKAGE_VERSION: "0.0.0";
260
-
261
- export { type CreateI18nConfig, type CreateI18nFromRegistryConfig, type I18nAdapter, type I18nFacade, type I18nKeys, type I18nKeysWithParams, type I18nextAdapterConfig, type LocaleContribution, type LocaleContributionsFromGlobOptions, type LocaleGlobModule, type LocaleRegistry, PACKAGE_NAME, PACKAGE_VERSION, type ResourceBundle, type ResourceI18n, type TranslationBundle, createI18n, createI18nFromRegistry, createI18nextAdapter, createLocaleRegistry, createResourceI18n, localeContributionsFromGlob, resolveKey };
324
+ //#endregion
325
+ export { type CreateI18nConfig, type CreateI18nFromRegistryConfig, type I18nAdapter, type I18nFacade, type I18nKeys, type I18nKeysWithParams, type I18nextAdapterConfig, type LocaleContribution, type LocaleContributionsFromGlobOptions, type LocaleGlobModule, type LocaleRegistry, type LocaleRegistryOptions, PACKAGE_NAME, PACKAGE_VERSION, type ResourceBundle, type ResourceI18n, type TranslationBundle, createI18n, createI18nFromRegistry, createI18nextAdapter, createLocaleRegistry, createResourceI18n, localeContributionsFromGlob, resolveKey };
326
+ //# sourceMappingURL=index.d.ts.map