@inglorious/i18n 2.0.0 → 2.1.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/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.0.0",
3
+ "version": "2.1.0",
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": "^26.5.2"
37
+ "i18next": "^27.0.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@inglorious/eslint-config": "1.2.0",
41
- "@inglorious/store": "12.1.1",
42
- "prettier": "^3.9.6",
43
- "vitest": "^5.0.0"
41
+ "@inglorious/store": "12.3.0",
42
+ "prettier": "^3.9.10",
43
+ "vitest": "^5.1.0"
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
  }
package/src/index.test.js CHANGED
@@ -396,6 +396,37 @@ describe("catalogues", () => {
396
396
  })
397
397
  })
398
398
 
399
+ describe("localized", () => {
400
+ const jazz = { en: "Jazz", it: "Musica jazz" }
401
+
402
+ it("picks content's translation in the active locale", () => {
403
+ stubBrowser({ languages: ["it"] })
404
+ const { localized } = createI18n({ resources })
405
+
406
+ expect(localized(jazz)).toBe("Musica jazz")
407
+ })
408
+
409
+ it("falls back to the default locale, and to nothing", () => {
410
+ stubBrowser({ languages: ["it"] })
411
+ const { localized } = createI18n({ resources, defaultLocale: "en" })
412
+
413
+ expect(localized({ en: "Jazz" })).toBe("Jazz")
414
+ expect(localized({ fr: "Jazz" })).toBeUndefined()
415
+ expect(localized(undefined)).toBeUndefined()
416
+ })
417
+
418
+ it("follows a switch", () => {
419
+ stubBrowser({ languages: ["en"] })
420
+ const { I18n, localized } = createI18n({ resources })
421
+ const entity = {}
422
+ I18n.create(entity)
423
+
424
+ I18n.localeChange(entity, "it")
425
+
426
+ expect(localized(jazz)).toBe("Musica jazz")
427
+ })
428
+ })
429
+
399
430
  describe("languages", () => {
400
431
  it("names each language in itself, for a picker", () => {
401
432
  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
  }