@inglorious/i18n 1.1.0 → 1.2.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 +1 -1
- package/package.json +2 -2
- package/src/index.js +9 -4
- package/src/index.test.js +20 -1
- package/types/index.d.ts +11 -4
package/README.md
CHANGED
|
@@ -126,7 +126,7 @@ The starting locale is the saved choice, else the first of the device's language
|
|
|
126
126
|
|
|
127
127
|
### The `I18n` type
|
|
128
128
|
|
|
129
|
-
- `create(entity)`: sets `entity.locale`, `entity.locales` and `entity.languages`, and `<html lang>`.
|
|
129
|
+
- `create(entity)`: sets `entity.locale`, `entity.dir`, `entity.locales` and `entity.languages`, and `<html lang>` and `<html dir>`. `dir` is `rtl` for right-to-left languages such as Arabic or Hebrew; styles written with logical properties (`margin-inline-start`, `text-align: start`) mirror on their own.
|
|
130
130
|
- `localeChange(entity, locale)`: switches i18next, updates `entity.locale`, remembers the choice and sets `<html lang>`. Takes a locale or a picker's `{ value }`; unsupported locales are ignored. A locale not loaded yet is loaded first (`entity.loadingLocale`), and the switch follows in `localeLoadSuccess`.
|
|
131
131
|
- `userLocaleLoad(entity, { userId, locale })` and `userLocaleClear(entity)`: the signed-in user's locale. See [The user's language](#the-users-language).
|
|
132
132
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/i18n",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.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",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@inglorious/eslint-config": "1.1.2",
|
|
41
|
-
"@inglorious/store": "11.0.
|
|
41
|
+
"@inglorious/store": "11.0.4",
|
|
42
42
|
"prettier": "^3.9.6",
|
|
43
43
|
"vitest": "^5.0.0"
|
|
44
44
|
},
|
package/src/index.js
CHANGED
|
@@ -89,12 +89,13 @@ export function createI18n({
|
|
|
89
89
|
const I18n = {
|
|
90
90
|
create(entity) {
|
|
91
91
|
entity.locale = instance.language
|
|
92
|
+
entity.dir = instance.dir(entity.locale)
|
|
92
93
|
entity.locales = locales
|
|
93
94
|
entity.languages = languages
|
|
94
95
|
entity.loadingLocale = null
|
|
95
96
|
entity.userId = null
|
|
96
97
|
entity.isUnsaved = false
|
|
97
|
-
applyDocumentLanguage(entity.locale)
|
|
98
|
+
applyDocumentLanguage(entity.locale, entity.dir)
|
|
98
99
|
},
|
|
99
100
|
|
|
100
101
|
// The user's choice: a locale, or a picker's `{ value }`. With
|
|
@@ -174,8 +175,9 @@ export function createI18n({
|
|
|
174
175
|
function switchLocale(entity, locale) {
|
|
175
176
|
instance.changeLanguage(locale)
|
|
176
177
|
entity.locale = locale
|
|
178
|
+
entity.dir = instance.dir(locale)
|
|
177
179
|
saveLocale(storageKey, locale)
|
|
178
|
-
applyDocumentLanguage(locale)
|
|
180
|
+
applyDocumentLanguage(locale, entity.dir)
|
|
179
181
|
}
|
|
180
182
|
|
|
181
183
|
/**
|
|
@@ -361,8 +363,11 @@ function saveLocale(storageKey, locale) {
|
|
|
361
363
|
}
|
|
362
364
|
|
|
363
365
|
// Screen readers, hyphenation and the browser's own spell checking read the
|
|
364
|
-
// document language, so it follows the app's.
|
|
365
|
-
|
|
366
|
+
// document language, so it follows the app's. So does the direction: in
|
|
367
|
+
// Arabic, Hebrew or Persian the whole page reads right to left, and CSS that
|
|
368
|
+
// uses logical properties (`margin-inline-start`, `text-align: start`) follows.
|
|
369
|
+
function applyDocumentLanguage(locale, dir) {
|
|
366
370
|
if (typeof document === "undefined") return
|
|
367
371
|
document.documentElement.lang = locale
|
|
372
|
+
document.documentElement.dir = dir
|
|
368
373
|
}
|
package/src/index.test.js
CHANGED
|
@@ -31,7 +31,7 @@ function stubBrowser({ languages = [], saved = {} } = {}) {
|
|
|
31
31
|
store[key] = value
|
|
32
32
|
},
|
|
33
33
|
})
|
|
34
|
-
vi.stubGlobal("document", { documentElement: { lang: "" } })
|
|
34
|
+
vi.stubGlobal("document", { documentElement: { lang: "", dir: "" } })
|
|
35
35
|
return store
|
|
36
36
|
}
|
|
37
37
|
|
|
@@ -390,6 +390,25 @@ describe("saveUserLocale", () => {
|
|
|
390
390
|
})
|
|
391
391
|
})
|
|
392
392
|
|
|
393
|
+
describe("direction", () => {
|
|
394
|
+
it("sets the direction of the language, on the entity and the document", () => {
|
|
395
|
+
stubBrowser({ languages: ["en"] })
|
|
396
|
+
const { I18n } = createI18n({
|
|
397
|
+
resources: { ...resources, ar: { translation: { hello: "مرحبا" } } },
|
|
398
|
+
})
|
|
399
|
+
const entity = {}
|
|
400
|
+
I18n.create(entity)
|
|
401
|
+
|
|
402
|
+
expect(entity.dir).toBe("ltr")
|
|
403
|
+
expect(document.documentElement.dir).toBe("ltr")
|
|
404
|
+
|
|
405
|
+
I18n.localeChange(entity, "ar")
|
|
406
|
+
|
|
407
|
+
expect(entity.dir).toBe("rtl")
|
|
408
|
+
expect(document.documentElement.dir).toBe("rtl")
|
|
409
|
+
})
|
|
410
|
+
})
|
|
411
|
+
|
|
393
412
|
describe("helpers", () => {
|
|
394
413
|
it("labels data in the active language, and keeps the key", () => {
|
|
395
414
|
stubBrowser({ languages: ["en"] })
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { i18n, InitOptions, Resource, TOptions } from "i18next"
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A catalogue file as a glob gives it: the module, or a function loading it.
|
|
5
|
+
* As loose as Vite's own types for `import.meta.glob`, which leave the
|
|
6
|
+
* module `unknown`.
|
|
7
|
+
*/
|
|
8
|
+
export type CatalogueEntry = unknown
|
|
6
9
|
|
|
7
10
|
export interface I18nOptions {
|
|
8
11
|
/**
|
|
@@ -40,6 +43,8 @@ export interface I18nEntity {
|
|
|
40
43
|
type: string
|
|
41
44
|
/** The active locale. */
|
|
42
45
|
locale?: string
|
|
46
|
+
/** Its writing direction, also set on `<html dir>`. */
|
|
47
|
+
dir?: "ltr" | "rtl"
|
|
43
48
|
/** The supported locales, for a language picker. */
|
|
44
49
|
locales?: string[]
|
|
45
50
|
/** The supported locales named in themselves, as options for a picker. */
|
|
@@ -52,7 +57,9 @@ export interface I18nEntity {
|
|
|
52
57
|
isUnsaved?: boolean
|
|
53
58
|
}
|
|
54
59
|
|
|
55
|
-
|
|
60
|
+
// A type alias rather than an interface, so it fits option types that allow
|
|
61
|
+
// extra fields, such as Inglorious UI's `SelectOption`.
|
|
62
|
+
export type LanguageOption = {
|
|
56
63
|
value: string
|
|
57
64
|
/** The language in itself: "Italiano", "Français". */
|
|
58
65
|
label: string
|