@zairakai/js-i18n 2.1.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 +5 -146
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="https://gitlab.com/zairakai/npm-packages/js-i18n/-/raw/main/assets/
|
|
2
|
+
<img src="https://gitlab.com/zairakai/npm-packages/js-i18n/-/raw/main/assets/banner.png" alt="JS i18n: internationalization with Laravel-style translations, replacements and pluralization" width="720">
|
|
3
3
|
</p>
|
|
4
4
|
|
|
5
5
|
# JS i18n
|
|
@@ -21,10 +21,6 @@
|
|
|
21
21
|
|
|
22
22
|
Internationalization library for the web with Laravel-style translation, pluralization, dot-notation keys and multi-locale support. Translations are validated at runtime with `zod`.
|
|
23
23
|
|
|
24
|
-
**Documentation: [js-i18n-8f9736.gitlab.io][docs]**
|
|
25
|
-
|
|
26
|
-
---
|
|
27
|
-
|
|
28
24
|
## Features
|
|
29
25
|
|
|
30
26
|
- **Laravel-style `__()`** - familiar `:attribute` replacement and `{0} | {1} | [2,*] :count` pluralization
|
|
@@ -33,13 +29,9 @@ Internationalization library for the web with Laravel-style translation, plurali
|
|
|
33
29
|
- **Runtime validation** - translation data validated at runtime, invalid structures throw with a descriptive error message
|
|
34
30
|
- **Type-safe** - full TypeScript types for IDE support and static analysis
|
|
35
31
|
|
|
36
|
-
---
|
|
37
|
-
|
|
38
32
|
## Documentation
|
|
39
33
|
|
|
40
|
-
The documentation site has
|
|
41
|
-
|
|
42
|
-
---
|
|
34
|
+
The [documentation site][docs] has the [usage guide](https://js-i18n-8f9736.gitlab.io/guides/usage/) (in a bundler or in the browser, with examples for the translation, the pluralization, the locales at runtime and the errors) and the reference of every function and type, generated from the source with TypeDoc, for every released version (selector at the top right, `next` is `main`). Build it with `cd docs && npm ci && npm run dev`: see [CONTRIBUTING.md](CONTRIBUTING.md#documentation-site).
|
|
43
35
|
|
|
44
36
|
## Install
|
|
45
37
|
|
|
@@ -47,136 +39,7 @@ The documentation site has this guide and the reference of the API generated fro
|
|
|
47
39
|
npm install @zairakai/js-i18n
|
|
48
40
|
```
|
|
49
41
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
The library is made for the web: ESM and CommonJS builds, no Node.js built-ins, `"sideEffects": false`. A bundler keeps only what you import (the tests of this repository check it with esbuild): a project that imports `applyPluralization` alone gets a few hundred bytes. `createTranslator` validates the translations with `zod`, so it brings `zod` with it.
|
|
53
|
-
|
|
54
|
-
There is no minimum browser version.
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-
|
|
58
|
-
## Usage
|
|
59
|
-
|
|
60
|
-
### Basic translation
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
import { createI18n } from '@zairakai/js-i18n'
|
|
64
|
-
|
|
65
|
-
const i18n = createI18n(
|
|
66
|
-
{
|
|
67
|
-
en: {
|
|
68
|
-
welcome: 'Welcome, :name!',
|
|
69
|
-
auth: {
|
|
70
|
-
login: 'Sign in',
|
|
71
|
-
logout: 'Sign out',
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
fr: {
|
|
75
|
-
welcome: 'Bienvenue, :name !',
|
|
76
|
-
auth: {
|
|
77
|
-
login: 'Se connecter',
|
|
78
|
-
logout: 'Se déconnecter',
|
|
79
|
-
},
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
'en'
|
|
83
|
-
)
|
|
84
|
-
|
|
85
|
-
i18n.__('welcome', { name: 'Stan' }) // "Welcome, Stan!"
|
|
86
|
-
i18n.__('auth.login') // "Sign in"
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
<details>
|
|
90
|
-
<summary>Pluralization: singular, plural and ranges</summary>
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
import { createI18n } from '@zairakai/js-i18n'
|
|
94
|
-
|
|
95
|
-
const i18n = createI18n({
|
|
96
|
-
en: {
|
|
97
|
-
apples: '{0} No apples|{1} One apple|[2,*] :count apples',
|
|
98
|
-
files: '[0,1] :count file|[2,*] :count files',
|
|
99
|
-
},
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
i18n.__('apples', 0) // "No apples"
|
|
103
|
-
i18n.__('apples', 1) // "One apple"
|
|
104
|
-
i18n.__('apples', 5) // "5 apples"
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
</details>
|
|
108
|
-
|
|
109
|
-
<details>
|
|
110
|
-
<summary>Count and replacements together</summary>
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
i18n.__('files', 3, { extra: 'info' }) // "3 files"
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
</details>
|
|
117
|
-
|
|
118
|
-
<details>
|
|
119
|
-
<summary>Managing locales at runtime</summary>
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
i18n.setLocale('fr')
|
|
123
|
-
i18n.getLocale() // "fr"
|
|
124
|
-
i18n.getAvailableLocales() // ["en", "fr"]
|
|
125
|
-
|
|
126
|
-
i18n.setTranslations('de', { welcome: 'Willkommen, :name!' })
|
|
127
|
-
i18n.addTranslations('de', { auth: { login: 'Anmelden' } })
|
|
128
|
-
|
|
129
|
-
i18n.hasTranslation('auth.login', 'de') // true
|
|
130
|
-
i18n.hasTranslation('auth.logout', 'de') // false
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
</details>
|
|
134
|
-
|
|
135
|
-
<details>
|
|
136
|
-
<summary>Error handling: what throws and when</summary>
|
|
137
|
-
|
|
138
|
-
Translation data is validated at runtime. Invalid structures (e.g. loading from an API or external JSON) throw an `Error` with a descriptive message indicating exactly what is wrong.
|
|
139
|
-
|
|
140
|
-
```typescript
|
|
141
|
-
import { createI18n } from '@zairakai/js-i18n'
|
|
142
|
-
|
|
143
|
-
try {
|
|
144
|
-
const data = await fetchTranslations() // unknown external data
|
|
145
|
-
const i18n = createI18n(data, 'en')
|
|
146
|
-
} catch (e) {
|
|
147
|
-
// e.message describes the invalid field and its path
|
|
148
|
-
console.error('Invalid translation data:', (e as Error).message)
|
|
149
|
-
}
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
The same applies to `setTranslations()`, `addTranslations()`, and `translate()` when replacements are malformed.
|
|
153
|
-
|
|
154
|
-
</details>
|
|
155
|
-
|
|
156
|
-
<details>
|
|
157
|
-
<summary>Pluralization syntax: the forms a translation can take</summary>
|
|
158
|
-
|
|
159
|
-
The pluralization format follows Laravel's `trans_choice` pattern exactly.
|
|
160
|
-
|
|
161
|
-
| Pattern | Matches | Example |
|
|
162
|
-
| :--- | :--- | :--- |
|
|
163
|
-
| `{0}` | Exactly 0 | `{0} No items` |
|
|
164
|
-
| `{1}` | Exactly 1 | `{1} One item` |
|
|
165
|
-
| `{n}` | Exactly n | `{5} Exactly five` |
|
|
166
|
-
| `[2,*]` | 2 and above | `[2,*] :count items` |
|
|
167
|
-
| `[0,1]` | 0 or 1 | `[0,1] :count file` |
|
|
168
|
-
| `[1,10]` | Between 1 and 10 (inclusive) | `[1,10] a few items` |
|
|
169
|
-
|
|
170
|
-
Forms are separated by `|`. The `:count` placeholder is automatically replaced by the count value.
|
|
171
|
-
|
|
172
|
-
```typescript
|
|
173
|
-
// Full example
|
|
174
|
-
'{0} No apples|{1} One apple|[2,*] :count apples'
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
</details>
|
|
178
|
-
|
|
179
|
-
---
|
|
42
|
+
The library is made for the web (ESM and CommonJS builds, `"sideEffects": false`): the usage guide says how to import it, and what the browsers need.
|
|
180
43
|
|
|
181
44
|
## Development
|
|
182
45
|
|
|
@@ -190,17 +53,13 @@ make ci # run CI validation locally
|
|
|
190
53
|
|
|
191
54
|
See [CONTRIBUTING.md](./CONTRIBUTING.md) for the full development workflow.
|
|
192
55
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
## Getting Help
|
|
56
|
+
## Getting help
|
|
196
57
|
|
|
197
58
|
[![License][license-badge]][license]
|
|
198
59
|
[![Security Policy][security-badge]][security]
|
|
199
60
|
[![Issues][issues-badge]][issues]
|
|
200
61
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
<!-- Reference Links -->
|
|
62
|
+
Made by [Zairakai][ecosystem].
|
|
204
63
|
|
|
205
64
|
## Statistics
|
|
206
65
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zairakai/js-i18n",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Internationalization library for the web with Laravel-style translation, pluralization, dot-notation keys and multi-locale support, validated at runtime with zod",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -95,16 +95,15 @@
|
|
|
95
95
|
"zod": "^4.6.7"
|
|
96
96
|
},
|
|
97
97
|
"devDependencies": {
|
|
98
|
-
"@types/node": "^26.6.
|
|
99
|
-
"@vitest/coverage-v8": "^5.1.
|
|
100
|
-
"@zairakai/js-dev-tools": "^4.
|
|
98
|
+
"@types/node": "^26.6.11",
|
|
99
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
100
|
+
"@zairakai/js-dev-tools": "^4.7.0",
|
|
101
101
|
"esbuild": "^0.28.2",
|
|
102
|
-
"knip": "^6.
|
|
102
|
+
"knip": "^6.49.0",
|
|
103
103
|
"sort-package-json": "^4.1.0",
|
|
104
104
|
"tsup": "^9.0.0",
|
|
105
|
-
"typedoc": "^0.28.20",
|
|
106
105
|
"typescript": "^6.0.3",
|
|
107
|
-
"vitest": "^5.1.
|
|
106
|
+
"vitest": "^5.1.2"
|
|
108
107
|
},
|
|
109
108
|
"publishConfig": {
|
|
110
109
|
"access": "public"
|