@molecule/app-i18n-i18next 1.0.0 → 1.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.
- package/README.md +466 -0
- package/package.json +5 -4
package/README.md
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:51:01.207Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-i18n-i18next
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Framework-agnostic i18next provider for molecule.dev.
|
|
16
|
+
|
|
17
|
+
Implements the I18nProvider interface using i18next directly,
|
|
18
|
+
without any framework-specific bindings. Suitable for Vue, Angular,
|
|
19
|
+
Svelte, Solid, and any other framework.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { setProvider } from '@molecule/app-i18n'
|
|
25
|
+
import { createI18nextProvider } from '@molecule/app-i18n-i18next'
|
|
26
|
+
|
|
27
|
+
const provider = createI18nextProvider({
|
|
28
|
+
defaultLocale: 'en',
|
|
29
|
+
locales: [
|
|
30
|
+
{ code: 'en', name: 'English', translations: { ... } },
|
|
31
|
+
{ code: 'fr', name: 'French', translations: { ... } },
|
|
32
|
+
],
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
setProvider(provider)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Type
|
|
39
|
+
|
|
40
|
+
`provider`
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install @molecule/app-i18n-i18next @molecule/app-i18n @molecule/app-logger i18next i18next-browser-languagedetector
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### Interfaces
|
|
51
|
+
|
|
52
|
+
#### `DateFormatOptions`
|
|
53
|
+
|
|
54
|
+
Date format options.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
interface DateFormatOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Date style.
|
|
60
|
+
*/
|
|
61
|
+
dateStyle?: 'full' | 'long' | 'medium' | 'short'
|
|
62
|
+
/**
|
|
63
|
+
* Time style.
|
|
64
|
+
*/
|
|
65
|
+
timeStyle?: 'full' | 'long' | 'medium' | 'short'
|
|
66
|
+
/**
|
|
67
|
+
* Custom format string (implementation-specific).
|
|
68
|
+
*/
|
|
69
|
+
format?: string
|
|
70
|
+
/**
|
|
71
|
+
* Relative time.
|
|
72
|
+
*/
|
|
73
|
+
relative?: boolean
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### `I18nextProviderConfig`
|
|
78
|
+
|
|
79
|
+
Configuration options for the i18next provider.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
interface I18nextProviderConfig {
|
|
83
|
+
/**
|
|
84
|
+
* Default locale code.
|
|
85
|
+
*/
|
|
86
|
+
defaultLocale?: string
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Fallback locale code.
|
|
90
|
+
*/
|
|
91
|
+
fallbackLocale?: string
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Available locales with translations.
|
|
95
|
+
*/
|
|
96
|
+
locales?: LocaleConfig[]
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Enable language detection.
|
|
100
|
+
*/
|
|
101
|
+
detection?: boolean
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Language detection options.
|
|
105
|
+
*/
|
|
106
|
+
detectionOptions?: {
|
|
107
|
+
order?: (
|
|
108
|
+
'querystring' | 'cookie' | 'localStorage' | 'sessionStorage' | 'navigator' | 'htmlTag'
|
|
109
|
+
)[]
|
|
110
|
+
lookupQuerystring?: string
|
|
111
|
+
lookupCookie?: string
|
|
112
|
+
lookupLocalStorage?: string
|
|
113
|
+
lookupSessionStorage?: string
|
|
114
|
+
caches?: ('localStorage' | 'cookie')[]
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Debug mode.
|
|
119
|
+
*/
|
|
120
|
+
debug?: boolean
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Custom i18next initialization options.
|
|
124
|
+
*/
|
|
125
|
+
i18nextOptions?: Partial<InitOptions>
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* i18next plugins to apply before initialization.
|
|
129
|
+
*
|
|
130
|
+
* Each plugin is passed to `i18n.use()` before `i18n.init()`.
|
|
131
|
+
* Useful for framework integrations (e.g. react-i18next's `initReactI18next`).
|
|
132
|
+
*/
|
|
133
|
+
plugins?: unknown[]
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### `I18nProvider`
|
|
138
|
+
|
|
139
|
+
i18n provider interface.
|
|
140
|
+
|
|
141
|
+
All i18n providers must implement this interface.
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
interface I18nProvider {
|
|
145
|
+
/**
|
|
146
|
+
* Gets the current locale.
|
|
147
|
+
*/
|
|
148
|
+
getLocale(): string
|
|
149
|
+
/**
|
|
150
|
+
* Sets the current locale.
|
|
151
|
+
*
|
|
152
|
+
* **Fleet contract:** every conformant provider (the core simple provider,
|
|
153
|
+
* `@molecule/api-i18n-simple`, `@molecule/app-i18n-i18next`, and
|
|
154
|
+
* `@molecule/app-i18n-react-i18next`) MUST throw `Error('Locale "<code>"
|
|
155
|
+
* not found')` when `locale` is not registered — via the constructor's
|
|
156
|
+
* `initialLocales`/`locales` config, `addLocale()`, or `addTranslations()`
|
|
157
|
+
* (all three register a locale). It must NOT silently degrade to
|
|
158
|
+
* fallback-locale text while `getLocale()` reports the unregistered code —
|
|
159
|
+
* that divergence makes a misconfigured locale switch indistinguishable
|
|
160
|
+
* from a working one until a user notices the wrong language on screen.
|
|
161
|
+
*/
|
|
162
|
+
setLocale(locale: string): Promise<void>
|
|
163
|
+
/**
|
|
164
|
+
* Gets all available locales.
|
|
165
|
+
*/
|
|
166
|
+
getLocales(): LocaleConfig[]
|
|
167
|
+
/**
|
|
168
|
+
* Adds a locale.
|
|
169
|
+
*/
|
|
170
|
+
addLocale(config: LocaleConfig): void
|
|
171
|
+
/**
|
|
172
|
+
* Removes a locale by code, notifying subscribers so language pickers
|
|
173
|
+
* built on `onLocaleChange` re-render their list. If the removed locale
|
|
174
|
+
* is currently active, the caller is responsible for switching to a
|
|
175
|
+
* fallback (e.g. `'en'`) BEFORE calling this — the provider will not
|
|
176
|
+
* auto-fall-back on its own.
|
|
177
|
+
*
|
|
178
|
+
* Returns `true` if the locale was registered and removed, `false`
|
|
179
|
+
* otherwise.
|
|
180
|
+
*/
|
|
181
|
+
removeLocale(code: string): boolean
|
|
182
|
+
/**
|
|
183
|
+
* Adds translations to a locale. Auto-creates the locale if it doesn't exist.
|
|
184
|
+
*
|
|
185
|
+
* **Fleet contract:** merges are DEEP, not a shallow spread — registering
|
|
186
|
+
* two calls (e.g. two modules) that share a top-level namespace key merges
|
|
187
|
+
* their subtrees instead of the second call clobbering the first's nested
|
|
188
|
+
* translations wholesale. `@molecule/api-i18n-simple` implements the same
|
|
189
|
+
* contract on the API side.
|
|
190
|
+
*/
|
|
191
|
+
addTranslations(locale: string, translations: Translations, namespace?: string): void
|
|
192
|
+
/**
|
|
193
|
+
* Translates a key with optional interpolation values and pluralization.
|
|
194
|
+
*
|
|
195
|
+
* **Fleet plural contract (matches i18next's own key resolution order):**
|
|
196
|
+
* when `options.count` is provided, the plural-suffixed key
|
|
197
|
+
* (`` `${key}_${pluralForm}` ``, e.g. `item_one`/`item_few`/…, falling back
|
|
198
|
+
* to `` `${key}_other` ``) is looked up FIRST and wins over the base `key`
|
|
199
|
+
* if BOTH are registered. Only when no plural-suffixed key exists at all
|
|
200
|
+
* does resolution fall back to the base key. A catalog that ships both
|
|
201
|
+
* `item` and `item_one`/`item_other` therefore pluralizes identically
|
|
202
|
+
* whichever provider is bonded.
|
|
203
|
+
*
|
|
204
|
+
* @returns The translated string, or the default value / key if not found.
|
|
205
|
+
*/
|
|
206
|
+
t(
|
|
207
|
+
key: string,
|
|
208
|
+
values?: InterpolationValues,
|
|
209
|
+
options?: {
|
|
210
|
+
defaultValue?: string
|
|
211
|
+
count?: number
|
|
212
|
+
},
|
|
213
|
+
): string
|
|
214
|
+
/**
|
|
215
|
+
* Checks if a translation key exists.
|
|
216
|
+
*
|
|
217
|
+
* **Fleet contract:** follows the SAME locale-resolution chain as `t()` —
|
|
218
|
+
* the active locale, then the English fallback — so `exists(key) === true`
|
|
219
|
+
* whenever `t(key)` would render real translated text (not the raw key or
|
|
220
|
+
* an inline `defaultValue`). Do not narrow this to "only the active
|
|
221
|
+
* locale's own catalog"; that made `exists()` return `false` for keys `t()`
|
|
222
|
+
* happily rendered via the English fallback, and the answer differed by
|
|
223
|
+
* provider.
|
|
224
|
+
*
|
|
225
|
+
* @returns `true` if the key has a translation.
|
|
226
|
+
*/
|
|
227
|
+
exists(key: string): boolean
|
|
228
|
+
/**
|
|
229
|
+
* Formats a number according to the current locale.
|
|
230
|
+
*
|
|
231
|
+
* @returns The locale-formatted number string.
|
|
232
|
+
*/
|
|
233
|
+
formatNumber(value: number, options?: NumberFormatOptions): string
|
|
234
|
+
/**
|
|
235
|
+
* Formats a date according to the current locale.
|
|
236
|
+
*
|
|
237
|
+
* @returns The locale-formatted date string.
|
|
238
|
+
*/
|
|
239
|
+
formatDate(value: Date | number | string, options?: DateFormatOptions): string
|
|
240
|
+
/**
|
|
241
|
+
* Formats a relative time (e.g. "2 hours ago").
|
|
242
|
+
*
|
|
243
|
+
* @returns The locale-formatted relative time string.
|
|
244
|
+
*/
|
|
245
|
+
formatRelativeTime(
|
|
246
|
+
value: Date | number,
|
|
247
|
+
options?: {
|
|
248
|
+
unit?: Intl.RelativeTimeFormatUnit
|
|
249
|
+
},
|
|
250
|
+
): string
|
|
251
|
+
/**
|
|
252
|
+
* Formats a list (e.g. "A, B, and C").
|
|
253
|
+
*
|
|
254
|
+
* @returns The locale-formatted list string.
|
|
255
|
+
*/
|
|
256
|
+
formatList(
|
|
257
|
+
values: string[],
|
|
258
|
+
options?: {
|
|
259
|
+
type?: 'conjunction' | 'disjunction' | 'unit'
|
|
260
|
+
},
|
|
261
|
+
): string
|
|
262
|
+
/**
|
|
263
|
+
* Subscribes to locale changes.
|
|
264
|
+
*
|
|
265
|
+
* @returns An unsubscribe function.
|
|
266
|
+
*/
|
|
267
|
+
onLocaleChange(listener: (locale: string) => void): () => void
|
|
268
|
+
/**
|
|
269
|
+
* Gets the text direction for the current locale.
|
|
270
|
+
*
|
|
271
|
+
* @returns `'ltr'` or `'rtl'`.
|
|
272
|
+
*/
|
|
273
|
+
getDirection(): 'ltr' | 'rtl'
|
|
274
|
+
/**
|
|
275
|
+
* Checks if a translation key exists (alias for exists).
|
|
276
|
+
*/
|
|
277
|
+
hasKey?(key: string): boolean
|
|
278
|
+
/**
|
|
279
|
+
* Checks if the provider is ready.
|
|
280
|
+
*/
|
|
281
|
+
isReady?(): boolean
|
|
282
|
+
/**
|
|
283
|
+
* Registers a callback for when the provider is ready.
|
|
284
|
+
*/
|
|
285
|
+
onReady?(callback: () => void): () => void
|
|
286
|
+
/**
|
|
287
|
+
* Registers a lazily-loaded content module for automatic reload on locale changes.
|
|
288
|
+
* All registered content is reloaded during `setLocale()` before listeners fire,
|
|
289
|
+
* ensuring content is available on the first re-render with no flash.
|
|
290
|
+
*
|
|
291
|
+
* Idempotent: registering the same module name twice is a no-op.
|
|
292
|
+
*/
|
|
293
|
+
registerContent?(module: string, loader: (locale: string) => Promise<void>): void
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
#### `LocaleConfig`
|
|
298
|
+
|
|
299
|
+
Configuration for a supported locale (code, display name, text direction, translations or lazy loader).
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
interface LocaleConfig {
|
|
303
|
+
/**
|
|
304
|
+
* Locale code (e.g., 'en-US', 'fr-FR').
|
|
305
|
+
*/
|
|
306
|
+
code: string
|
|
307
|
+
/**
|
|
308
|
+
* Display name (e.g., 'English (US)', 'Francais').
|
|
309
|
+
*/
|
|
310
|
+
name: string
|
|
311
|
+
/**
|
|
312
|
+
* Native display name.
|
|
313
|
+
*/
|
|
314
|
+
nativeName?: string
|
|
315
|
+
/**
|
|
316
|
+
* Text direction.
|
|
317
|
+
*/
|
|
318
|
+
direction?: 'ltr' | 'rtl'
|
|
319
|
+
/**
|
|
320
|
+
* Translations for this locale.
|
|
321
|
+
*/
|
|
322
|
+
translations?: Translations
|
|
323
|
+
/**
|
|
324
|
+
* Lazy loader for translations. Called on first setLocale() to this locale.
|
|
325
|
+
* When provided, translations can be omitted and will be loaded on demand.
|
|
326
|
+
*/
|
|
327
|
+
loader?: () => Promise<Translations>
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
#### `NumberFormatOptions`
|
|
332
|
+
|
|
333
|
+
Number format options.
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
interface NumberFormatOptions {
|
|
337
|
+
/**
|
|
338
|
+
* Number style.
|
|
339
|
+
*/
|
|
340
|
+
style?: 'decimal' | 'currency' | 'percent' | 'unit'
|
|
341
|
+
/**
|
|
342
|
+
* Currency code (for currency style).
|
|
343
|
+
*/
|
|
344
|
+
currency?: string
|
|
345
|
+
/**
|
|
346
|
+
* Minimum fraction digits.
|
|
347
|
+
*/
|
|
348
|
+
minimumFractionDigits?: number
|
|
349
|
+
/**
|
|
350
|
+
* Maximum fraction digits.
|
|
351
|
+
*/
|
|
352
|
+
maximumFractionDigits?: number
|
|
353
|
+
/**
|
|
354
|
+
* Use grouping separators.
|
|
355
|
+
*/
|
|
356
|
+
useGrouping?: boolean
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
#### `Translations`
|
|
361
|
+
|
|
362
|
+
Translation key/value map.
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
interface Translations {
|
|
366
|
+
[key: string]: string | Translations
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
### Types
|
|
371
|
+
|
|
372
|
+
#### `InterpolationValues`
|
|
373
|
+
|
|
374
|
+
Key-value map of interpolation variables passed to a translation string (e.g. `{ name: 'World' }`).
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
type InterpolationValues = Record<string, string | number | boolean | Date>
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### Functions
|
|
381
|
+
|
|
382
|
+
#### `createI18nextProvider(config)`
|
|
383
|
+
|
|
384
|
+
Creates a framework-agnostic i18n provider backed by i18next. Supports eager and lazy-loaded
|
|
385
|
+
locale translations, browser language detection, custom plugins, and Intl-based number/date/list formatting.
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
function createI18nextProvider(
|
|
389
|
+
config?: I18nextProviderConfig,
|
|
390
|
+
): I18nProvider & { i18n: I18nInstance; initialize: () => Promise<void> }
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
- `config` — i18next provider configuration including `defaultLocale`, `locales` (with optional `loader` for lazy loading), `detection` flag, `debug`, `plugins`, and raw `i18nextOptions`.
|
|
394
|
+
|
|
395
|
+
**Returns:** An `I18nProvider` with translation, formatting, locale management methods, plus the underlying `i18n` instance and `initialize()` function.
|
|
396
|
+
|
|
397
|
+
#### `localeConfigToResources(locales)`
|
|
398
|
+
|
|
399
|
+
Converts an array of molecule `LocaleConfig` objects to the i18next resource bundle format.
|
|
400
|
+
Each locale's translations are placed under a `translation` namespace keyed by locale code.
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
function localeConfigToResources(
|
|
404
|
+
locales: LocaleConfig[],
|
|
405
|
+
): Record<string, { translation: Translations }>
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
- `locales` — The locale configurations with code and translations.
|
|
409
|
+
|
|
410
|
+
**Returns:** An i18next-compatible resources object (e.g. `{ en: { translation: { ... } } }`).
|
|
411
|
+
|
|
412
|
+
### Constants
|
|
413
|
+
|
|
414
|
+
#### `provider`
|
|
415
|
+
|
|
416
|
+
Default provider instance.
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
const provider: I18nProvider & { i18n: I18nInstance; initialize: () => Promise<void> }
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
## Core Interface
|
|
423
|
+
|
|
424
|
+
Implements `@molecule/app-i18n` interface.
|
|
425
|
+
|
|
426
|
+
## Bond Wiring
|
|
427
|
+
|
|
428
|
+
Setup function to register this provider with the core interface:
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
import { setProvider } from '@molecule/app-i18n'
|
|
432
|
+
import { provider } from '@molecule/app-i18n-i18next'
|
|
433
|
+
|
|
434
|
+
export function setupI18nI18next(): void {
|
|
435
|
+
setProvider(provider)
|
|
436
|
+
}
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
## Injection Notes
|
|
440
|
+
|
|
441
|
+
### Requirements
|
|
442
|
+
|
|
443
|
+
Peer dependencies:
|
|
444
|
+
|
|
445
|
+
- `@molecule/app-i18n` ^1.0.1
|
|
446
|
+
- `@molecule/app-logger` ^1.0.1
|
|
447
|
+
|
|
448
|
+
### Runtime Dependencies
|
|
449
|
+
|
|
450
|
+
- `@molecule/app-i18n`
|
|
451
|
+
- `@molecule/app-logger`
|
|
452
|
+
- `i18next`
|
|
453
|
+
- `i18next-browser-languagedetector`
|
|
454
|
+
|
|
455
|
+
**Startup locale vs. `detection`:** with `detection: true` (the default),
|
|
456
|
+
`defaultLocale` is only the FALLBACK — the actual startup locale is
|
|
457
|
+
whatever the browser detector resolves (querystring, then `navigator`, by
|
|
458
|
+
default). Apps that want a pinned startup locale must pass
|
|
459
|
+
`detection: false` (or an explicit `i18nextOptions.lng`).
|
|
460
|
+
|
|
461
|
+
**`setLocale()` contract:** THROWS `Error('Locale "<code>" not found')`
|
|
462
|
+
for a locale that was never registered — via the `locales` config,
|
|
463
|
+
`addLocale()`, or `addTranslations()` (all three register it). It does
|
|
464
|
+
NOT fall through to real i18next's own `changeLanguage()` degrade-to-
|
|
465
|
+
`fallbackLng` behavior, matching `@molecule/app-i18n`'s core simple
|
|
466
|
+
provider and `@molecule/api-i18n-simple`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-i18n-i18next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Framework-agnostic i18next provider for molecule.dev",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"dist"
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md"
|
|
20
21
|
],
|
|
21
22
|
"license": "Apache-2.0",
|
|
22
23
|
"dependencies": {
|
|
@@ -29,8 +30,8 @@
|
|
|
29
30
|
"vitest": "4.1.10"
|
|
30
31
|
},
|
|
31
32
|
"peerDependencies": {
|
|
32
|
-
"@molecule/app-i18n": "^1.0.
|
|
33
|
-
"@molecule/app-logger": "^1.0.
|
|
33
|
+
"@molecule/app-i18n": "^1.0.1",
|
|
34
|
+
"@molecule/app-logger": "^1.0.1"
|
|
34
35
|
},
|
|
35
36
|
"repository": {
|
|
36
37
|
"type": "git",
|