@inglorious/i18n 1.2.3 → 1.3.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 +2 -1
- package/package.json +1 -1
- package/src/index.js +23 -9
- package/src/index.test.js +57 -0
- package/types/index.d.ts +6 -0
package/README.md
CHANGED
|
@@ -116,11 +116,12 @@ Returns `{ I18n, ready, languages, t, trans, currentLocale, labelled, translated
|
|
|
116
116
|
- `loadLocale`: loads a locale that is not in `resources`, resolving to `{ translation: { … } }`. The lower-level alternative to a lazy glob.
|
|
117
117
|
- `locales`: the supported locales, in picker order. Defaults to those in `catalogues` and `resources`.
|
|
118
118
|
- `saveUserLocale`: saves the locale for the signed-in user. See [The user's language](#the-users-language).
|
|
119
|
+
- `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.
|
|
119
120
|
- `defaultLocale`: the fallback, and the locale when nothing else matches. Defaults to the first locale.
|
|
120
121
|
- `storageKey`: where to remember the user's choice in `localStorage`. Omit it to not remember the choice.
|
|
121
122
|
- `i18nextOptions`: any further i18next options.
|
|
122
123
|
|
|
123
|
-
The starting locale is the saved choice, else the first of the device's languages that is supported (`it-CH` picks `it`), else `defaultLocale`.
|
|
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`.
|
|
124
125
|
|
|
125
126
|
`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.
|
|
126
127
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/i18n",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.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",
|
package/src/index.js
CHANGED
|
@@ -34,6 +34,7 @@ export function createI18n({
|
|
|
34
34
|
resources: givenResources = {},
|
|
35
35
|
loadLocale: givenLoadLocale,
|
|
36
36
|
locales: givenLocales,
|
|
37
|
+
isServerRendered = false,
|
|
37
38
|
defaultLocale,
|
|
38
39
|
storageKey,
|
|
39
40
|
saveUserLocale,
|
|
@@ -49,11 +50,12 @@ export function createI18n({
|
|
|
49
50
|
]
|
|
50
51
|
const [firstLocale = "en"] = locales
|
|
51
52
|
const fallbackLocale = defaultLocale ?? firstLocale
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
// A server-rendered page is already in a language, which hydration must keep.
|
|
54
|
+
// In a page served as is, `<html lang>` is whatever the file says, so it is
|
|
55
|
+
// detected instead.
|
|
56
|
+
const startLocale =
|
|
57
|
+
(isServerRendered && matchLocale(getDocumentLanguage(), locales)) ||
|
|
58
|
+
detectLocale({ locales, defaultLocale: fallbackLocale, storageKey })
|
|
57
59
|
const languages = locales.map((locale) => ({
|
|
58
60
|
value: locale,
|
|
59
61
|
label: languageName(locale),
|
|
@@ -251,15 +253,23 @@ export function detectLocale({ locales, defaultLocale, storageKey }) {
|
|
|
251
253
|
typeof navigator === "undefined"
|
|
252
254
|
? []
|
|
253
255
|
: (navigator.languages ?? [navigator.language])
|
|
254
|
-
for (const language of preferred
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
if (locales.includes(base)) return base
|
|
256
|
+
for (const language of preferred) {
|
|
257
|
+
const locale = matchLocale(language, locales)
|
|
258
|
+
if (locale) return locale
|
|
258
259
|
}
|
|
259
260
|
|
|
260
261
|
return defaultLocale
|
|
261
262
|
}
|
|
262
263
|
|
|
264
|
+
// The supported locale for a language, exactly or by its base language, so
|
|
265
|
+
// `it-CH` picks `it`; null when there is none.
|
|
266
|
+
function matchLocale(language, locales) {
|
|
267
|
+
if (!language) return null
|
|
268
|
+
if (locales.includes(language)) return language
|
|
269
|
+
const [base] = language.split("-")
|
|
270
|
+
return locales.includes(base) ? base : null
|
|
271
|
+
}
|
|
272
|
+
|
|
263
273
|
/**
|
|
264
274
|
* Turns `catalogues`, a map from file path to catalogue such as
|
|
265
275
|
* `import.meta.glob` returns, into the resources and loader i18next needs.
|
|
@@ -366,6 +376,10 @@ function saveLocale(storageKey, locale) {
|
|
|
366
376
|
}
|
|
367
377
|
}
|
|
368
378
|
|
|
379
|
+
function getDocumentLanguage() {
|
|
380
|
+
return globalThis.document?.documentElement.lang
|
|
381
|
+
}
|
|
382
|
+
|
|
369
383
|
// Screen readers, hyphenation and the browser's own spell checking read the
|
|
370
384
|
// document language, so it follows the app's. So does the direction: in
|
|
371
385
|
// Arabic, Hebrew or Persian the whole page reads right to left, and CSS that
|
package/src/index.test.js
CHANGED
|
@@ -89,6 +89,45 @@ describe("createI18n", () => {
|
|
|
89
89
|
expect(document.documentElement.lang).toBe("it")
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
+
it("starts a server-rendered page in its language, ahead of the saved choice and the device", () => {
|
|
93
|
+
stubBrowser({ languages: ["en"], saved: { lang: "en" } })
|
|
94
|
+
document.documentElement.lang = "it-IT"
|
|
95
|
+
const { I18n, t } = createI18n({
|
|
96
|
+
resources,
|
|
97
|
+
isServerRendered: true,
|
|
98
|
+
storageKey: "lang",
|
|
99
|
+
})
|
|
100
|
+
const entity = {}
|
|
101
|
+
|
|
102
|
+
I18n.create(entity)
|
|
103
|
+
|
|
104
|
+
expect(entity.locale).toBe("it")
|
|
105
|
+
expect(document.documentElement.lang).toBe("it")
|
|
106
|
+
expect(t("hello")).toBe("Ciao")
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it("detects the locale when the page's language is unsupported", () => {
|
|
110
|
+
stubBrowser({ languages: ["it"] })
|
|
111
|
+
document.documentElement.lang = "de"
|
|
112
|
+
const { I18n } = createI18n({ resources, isServerRendered: true })
|
|
113
|
+
const entity = {}
|
|
114
|
+
|
|
115
|
+
I18n.create(entity)
|
|
116
|
+
|
|
117
|
+
expect(entity.locale).toBe("it")
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it("ignores the language of a page that is not server-rendered", () => {
|
|
121
|
+
stubBrowser({ languages: ["it"] })
|
|
122
|
+
document.documentElement.lang = "en"
|
|
123
|
+
const { I18n } = createI18n({ resources })
|
|
124
|
+
const entity = {}
|
|
125
|
+
|
|
126
|
+
I18n.create(entity)
|
|
127
|
+
|
|
128
|
+
expect(entity.locale).toBe("it")
|
|
129
|
+
})
|
|
130
|
+
|
|
92
131
|
it("switches language, remembers it and ignores unsupported ones", () => {
|
|
93
132
|
const saved = stubBrowser({ languages: ["en"] })
|
|
94
133
|
const { I18n, t } = createI18n({ resources, storageKey: "lang" })
|
|
@@ -168,6 +207,24 @@ describe("loadLocale", () => {
|
|
|
168
207
|
expect(t("hello")).toBe("Ciao")
|
|
169
208
|
})
|
|
170
209
|
|
|
210
|
+
it("loads only the server-rendered page's locale, not the device's", async () => {
|
|
211
|
+
stubBrowser({ languages: ["fr-FR"] })
|
|
212
|
+
document.documentElement.lang = "it"
|
|
213
|
+
const loadLocale = vi.fn(async () => italian)
|
|
214
|
+
const { ready, t } = createI18n({
|
|
215
|
+
resources: { en },
|
|
216
|
+
locales: ["en", "it", "fr"],
|
|
217
|
+
isServerRendered: true,
|
|
218
|
+
loadLocale,
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
await ready
|
|
222
|
+
|
|
223
|
+
expect(loadLocale).toHaveBeenCalledTimes(1)
|
|
224
|
+
expect(loadLocale).toHaveBeenCalledWith("it")
|
|
225
|
+
expect(t("hello")).toBe("Ciao")
|
|
226
|
+
})
|
|
227
|
+
|
|
171
228
|
it("is ready at once when the starting locale is bundled", async () => {
|
|
172
229
|
stubBrowser({ languages: ["en"] })
|
|
173
230
|
const loadLocale = vi.fn()
|
package/types/index.d.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface I18nOptions {
|
|
|
24
24
|
* catalogue is its own chunk.
|
|
25
25
|
*/
|
|
26
26
|
loadLocale?: (locale: string) => Promise<Resource[string]>
|
|
27
|
+
/**
|
|
28
|
+
* Whether the page comes rendered from a server, as with SSX: it then starts in the
|
|
29
|
+
* language of `<html lang>`, the one the server rendered it in, instead of detecting
|
|
30
|
+
* one. Defaults to `false`.
|
|
31
|
+
*/
|
|
32
|
+
isServerRendered?: boolean
|
|
27
33
|
/** The fallback, and the locale when nothing else matches. Defaults to the first locale. */
|
|
28
34
|
defaultLocale?: string
|
|
29
35
|
/** Where to remember the user's choice in localStorage. Omit to not remember it. */
|