@burojs/i18n-core 0.2.0 → 0.4.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,22 @@
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
+ 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;
17
17
  }
18
-
18
+ //#endregion
19
+ //#region src/types/bundle.d.ts
19
20
  /**
20
21
  * Recursive translation bundle: keys map to a string, a nested bundle, or an
21
22
  * ARRAY of either.
@@ -30,17 +31,18 @@ interface I18nAdapter {
30
31
  * the package that owns the type.
31
32
  */
32
33
  type TranslationBundle = {
33
- [key: string]: string | TranslationBundle | readonly (string | TranslationBundle)[];
34
+ [key: string]: string | TranslationBundle | readonly (string | TranslationBundle)[];
34
35
  };
35
36
  /**
36
37
  * Per-resource bundle: optional async loader returning default-export bundle.
37
38
  */
38
39
  type ResourceBundle = (() => Promise<{
39
- default: TranslationBundle;
40
+ default: TranslationBundle;
40
41
  }>) | Record<string, () => Promise<{
41
- default: TranslationBundle;
42
+ default: TranslationBundle;
42
43
  }>>;
43
-
44
+ //#endregion
45
+ //#region src/types/i18n-keys.d.ts
44
46
  /**
45
47
  * Open interface for opt-in codegen-typed i18n keys.
46
48
  *
@@ -50,14 +52,13 @@ type ResourceBundle = (() => Promise<{
50
52
  * }
51
53
  * }
52
54
  */
53
- interface I18nKeys {
54
- }
55
+ interface I18nKeys {}
55
56
  /**
56
57
  * Open interface for keys requiring interpolation parameters.
57
58
  */
58
- interface I18nKeysWithParams {
59
- }
60
-
59
+ interface I18nKeysWithParams {}
60
+ //#endregion
61
+ //#region src/lookup.d.ts
61
62
  /**
62
63
  * Build candidate keys for fallback lookup.
63
64
  *
@@ -71,22 +72,23 @@ interface I18nKeysWithParams {
71
72
  * `common.`), return it as-is without further scoping.
72
73
  */
73
74
  declare function resolveKey(key: string, resource?: string): string[];
74
-
75
+ //#endregion
76
+ //#region src/i18next-adapter.d.ts
75
77
  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;
78
+ defaultLocale: string;
79
+ locales: string[];
80
+ resources?: Record<string, Record<string, TranslationBundle>>;
81
+ /**
82
+ * A single fallback locale (`'en'`) applies uniformly to every locale. A
83
+ * `fr → en` fallback for one locale but not another needs i18next's own
84
+ * per-locale chain shape instead — `{ fr: ['en'], default: ['en'] }` — so
85
+ * this accepts i18next's full `FallbackLng` type rather than just `string`.
86
+ * The chain is entirely CONFIG: nothing in this adapter special-cases any
87
+ * particular locale code, here or anywhere else.
88
+ */
89
+ fallbackLocale?: FallbackLng;
90
+ keySeparator?: string | false;
91
+ nsSeparator?: string | false;
90
92
  }
91
93
  /**
92
94
  * Default I18nAdapter implementation backed by i18next.
@@ -94,47 +96,49 @@ interface I18nextAdapterConfig {
94
96
  * (e.g. 'common', 'resources').
95
97
  */
96
98
  declare function createI18nextAdapter(config: I18nextAdapterConfig): Promise<I18nAdapter>;
97
-
99
+ //#endregion
100
+ //#region src/i18n.d.ts
98
101
  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;
102
+ t: (key: string, options?: {
103
+ resource?: string;
104
+ params?: Record<string, unknown>;
105
+ }) => string;
106
+ /**
107
+ * True when a translation for `key` EXISTS (through the same candidate chain
108
+ * as {@link I18nFacade.t}). Distinct from `t`, which returns the raw key on a
109
+ * miss — the content model probes presence with `has` to tell "no bundle
110
+ * entry" (render nothing) from "bundle entry === key text".
111
+ */
112
+ has: (key: string, options?: {
113
+ resource?: string;
114
+ }) => boolean;
115
+ changeLocale: (locale: string) => Promise<void>;
116
+ getCurrentLocale: () => string;
117
+ /**
118
+ * Subscribe to active-locale changes. Returns an unsubscribe fn. When the
119
+ * adapter can't notify, this is a no-op subscription (returns an unsubscribe
120
+ * that does nothing) — the live language switch then falls back to a static
121
+ * read. Consumed by `useLocale()` via `useSyncExternalStore`.
122
+ */
123
+ onLocaleChange: (cb: () => void) => () => void;
124
+ loadNamespace: (namespace: string, locale: string) => Promise<void>;
125
+ adapter: I18nAdapter;
123
126
  }
124
127
  interface CreateI18nConfig {
125
- adapter: I18nAdapter;
126
- warnOnMissing?: boolean;
128
+ adapter: I18nAdapter;
129
+ warnOnMissing?: boolean;
127
130
  }
128
131
  declare function createI18n(config: CreateI18nConfig): I18nFacade;
129
-
132
+ //#endregion
133
+ //#region src/resource-context.d.ts
130
134
  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;
135
+ t: (key: string, options?: {
136
+ resource?: string;
137
+ params?: Record<string, unknown>;
138
+ }) => string;
139
+ changeLocale: (locale: string) => Promise<void>;
140
+ getCurrentLocale: () => string;
141
+ resource: string;
138
142
  }
139
143
  /**
140
144
  * Wraps an I18nFacade with a default resource scope.
@@ -142,7 +146,8 @@ interface ResourceI18n {
142
146
  * Pass an explicit `resource` option to override the default scope.
143
147
  */
144
148
  declare function createResourceI18n(i18n: I18nFacade, resource: string): ResourceI18n;
