@inglorious/i18n 1.3.0 → 2.0.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 +18 -7
- package/package.json +3 -3
- package/src/index.js +38 -53
- package/src/index.test.js +38 -10
- package/src/trans.js +0 -4
- package/src/trans.test.js +0 -1
- package/types/index.d.ts +16 -11
package/README.md
CHANGED
|
@@ -96,20 +96,30 @@ api.notify("#i18n:userLocaleClear")
|
|
|
96
96
|
|
|
97
97
|
## Labels in data
|
|
98
98
|
|
|
99
|
-
Options defined in data would freeze in the language of the moment they were created.
|
|
99
|
+
Options defined in data would freeze in the language of the moment they were created. Keep the key as the label, and translate it where it is rendered. A label that is not a key, such as a place name, comes back as it is:
|
|
100
100
|
|
|
101
101
|
```javascript
|
|
102
|
-
const periods = [
|
|
102
|
+
const periods = [{ value: "week", label: "periods.week" }]
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
periods.map((period) => ({ ...period, label: t(period.label) }))
|
|
105
|
+
t("Lombardy") // data, not copy: as is
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Messages in state
|
|
109
|
+
|
|
110
|
+
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.
|
|
111
|
+
|
|
112
|
+
```javascript
|
|
113
|
+
entity.error = { key: "errors.tooLong", values: { max: 80 } }
|
|
114
|
+
|
|
115
|
+
html`<p>${t(entity.error)}</p>`
|
|
106
116
|
```
|
|
107
117
|
|
|
108
118
|
## API
|
|
109
119
|
|
|
110
120
|
### `createI18n(options)`
|
|
111
121
|
|
|
112
|
-
Returns `{ I18n, ready, languages, t, trans, currentLocale,
|
|
122
|
+
Returns `{ I18n, ready, languages, t, trans, currentLocale, i18next }`.
|
|
113
123
|
|
|
114
124
|
- `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.
|
|
115
125
|
- `resources`: catalogues bundled with the app, `{ [locale]: { translation: { … } } }`. The lower-level alternative to `catalogues`.
|
|
@@ -119,9 +129,10 @@ Returns `{ I18n, ready, languages, t, trans, currentLocale, labelled, translated
|
|
|
119
129
|
- `isServerRendered`: whether the page comes rendered from a server, as with SSX. It then starts in the language of `<html lang>`, the one the server rendered, so hydration keeps it and only its catalogue loads. Leave it off for a page served as is, whose `<html lang>` says nothing about the visitor.
|
|
120
130
|
- `defaultLocale`: the fallback, and the locale when nothing else matches. Defaults to the first locale.
|
|
121
131
|
- `storageKey`: where to remember the user's choice in `localStorage`. Omit it to not remember the choice.
|
|
132
|
+
- `queryParam`: the query parameter a link names the language with, such as `"lang"` for `?lang=it`, so a page linking to the app can open it in the page's language. That locale is remembered, as a choice would be.
|
|
122
133
|
- `i18nextOptions`: any further i18next options.
|
|
123
134
|
|
|
124
|
-
The starting locale is that of `<html lang>` on a server-rendered page, when supported, else the saved choice, else the first of the device's languages that is supported (`it-CH` picks `it`), else `defaultLocale`.
|
|
135
|
+
The starting locale is that of `<html lang>` on a server-rendered page, when supported, else the one in `queryParam`, else the saved choice, else the first of the device's languages that is supported (`it-CH` picks `it`), else `defaultLocale`.
|
|
125
136
|
|
|
126
137
|
`ready` resolves once the starting locale and the fallback are loaded: at once when they are bundled. With every catalogue bundled, i18next also switches synchronously, so there is no loading state to handle.
|
|
127
138
|
|
|
@@ -186,6 +197,6 @@ It is safe with user data:
|
|
|
186
197
|
- A tag without a component (`<script>`, a typo, a stray `</b>`) shows as written instead of becoming markup.
|
|
187
198
|
- Plurals and formatters work as in `t`: `trans("events", { values: { count } })`.
|
|
188
199
|
|
|
189
|
-
### `detectLocale({ locales, defaultLocale, storageKey })`
|
|
200
|
+
### `detectLocale({ locales, defaultLocale, storageKey, queryParam })`
|
|
190
201
|
|
|
191
202
|
The detection `createI18n` uses, exported for apps that need it elsewhere.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/i18n",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@inglorious/eslint-config": "1.2.0",
|
|
41
|
-
"@inglorious/store": "12.1.
|
|
41
|
+
"@inglorious/store": "12.1.1",
|
|
42
42
|
"prettier": "^3.9.6",
|
|
43
43
|
"vitest": "^5.0.0"
|
|
44
44
|
},
|
|
45
45
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
46
|
+
"node": ">= 26"
|
|
47
47
|
},
|
|
48
48
|
"scripts": {
|
|
49
49
|
"format": "prettier --write .",
|
package/src/index.js
CHANGED
|
@@ -37,6 +37,7 @@ export function createI18n({
|
|
|
37
37
|
isServerRendered = false,
|
|
38
38
|
defaultLocale,
|
|
39
39
|
storageKey,
|
|
40
|
+
queryParam,
|
|
40
41
|
saveUserLocale,
|
|
41
42
|
i18nextOptions = {},
|
|
42
43
|
}) {
|
|
@@ -55,7 +56,15 @@ export function createI18n({
|
|
|
55
56
|
// detected instead.
|
|
56
57
|
const startLocale =
|
|
57
58
|
(isServerRendered && matchLocale(getDocumentLanguage(), locales)) ||
|
|
58
|
-
detectLocale({
|
|
59
|
+
detectLocale({
|
|
60
|
+
locales,
|
|
61
|
+
defaultLocale: fallbackLocale,
|
|
62
|
+
storageKey,
|
|
63
|
+
queryParam,
|
|
64
|
+
})
|
|
65
|
+
if (startLocale === queryLocale(queryParam, locales)) {
|
|
66
|
+
saveLocale(storageKey, startLocale)
|
|
67
|
+
}
|
|
59
68
|
const languages = locales.map((locale) => ({
|
|
60
69
|
value: locale,
|
|
61
70
|
label: languageName(locale),
|
|
@@ -65,24 +74,19 @@ export function createI18n({
|
|
|
65
74
|
instance.init({
|
|
66
75
|
...i18nextOptions,
|
|
67
76
|
resources,
|
|
68
|
-
// Until a loaded catalogue arrives, keys fall back rather than wait.
|
|
69
77
|
partialBundledLanguages: true,
|
|
70
78
|
lng: startLocale,
|
|
71
79
|
fallbackLng: fallbackLocale,
|
|
72
80
|
supportedLngs: locales,
|
|
73
|
-
// lit-html escapes every text binding already; escaping here too would
|
|
74
|
-
// print entities such as & on screen.
|
|
75
81
|
interpolation: { escapeValue: false, ...i18nextOptions.interpolation },
|
|
76
82
|
initAsync: false,
|
|
77
83
|
})
|
|
78
84
|
|
|
79
85
|
const loader = createLoader(instance, resources, loadLocale)
|
|
80
86
|
|
|
81
|
-
// The fallback too: it fills in whatever the starting locale is missing.
|
|
82
87
|
const ready = Promise.all(
|
|
83
88
|
[...new Set([startLocale, fallbackLocale])].map((locale) =>
|
|
84
89
|
loader.load(locale).catch((error) => {
|
|
85
|
-
// The app still starts, with whatever did load.
|
|
86
90
|
console.warn(`[i18n] could not load "${locale}":`, error)
|
|
87
91
|
}),
|
|
88
92
|
),
|
|
@@ -100,9 +104,6 @@ export function createI18n({
|
|
|
100
104
|
applyDocumentLanguage(entity.locale, entity.dir)
|
|
101
105
|
},
|
|
102
106
|
|
|
103
|
-
// The user's choice: a locale, or a picker's `{ value }`. With
|
|
104
|
-
// `saveUserLocale` it is saved for the signed-in user at once, and kept
|
|
105
|
-
// for whoever signs in next otherwise.
|
|
106
107
|
localeChange(entity, payload, api) {
|
|
107
108
|
const locale = payload?.value ?? payload
|
|
108
109
|
if (!requestLocale(entity, locale, api)) return
|
|
@@ -112,8 +113,6 @@ export function createI18n({
|
|
|
112
113
|
else entity.isUnsaved = true
|
|
113
114
|
},
|
|
114
115
|
|
|
115
|
-
// Only the latest request switches: a slower load for a locale the user
|
|
116
|
-
// has since moved away from is kept for later, but not applied.
|
|
117
116
|
localeLoadSuccess(entity, locale) {
|
|
118
117
|
if (entity.loadingLocale !== locale) return
|
|
119
118
|
entity.loadingLocale = null
|
|
@@ -126,10 +125,6 @@ export function createI18n({
|
|
|
126
125
|
console.warn(`[i18n] could not load "${locale}":`, error)
|
|
127
126
|
},
|
|
128
127
|
|
|
129
|
-
// The signed-in user and the locale saved for them, or none yet. A choice
|
|
130
|
-
// made before they signed in is the more recent, so it is saved instead
|
|
131
|
-
// of applying theirs; with nothing saved, the current one is. Once per
|
|
132
|
-
// user, so reloading their data does not undo a change still being saved.
|
|
133
128
|
userLocaleLoad(entity, { userId, locale }, api) {
|
|
134
129
|
if (entity.userId === userId) return
|
|
135
130
|
entity.userId = userId
|
|
@@ -143,32 +138,22 @@ export function createI18n({
|
|
|
143
138
|
requestLocale(entity, locale, api)
|
|
144
139
|
},
|
|
145
140
|
|
|
146
|
-
// Signed out: later choices wait for the next user.
|
|
147
141
|
userLocaleClear(entity) {
|
|
148
142
|
entity.userId = null
|
|
149
143
|
entity.isUnsaved = false
|
|
150
144
|
},
|
|
151
145
|
}
|
|
152
146
|
|
|
153
|
-
// Switches to `locale`, loading it first if needed, so `loadingLocale` names
|
|
154
|
-
// it until its catalogue is in: what a server render or a hydration waits for.
|
|
155
|
-
// Whether it is a change.
|
|
156
147
|
function requestLocale(entity, locale, api) {
|
|
157
148
|
if (!locales.includes(locale)) return false
|
|
158
149
|
const isChange = locale !== entity.locale
|
|
159
|
-
// Choosing the current locale again also cancels a pending change.
|
|
160
150
|
entity.loadingLocale = null
|
|
161
151
|
|
|
162
152
|
if (loader.isLoaded(locale)) {
|
|
163
|
-
// The i18next instance is shared by every store, as on a server rendering
|
|
164
|
-
// pages in different languages, so it can be elsewhere when the entity is not.
|
|
165
153
|
if (isChange || locale !== instance.language) switchLocale(entity, locale)
|
|
166
154
|
return isChange
|
|
167
155
|
}
|
|
168
156
|
|
|
169
|
-
// Also the current locale, while the catalogue it started with is loading.
|
|
170
|
-
// The id is taken now: the promise settles after this batch, when the
|
|
171
|
-
// entity here is no longer the live one.
|
|
172
157
|
const id = entity.id
|
|
173
158
|
entity.loadingLocale = locale
|
|
174
159
|
loader.load(locale).then(
|
|
@@ -208,7 +193,14 @@ export function createI18n({
|
|
|
208
193
|
)
|
|
209
194
|
}
|
|
210
195
|
|
|
196
|
+
/**
|
|
197
|
+
* Translates a key, or a message kept in state as `{ key, values }`. Text
|
|
198
|
+
* that is not a key, such as an error from a server, comes back as it is.
|
|
199
|
+
*/
|
|
211
200
|
function t(key, options) {
|
|
201
|
+
if (key && typeof key === "object" && !Array.isArray(key)) {
|
|
202
|
+
return instance.t(key.key, { ...key.values, ...options })
|
|
203
|
+
}
|
|
212
204
|
return instance.t(key, options)
|
|
213
205
|
}
|
|
214
206
|
|
|
@@ -218,34 +210,29 @@ export function createI18n({
|
|
|
218
210
|
languages,
|
|
219
211
|
t,
|
|
220
212
|
trans,
|
|
221
|
-
// The active locale, for `Intl` formatters.
|
|
222
213
|
currentLocale: () => instance.language,
|
|
223
|
-
// A data item whose label follows the language: `label` reads through `t`
|
|
224
|
-
// every time, and `labelKey` survives being copied into store state,
|
|
225
|
-
// where the getter would freeze into the text of the moment.
|
|
226
|
-
labelled: (labelKey, fields = {}) => ({
|
|
227
|
-
...fields,
|
|
228
|
-
labelKey,
|
|
229
|
-
get label() {
|
|
230
|
-
return t(labelKey)
|
|
231
|
-
},
|
|
232
|
-
}),
|
|
233
|
-
// An option's label: translated when it carries a key, as is otherwise.
|
|
234
|
-
translatedLabel: (option) =>
|
|
235
|
-
option.labelKey ? t(option.labelKey) : option.label,
|
|
236
214
|
i18next: instance,
|
|
237
215
|
}
|
|
238
216
|
}
|
|
239
217
|
|
|
240
218
|
/**
|
|
241
|
-
* The locale to start with: the one
|
|
242
|
-
* first of the device's languages that is
|
|
243
|
-
* language, so `it-CH` picks `it`), else the
|
|
219
|
+
* The locale to start with: the one a link names in `queryParam`, else the one
|
|
220
|
+
* saved by a previous choice, else the first of the device's languages that is
|
|
221
|
+
* supported (exactly, or by its base language, so `it-CH` picks `it`), else the
|
|
222
|
+
* default.
|
|
244
223
|
*
|
|
245
|
-
* @param {{ locales: string[], defaultLocale: string, storageKey?: string }} options
|
|
224
|
+
* @param {{ locales: string[], defaultLocale: string, storageKey?: string, queryParam?: string }} options
|
|
246
225
|
* @returns {string}
|
|
247
226
|
*/
|
|
248
|
-
export function detectLocale({
|
|
227
|
+
export function detectLocale({
|
|
228
|
+
locales,
|
|
229
|
+
defaultLocale,
|
|
230
|
+
storageKey,
|
|
231
|
+
queryParam,
|
|
232
|
+
}) {
|
|
233
|
+
const linked = queryLocale(queryParam, locales)
|
|
234
|
+
if (linked) return linked
|
|
235
|
+
|
|
249
236
|
const saved = readLocale(storageKey)
|
|
250
237
|
if (saved && locales.includes(saved)) return saved
|
|
251
238
|
|
|
@@ -261,8 +248,13 @@ export function detectLocale({ locales, defaultLocale, storageKey }) {
|
|
|
261
248
|
return defaultLocale
|
|
262
249
|
}
|
|
263
250
|
|
|
264
|
-
|
|
265
|
-
|
|
251
|
+
function queryLocale(queryParam, locales) {
|
|
252
|
+
if (!queryParam) return null
|
|
253
|
+
const search = globalThis.location?.search ?? ""
|
|
254
|
+
const locale = new URLSearchParams(search).get(queryParam)
|
|
255
|
+
return locales.includes(locale) ? locale : null
|
|
256
|
+
}
|
|
257
|
+
|
|
266
258
|
function matchLocale(language, locales) {
|
|
267
259
|
if (!language) return null
|
|
268
260
|
if (locales.includes(language)) return language
|
|
@@ -323,7 +315,6 @@ function createLoader(instance, resources, loadLocale) {
|
|
|
323
315
|
if (isLoaded(locale)) return Promise.resolve()
|
|
324
316
|
if (pending.has(locale)) return pending.get(locale)
|
|
325
317
|
|
|
326
|
-
// Called right away; a loader that throws rejects like one that fails.
|
|
327
318
|
const promise = new Promise((resolve) => resolve(loadLocale(locale)))
|
|
328
319
|
.then((bundles) => {
|
|
329
320
|
for (const [namespace, catalogue] of Object.entries(bundles ?? {})) {
|
|
@@ -361,8 +352,6 @@ function readLocale(storageKey) {
|
|
|
361
352
|
try {
|
|
362
353
|
return globalThis.localStorage?.getItem(storageKey) ?? null
|
|
363
354
|
} catch {
|
|
364
|
-
// Storage can be unavailable (private mode, server rendering): fall back
|
|
365
|
-
// to detection rather than failing.
|
|
366
355
|
return null
|
|
367
356
|
}
|
|
368
357
|
}
|
|
@@ -380,10 +369,6 @@ function getDocumentLanguage() {
|
|
|
380
369
|
return globalThis.document?.documentElement.lang
|
|
381
370
|
}
|
|
382
371
|
|
|
383
|
-
// Screen readers, hyphenation and the browser's own spell checking read the
|
|
384
|
-
// document language, so it follows the app's. So does the direction: in
|
|
385
|
-
// Arabic, Hebrew or Persian the whole page reads right to left, and CSS that
|
|
386
|
-
// uses logical properties (`margin-inline-start`, `text-align: start`) follows.
|
|
387
372
|
function applyDocumentLanguage(locale, dir) {
|
|
388
373
|
if (typeof document === "undefined") return
|
|
389
374
|
document.documentElement.lang = locale
|
package/src/index.test.js
CHANGED
|
@@ -22,8 +22,9 @@ const resources = {
|
|
|
22
22
|
},
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
function stubBrowser({ languages = [], saved = {} } = {}) {
|
|
25
|
+
function stubBrowser({ languages = [], saved = {}, search = "" } = {}) {
|
|
26
26
|
const store = { ...saved }
|
|
27
|
+
vi.stubGlobal("location", { search })
|
|
27
28
|
vi.stubGlobal("navigator", { languages, language: languages[0] })
|
|
28
29
|
vi.stubGlobal("localStorage", {
|
|
29
30
|
getItem: (key) => store[key] ?? null,
|
|
@@ -61,6 +62,16 @@ describe("detectLocale", () => {
|
|
|
61
62
|
expect(detectLocale(options)).toBe("en")
|
|
62
63
|
})
|
|
63
64
|
|
|
65
|
+
it("prefers the locale a link names", () => {
|
|
66
|
+
stubBrowser({ saved: { lang: "en" }, search: "?lang=it" })
|
|
67
|
+
expect(detectLocale({ ...options, queryParam: "lang" })).toBe("it")
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("ignores a linked locale that is not supported", () => {
|
|
71
|
+
stubBrowser({ saved: { lang: "it" }, search: "?lang=de" })
|
|
72
|
+
expect(detectLocale({ ...options, queryParam: "lang" })).toBe("it")
|
|
73
|
+
})
|
|
74
|
+
|
|
64
75
|
it("ignores a saved locale that is no longer supported", () => {
|
|
65
76
|
stubBrowser({ languages: ["en"], saved: { lang: "de" } })
|
|
66
77
|
expect(detectLocale(options)).toBe("en")
|
|
@@ -166,7 +177,6 @@ describe("createI18n", () => {
|
|
|
166
177
|
describe("loadLocale", () => {
|
|
167
178
|
const { en, it: italian } = resources
|
|
168
179
|
|
|
169
|
-
// A loader whose loads resolve or fail when the test says so.
|
|
170
180
|
function controlledLoader() {
|
|
171
181
|
const loads = {}
|
|
172
182
|
const loadLocale = vi.fn(
|
|
@@ -501,22 +511,40 @@ describe("direction", () => {
|
|
|
501
511
|
})
|
|
502
512
|
|
|
503
513
|
describe("helpers", () => {
|
|
504
|
-
it("
|
|
505
|
-
stubBrowser({ languages: ["en"] })
|
|
506
|
-
const {
|
|
514
|
+
it("remembers the locale a link names", () => {
|
|
515
|
+
const saved = stubBrowser({ languages: ["en"], search: "?lang=it" })
|
|
516
|
+
const { t } = createI18n({
|
|
507
517
|
resources,
|
|
518
|
+
storageKey: "lang",
|
|
519
|
+
queryParam: "lang",
|
|
508
520
|
})
|
|
521
|
+
|
|
522
|
+
expect(t("hello")).toBe("Ciao")
|
|
523
|
+
expect(saved.lang).toBe("it")
|
|
524
|
+
})
|
|
525
|
+
|
|
526
|
+
it("translates messages kept in state, and leaves other text as it is", () => {
|
|
527
|
+
stubBrowser({ languages: ["en"] })
|
|
528
|
+
const { t } = createI18n({ resources })
|
|
529
|
+
|
|
530
|
+
expect(t("hello")).toBe("Hello")
|
|
531
|
+
expect(t({ key: "greet", values: { name: "Ada" } })).toBe("Hello, Ada")
|
|
532
|
+
expect(t("Network down: retry later.")).toBe("Network down: retry later.")
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
it("translates a label kept as a key in the active language", () => {
|
|
536
|
+
stubBrowser({ languages: ["en"] })
|
|
537
|
+
const { I18n, t, currentLocale } = createI18n({ resources })
|
|
509
538
|
const entity = {}
|
|
510
539
|
I18n.create(entity)
|
|
511
|
-
const option =
|
|
540
|
+
const option = { value: "greeting", label: "hello" }
|
|
512
541
|
|
|
513
|
-
expect(option.label).toBe("Hello")
|
|
542
|
+
expect(t(option.label)).toBe("Hello")
|
|
514
543
|
|
|
515
544
|
I18n.localeChange(entity, "it")
|
|
516
545
|
|
|
517
|
-
expect(option.label).toBe("Ciao")
|
|
546
|
+
expect(t(option.label)).toBe("Ciao")
|
|
518
547
|
expect(currentLocale()).toBe("it")
|
|
519
|
-
expect(
|
|
520
|
-
expect(translatedLabel({ label: "Lombardy" })).toBe("Lombardy")
|
|
548
|
+
expect(t("Lombardy")).toBe("Lombardy")
|
|
521
549
|
})
|
|
522
550
|
})
|
package/src/trans.js
CHANGED
|
@@ -28,7 +28,6 @@ export function parseTrans(raw, components, interpolate) {
|
|
|
28
28
|
|
|
29
29
|
const component = components[name]
|
|
30
30
|
if (!component) {
|
|
31
|
-
// Not one of the caller's tags: show it as it was written.
|
|
32
31
|
current.children.push(source)
|
|
33
32
|
} else if (selfClosing) {
|
|
34
33
|
current.children.push(component([]))
|
|
@@ -39,14 +38,11 @@ export function parseTrans(raw, components, interpolate) {
|
|
|
39
38
|
current = node.parent
|
|
40
39
|
current.children.push(component(node.children))
|
|
41
40
|
} else {
|
|
42
|
-
// A closing tag with nothing of its name open.
|
|
43
41
|
current.children.push(source)
|
|
44
42
|
}
|
|
45
43
|
}
|
|
46
44
|
addText(raw.slice(last))
|
|
47
45
|
|
|
48
|
-
// Tags left open were never components: put them back as text, contents
|
|
49
|
-
// and all.
|
|
50
46
|
while (current !== root) {
|
|
51
47
|
const node = current
|
|
52
48
|
current = node.parent
|
package/src/trans.test.js
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -34,6 +34,12 @@ export interface I18nOptions {
|
|
|
34
34
|
defaultLocale?: string
|
|
35
35
|
/** Where to remember the user's choice in localStorage. Omit to not remember it. */
|
|
36
36
|
storageKey?: string
|
|
37
|
+
/**
|
|
38
|
+
* The query parameter a link names the language with, as in `?lang=it`. A
|
|
39
|
+
* supported locale there wins over the saved choice and the device, and is
|
|
40
|
+
* remembered with `storageKey`.
|
|
41
|
+
*/
|
|
42
|
+
queryParam?: string
|
|
37
43
|
/**
|
|
38
44
|
* Saves the user's locale wherever the app keeps their preferences, e.g.
|
|
39
45
|
* `(locale, api) => api.notify("#profile:localeSave", locale)`. Called for a
|
|
@@ -63,8 +69,6 @@ export interface I18nEntity {
|
|
|
63
69
|
isUnsaved?: boolean
|
|
64
70
|
}
|
|
65
71
|
|
|
66
|
-
// A type alias rather than an interface, so it fits option types that allow
|
|
67
|
-
// extra fields, such as Inglorious UI's `SelectOption`.
|
|
68
72
|
export type LanguageOption = {
|
|
69
73
|
value: string
|
|
70
74
|
/** The language in itself: "Italiano", "Français". */
|
|
@@ -102,6 +106,9 @@ export type I18nType = {
|
|
|
102
106
|
userLocaleClear(entity: I18nEntity): void
|
|
103
107
|
}
|
|
104
108
|
|
|
109
|
+
/** A message kept in state: a key, a key with values, or text shown as it is. */
|
|
110
|
+
export type Message = string | { key: string; values?: TOptions }
|
|
111
|
+
|
|
105
112
|
export interface TransOptions<Part = unknown> {
|
|
106
113
|
/** Values for the `{{placeholders}}`. They are inserted as text, never as markup. */
|
|
107
114
|
values?: TOptions
|
|
@@ -118,15 +125,12 @@ export interface I18n {
|
|
|
118
125
|
languages: LanguageOption[]
|
|
119
126
|
/** The active locale, for `Intl` formatters. */
|
|
120
127
|
currentLocale: () => string
|
|
121
|
-
/**
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
translatedLabel: (option: { labelKey?: string; label?: string }) => string
|
|
128
|
-
/** Translates `key` in the active locale. Same signature as i18next's `t`. */
|
|
129
|
-
t: (key: string | string[], options?: TOptions) => string
|
|
128
|
+
/**
|
|
129
|
+
* Translates `key` in the active locale, as i18next's `t`. It also takes a
|
|
130
|
+
* message kept in state as `{ key, values }`; text that is not a key comes
|
|
131
|
+
* back as it is.
|
|
132
|
+
*/
|
|
133
|
+
t: (key: string | string[] | Message, options?: TOptions) => string
|
|
130
134
|
/**
|
|
131
135
|
* Translates `key` into template parts, rendering the tags in the translation
|
|
132
136
|
* through `components`. Tags without a component show as written.
|
|
@@ -145,4 +149,5 @@ export function detectLocale(options: {
|
|
|
145
149
|
locales: string[]
|
|
146
150
|
defaultLocale: string
|
|
147
151
|
storageKey?: string
|
|
152
|
+
queryParam?: string
|
|
148
153
|
}): string
|