@inglorious/i18n 2.0.0 → 2.1.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 +16 -1
- package/package.json +5 -5
- package/src/index.js +11 -2
- package/src/index.test.js +37 -4
- package/types/index.d.ts +7 -0
package/README.md
CHANGED
|
@@ -105,6 +105,21 @@ periods.map((period) => ({ ...period, label: t(period.label) }))
|
|
|
105
105
|
t("Lombardy") // data, not copy: as is
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
+
## Content with its own translations
|
|
109
|
+
|
|
110
|
+
Records that come from a database or a CMS can't have keys in the catalogues: they may not exist yet when the app ships. Each brings its own translations instead, and `localized` picks the one in the active locale, else the default locale's:
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
const genre = {
|
|
114
|
+
value: "jazz",
|
|
115
|
+
labels: { en: "Jazz", it: "Jazz", ja: "ジャズ" },
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
localized(genre.labels) // "ジャズ" in Japanese, "Jazz" in French
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Keep the record as it is in state and call `localized` where it is rendered, so it follows a switch.
|
|
122
|
+
|
|
108
123
|
## Messages in state
|
|
109
124
|
|
|
110
125
|
For the same reason, state holds a message's key rather than its text: an error saved in Italian would stay Italian after a switch to English. `t` takes the message as it is kept: a key, `{ key, values }` for one that interpolates, or text that is not a key, such as an error from a server, which comes back as it is.
|
|
@@ -119,7 +134,7 @@ html`<p>${t(entity.error)}</p>`
|
|
|
119
134
|
|
|
120
135
|
### `createI18n(options)`
|
|
121
136
|
|
|
122
|
-
Returns `{ I18n, ready, languages, t, trans, currentLocale, i18next }`.
|
|
137
|
+
Returns `{ I18n, ready, languages, t, trans, localized, currentLocale, i18next }`.
|
|
123
138
|
|
|
124
139
|
- `catalogues`: the catalogues by file path, as `import.meta.glob("./locales/*.json")` returns them. A lazy glob loads each on demand; an eager one (`{ eager: true }`) bundles them all.
|
|
125
140
|
- `resources`: catalogues bundled with the app, `{ [locale]: { translation: { … } } }`. The lower-level alternative to `catalogues`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/i18n",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Internationalization for Inglorious Web: an i18next instance driven by a store entity.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"i18next": "^
|
|
37
|
+
"i18next": "^27.2.1"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@inglorious/eslint-config": "1.2.0",
|
|
41
|
-
"@inglorious/store": "12.
|
|
42
|
-
"prettier": "^3.
|
|
43
|
-
"vitest": "^5.
|
|
41
|
+
"@inglorious/store": "12.9.1",
|
|
42
|
+
"prettier": "^3.10.1",
|
|
43
|
+
"vitest": "^5.1.1"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
46
|
"node": ">= 26"
|
package/src/index.js
CHANGED
|
@@ -204,12 +204,21 @@ export function createI18n({
|
|
|
204
204
|
return instance.t(key, options)
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Content's own translation, such as a record's `{ en: "Jazz", it: "Jazz" }`
|
|
209
|
+
* from a database or a CMS: the active locale's, else the default locale's.
|
|
210
|
+
*/
|
|
211
|
+
function localized(translations) {
|
|
212
|
+
return translations?.[instance.language] ?? translations?.[fallbackLocale]
|
|
213
|
+
}
|
|
214
|
+
|
|
207
215
|
return {
|
|
208
216
|
I18n,
|
|
209
217
|
ready,
|
|
210
218
|
languages,
|
|
211
219
|
t,
|
|
212
220
|
trans,
|
|
221
|
+
localized,
|
|
213
222
|
currentLocale: () => instance.language,
|
|
214
223
|
i18next: instance,
|
|
215
224
|
}
|
|
@@ -350,7 +359,7 @@ function languageName(locale) {
|
|
|
350
359
|
function readLocale(storageKey) {
|
|
351
360
|
if (!storageKey) return null
|
|
352
361
|
try {
|
|
353
|
-
return globalThis.localStorage?.getItem(storageKey) ?? null
|
|
362
|
+
return globalThis.window?.localStorage?.getItem(storageKey) ?? null
|
|
354
363
|
} catch {
|
|
355
364
|
return null
|
|
356
365
|
}
|
|
@@ -359,7 +368,7 @@ function readLocale(storageKey) {
|
|
|
359
368
|
function saveLocale(storageKey, locale) {
|
|
360
369
|
if (!storageKey) return
|
|
361
370
|
try {
|
|
362
|
-
globalThis.localStorage?.setItem(storageKey, locale)
|
|
371
|
+
globalThis.window?.localStorage?.setItem(storageKey, locale)
|
|
363
372
|
} catch {
|
|
364
373
|
// Not persisting the choice is better than breaking the switch.
|
|
365
374
|
}
|
package/src/index.test.js
CHANGED
|
@@ -26,10 +26,12 @@ function stubBrowser({ languages = [], saved = {}, search = "" } = {}) {
|
|
|
26
26
|
const store = { ...saved }
|
|
27
27
|
vi.stubGlobal("location", { search })
|
|
28
28
|
vi.stubGlobal("navigator", { languages, language: languages[0] })
|
|
29
|
-
vi.stubGlobal("
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
vi.stubGlobal("window", {
|
|
30
|
+
localStorage: {
|
|
31
|
+
getItem: (key) => store[key] ?? null,
|
|
32
|
+
setItem: (key, value) => {
|
|
33
|
+
store[key] = value
|
|
34
|
+
},
|
|
33
35
|
},
|
|
34
36
|
})
|
|
35
37
|
vi.stubGlobal("document", { documentElement: { lang: "", dir: "" } })
|
|
@@ -396,6 +398,37 @@ describe("catalogues", () => {
|
|
|
396
398
|
})
|
|
397
399
|
})
|
|
398
400
|
|
|
401
|
+
describe("localized", () => {
|
|
402
|
+
const jazz = { en: "Jazz", it: "Musica jazz" }
|
|
403
|
+
|
|
404
|
+
it("picks content's translation in the active locale", () => {
|
|
405
|
+
stubBrowser({ languages: ["it"] })
|
|
406
|
+
const { localized } = createI18n({ resources })
|
|
407
|
+
|
|
408
|
+
expect(localized(jazz)).toBe("Musica jazz")
|
|
409
|
+
})
|
|
410
|
+
|
|
411
|
+
it("falls back to the default locale, and to nothing", () => {
|
|
412
|
+
stubBrowser({ languages: ["it"] })
|
|
413
|
+
const { localized } = createI18n({ resources, defaultLocale: "en" })
|
|
414
|
+
|
|
415
|
+
expect(localized({ en: "Jazz" })).toBe("Jazz")
|
|
416
|
+
expect(localized({ fr: "Jazz" })).toBeUndefined()
|
|
417
|
+
expect(localized(undefined)).toBeUndefined()
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
it("follows a switch", () => {
|
|
421
|
+
stubBrowser({ languages: ["en"] })
|
|
422
|
+
const { I18n, localized } = createI18n({ resources })
|
|
423
|
+
const entity = {}
|
|
424
|
+
I18n.create(entity)
|
|
425
|
+
|
|
426
|
+
I18n.localeChange(entity, "it")
|
|
427
|
+
|
|
428
|
+
expect(localized(jazz)).toBe("Musica jazz")
|
|
429
|
+
})
|
|
430
|
+
})
|
|
431
|
+
|
|
399
432
|
describe("languages", () => {
|
|
400
433
|
it("names each language in itself, for a picker", () => {
|
|
401
434
|
stubBrowser({ languages: ["en"] })
|
package/types/index.d.ts
CHANGED
|
@@ -139,6 +139,13 @@ export interface I18n {
|
|
|
139
139
|
key: string | string[],
|
|
140
140
|
options?: TransOptions<Part>,
|
|
141
141
|
) => Array<string | Part>
|
|
142
|
+
/**
|
|
143
|
+
* Content's own translation, such as a record's `{ en: "Jazz", it: "Jazz" }`
|
|
144
|
+
* from a database or a CMS: the active locale's, else the default locale's.
|
|
145
|
+
*/
|
|
146
|
+
localized: <Value = string>(
|
|
147
|
+
translations: Partial<Record<string, Value>> | null | undefined,
|
|
148
|
+
) => Value | undefined
|
|
142
149
|
/** The underlying i18next instance, for anything the adapter does not cover. */
|
|
143
150
|
i18next: i18n
|
|
144
151
|
}
|