145
-
149
+ //#endregion
150
+ //#region src/locale-registry.d.ts
146
151
  /**
147
152
  * One package/app/domain module's offering for one locale.
148
153
  *
@@ -155,52 +160,52 @@ declare function createResourceI18n(i18n: I18nFacade, resource: string): Resourc
155
160
  * twenty-first `case`.
156
161
  */
157
162
  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;
163
+ /** Locale code this contribution provides strings for (e.g. 'en', 'es', 'fr'). */
164
+ locale: string;
165
+ /**
166
+ * i18next namespace the bundle merges into. Defaults to `'common'` — the
167
+ * same default namespace `createI18nextAdapter` uses.
168
+ */
169
+ namespace?: string;
170
+ /**
171
+ * Declared merge priority. On a key collision within the same
172
+ * (locale, namespace), the contribution with the HIGHER priority wins —
173
+ * not whichever happened to register last. Ties fall back to registration
174
+ * order (last registered wins), which is why two genuinely competing
175
+ * sources must declare distinct priorities rather than relying on the tie
176
+ * break.
177
+ */
178
+ priority: number;
179
+ /**
180
+ * Where this contribution came from — a package name, `'app'`, an enum
181
+ * module name. Diagnostic only (surfaced by {@link LocaleRegistry.contributions}
182
+ * for debugging a collision); never branched on.
183
+ */
184
+ source: string;
185
+ bundle: TranslationBundle;
181
186
  }
182
187
  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>>;
188
+ /** Add one contribution. Order of registration does not matter for the merge result — only `priority` does (see {@link LocaleContribution.priority}). */
189
+ register(contribution: LocaleContribution): void;
190
+ /**
191
+ * Every locale code that has at least one contribution, sorted. Derived
192
+ * purely from what was registered — nothing enumerates or counts locales
193
+ * anywhere else in this module.
194
+ */
195
+ locales(): string[];
196
+ /** Every namespace that has at least one contribution, sorted. */
197
+ namespaces(): string[];
198
+ /** All registered contributions, in registration order. For diagnostics/tests. */
199
+ contributions(): readonly LocaleContribution[];
200
+ /**
201
+ * Assemble `{ [locale]: { [namespace]: TranslationBundle } }`, ready to
202
+ * pass straight as `createI18nextAdapter`'s `resources` config. Within one
203
+ * (locale, namespace) group, contributions are deep-merged in ascending
204
+ * priority order, so a higher-priority contribution's leaves win over a
205
+ * lower-priority one's on any overlapping key while leaving the rest of
206
+ * the lower-priority bundle intact.
207
+ */
208
+ buildResources(): Record<string, Record<string, TranslationBundle>>;
204
209
  }
205
210
  declare function createLocaleRegistry(): LocaleRegistry;
206
211
  /**
@@ -214,24 +219,24 @@ declare function createLocaleRegistry(): LocaleRegistry;
214
219
  * whatever their bundler offers) and hands the plain object here.
215
220
  */
216
221
  type LocaleGlobModule = TranslationBundle | {
217
- default: TranslationBundle;
222
+ default: TranslationBundle;
218
223
  };
219
224
  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;
225
+ /** {@link LocaleContribution.source} for every contribution produced. */
226
+ source: string;
227
+ /** {@link LocaleContribution.priority} for every contribution produced. */
228
+ priority: number;
229
+ /** {@link LocaleContribution.namespace}. Defaults to `'common'`. */
230
+ namespace?: string;
231
+ /**
232
+ * Extracts the locale code from a glob path. Default matches a bare
233
+ * `<locale>.ts`/`<locale>.tsx`/`<locale>.js` filename (`'./locales/en.ts'`
234
+ * → `'en'`, `'./locales/fr-CA.ts'` → `'fr-CA'`) — no restriction to a
235
+ * known set of codes; whatever the filename says IS the locale code. Paths
236
+ * that don't match are skipped rather than guessed at (same discipline as
237
+ * `docsFromGlob`).
238
+ */
239
+ localeOf?: (path: string) => string | undefined;
235
240
  }
236
241
  /**
237
242
  * Normalize a bundler glob of per-locale files into {@link LocaleContribution}s.
@@ -241,9 +246,10 @@ interface LocaleContributionsFromGlobOptions {
241
246
  * mechanism for adding a language.
242
247
  */
243
248
  declare function localeContributionsFromGlob(glob: Record<string, LocaleGlobModule>, options: LocaleContributionsFromGlobOptions): LocaleContribution[];
244
-
249
+ //#endregion
250
+ //#region src/create-i18n-from-registry.d.ts
245
251
  type CreateI18nFromRegistryConfig = Pick<I18nextAdapterConfig, 'defaultLocale' | 'fallbackLocale' | 'keySeparator' | 'nsSeparator'> & {
246
- warnOnMissing?: boolean;
252
+ warnOnMissing?: boolean;
247
253
  };
248
254
  /**
249
255
  * Wires a {@link LocaleRegistry} straight into `createI18nextAdapter` +
@@ -254,8 +260,10 @@ type CreateI18nFromRegistryConfig = Pick<I18nextAdapterConfig, 'defaultLocale' |
254
260
  * just calls this.
255
261
  */
256
262
  declare function createI18nFromRegistry(registry: LocaleRegistry, config: CreateI18nFromRegistryConfig): Promise<I18nFacade>;
257
-
263
+ //#endregion
264
+ //#region src/index.d.ts
258
265
  declare const PACKAGE_NAME: "@burojs/i18n-core";
259
266
  declare const PACKAGE_VERSION: "0.0.0";
260
-
267
+ //#endregion
261
268
  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 };
269
+ //# sourceMappingURL=index.d.ts.map