@zairakai/js-i18n 1.0.16 → 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 +28 -50
- package/dist/index.cjs +30 -20
- package/dist/index.d.cts +0 -11
- package/dist/index.d.ts +0 -11
- package/dist/index.js +30 -20
- package/package.json +25 -15
- package/src/index.ts +4 -1
- package/src/pluralization.ts +39 -14
- package/src/schemas.ts +17 -9
- package/src/translator.ts +30 -11
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://gitlab.com/zairakai/npm-packages/js-i18n/-/raw/main/assets/icon.png" alt="JS i18n" width="160">
|
|
3
|
+
</p>
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# JS i18n
|
|
6
6
|
|
|
7
7
|
[![Main][pipeline-main-badge]][pipeline-main-link]
|
|
8
8
|
[![Develop][pipeline-develop-badge]][pipeline-develop-link]
|
|
@@ -27,11 +27,11 @@ Internationalization library for the web with Laravel-style translation, plurali
|
|
|
27
27
|
|
|
28
28
|
## Features
|
|
29
29
|
|
|
30
|
-
- **Laravel-style `__()`**
|
|
31
|
-
- **Dot notation keys**
|
|
32
|
-
- **Multi-locale**
|
|
33
|
-
- **Runtime validation**
|
|
34
|
-
- **Type-safe**
|
|
30
|
+
- **Laravel-style `__()`** - familiar `:attribute` replacement and `{0} | {1} | [2,*] :count` pluralization
|
|
31
|
+
- **Dot notation keys** - navigate nested translation objects with `auth.login.title`
|
|
32
|
+
- **Multi-locale** - manage any number of locales, switch at runtime
|
|
33
|
+
- **Runtime validation** - translation data validated at runtime, invalid structures throw with a descriptive error message
|
|
34
|
+
- **Type-safe** - full TypeScript types for IDE support and static analysis
|
|
35
35
|
|
|
36
36
|
---
|
|
37
37
|
|
|
@@ -86,7 +86,8 @@ i18n.__('welcome', { name: 'Stan' }) // "Welcome, Stan!"
|
|
|
86
86
|
i18n.__('auth.login') // "Sign in"
|
|
87
87
|
```
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
<details>
|
|
90
|
+
<summary>Pluralization: singular, plural and ranges</summary>
|
|
90
91
|
|
|
91
92
|
```typescript
|
|
92
93
|
import { createI18n } from '@zairakai/js-i18n'
|
|
@@ -103,13 +104,19 @@ i18n.__('apples', 1) // "One apple"
|
|
|
103
104
|
i18n.__('apples', 5) // "5 apples"
|
|
104
105
|
```
|
|
105
106
|
|
|
106
|
-
|
|
107
|
+
</details>
|
|
108
|
+
|
|
109
|
+
<details>
|
|
110
|
+
<summary>Count and replacements together</summary>
|
|
107
111
|
|
|
108
112
|
```typescript
|
|
109
113
|
i18n.__('files', 3, { extra: 'info' }) // "3 files"
|
|
110
114
|
```
|
|
111
115
|
|
|
112
|
-
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
<details>
|
|
119
|
+
<summary>Managing locales at runtime</summary>
|
|
113
120
|
|
|
114
121
|
```typescript
|
|
115
122
|
i18n.setLocale('fr')
|
|
@@ -123,7 +130,10 @@ i18n.hasTranslation('auth.login', 'de') // true
|
|
|
123
130
|
i18n.hasTranslation('auth.logout', 'de') // false
|
|
124
131
|
```
|
|
125
132
|
|
|
126
|
-
|
|
133
|
+
</details>
|
|
134
|
+
|
|
135
|
+
<details>
|
|
136
|
+
<summary>Error handling: what throws and when</summary>
|
|
127
137
|
|
|
128
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.
|
|
129
139
|
|
|
@@ -141,9 +151,10 @@ try {
|
|
|
141
151
|
|
|
142
152
|
The same applies to `setTranslations()`, `addTranslations()`, and `translate()` when replacements are malformed.
|
|
143
153
|
|
|
144
|
-
|
|
154
|
+
</details>
|
|
145
155
|
|
|
146
|
-
|
|
156
|
+
<details>
|
|
157
|
+
<summary>Pluralization syntax: the forms a translation can take</summary>
|
|
147
158
|
|
|
148
159
|
The pluralization format follows Laravel's `trans_choice` pattern exactly.
|
|
149
160
|
|
|
@@ -163,40 +174,7 @@ Forms are separated by `|`. The `:count` placeholder is automatically replaced b
|
|
|
163
174
|
'{0} No apples|{1} One apple|[2,*] :count apples'
|
|
164
175
|
```
|
|
165
176
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
## API Reference
|
|
169
|
-
|
|
170
|
-
### Factory functions
|
|
171
|
-
|
|
172
|
-
| Function | Description |
|
|
173
|
-
| :--- | :--- |
|
|
174
|
-
| `createI18n(translations?, locale?)` | Creates a translator instance (alias, preferred). |
|
|
175
|
-
| `createTranslator(translations?, locale?)` | Creates a translator instance (explicit name). |
|
|
176
|
-
|
|
177
|
-
Both return a `Translator` instance. Default locale is `'en'`.
|
|
178
|
-
|
|
179
|
-
### Translator methods
|
|
180
|
-
|
|
181
|
-
| Method | Description |
|
|
182
|
-
| :--- | :--- |
|
|
183
|
-
| `__(path, replacements?)` | Translate a key with optional replacements. |
|
|
184
|
-
| `__(path, count, replacements?)` | Translate with pluralization count. |
|
|
185
|
-
| `translate(path, replacements?)` | Alias for `__`. |
|
|
186
|
-
| `translate(path, count, replacements?)` | Alias for `__` with count. |
|
|
187
|
-
| `setLocale(locale)` | Set the active locale. |
|
|
188
|
-
| `getLocale()` | Get the active locale. |
|
|
189
|
-
| `setTranslations(locale, translations)` | Replace all translations for a locale. |
|
|
190
|
-
| `addTranslations(locale, translations)` | Merge translations into an existing locale. |
|
|
191
|
-
| `hasTranslation(path, locale?)` | Check if a translation key exists (uses active locale). |
|
|
192
|
-
| `getAvailableLocales()` | Return all registered locale codes. |
|
|
193
|
-
|
|
194
|
-
### Pluralization utilities
|
|
195
|
-
|
|
196
|
-
| Function | Description |
|
|
197
|
-
| :--- | :--- |
|
|
198
|
-
| `applyPluralization(value, count)` | Apply pluralization to a raw string with count. |
|
|
199
|
-
| `selectPluralForm(forms, count)` | Select the correct form from a pre-split array. |
|
|
177
|
+
</details>
|
|
200
178
|
|
|
201
179
|
---
|
|
202
180
|
|
|
@@ -242,7 +220,7 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the full development workflow.
|
|
|
242
220
|
[license]: ./LICENSE
|
|
243
221
|
[security-badge]: https://img.shields.io/badge/security-scanned-green.svg
|
|
244
222
|
[security]: ./SECURITY.md
|
|
245
|
-
[issues-badge]: https://img.shields.io/gitlab/issues/open-raw/zairakai%2Fnpm-packages%
|
|
223
|
+
[issues-badge]: https://img.shields.io/gitlab/issues/open-raw/zairakai%2Fnpm-packages%2Fjs-i18n?logo=gitlab&label=Issues
|
|
246
224
|
[issues]: https://gitlab.com/zairakai/npm-packages/js-i18n/-/issues
|
|
247
225
|
[node-badge]: https://img.shields.io/badge/node.js-%3E%3D24-green.svg?logo=node.js
|
|
248
226
|
[node]: https://nodejs.org
|
package/dist/index.cjs
CHANGED
|
@@ -28,22 +28,26 @@ __export(src_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(src_exports);
|
|
29
29
|
|
|
30
30
|
// src/pluralization.ts
|
|
31
|
+
var group = (match, index) => match[index] ?? "";
|
|
32
|
+
var first = (part) => part?.trim() ?? "";
|
|
31
33
|
var selectPluralForm = (message, count) => {
|
|
32
34
|
const parts = message.split("|");
|
|
33
35
|
for (const part of parts) {
|
|
34
36
|
const trimmedPart = part.trim();
|
|
35
37
|
const exactMatch2 = trimmedPart.match(/^{\s*(\d+)\s*}\s*(.*)$/);
|
|
36
|
-
const rangeMatch2 = trimmedPart.match(
|
|
38
|
+
const rangeMatch2 = trimmedPart.match(
|
|
39
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/
|
|
40
|
+
);
|
|
37
41
|
if (exactMatch2) {
|
|
38
|
-
const exactNumber = parseInt(exactMatch2
|
|
42
|
+
const exactNumber = parseInt(group(exactMatch2, 1), 10);
|
|
39
43
|
if (count === exactNumber) {
|
|
40
|
-
return exactMatch2
|
|
44
|
+
return group(exactMatch2, 2).trim();
|
|
41
45
|
}
|
|
42
46
|
} else if (rangeMatch2) {
|
|
43
|
-
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(rangeMatch2
|
|
44
|
-
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(rangeMatch2
|
|
47
|
+
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(group(rangeMatch2, 1), 10);
|
|
48
|
+
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(group(rangeMatch2, 2), 10);
|
|
45
49
|
if (count >= min && count <= max) {
|
|
46
|
-
return rangeMatch2
|
|
50
|
+
return group(rangeMatch2, 3).trim();
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
53
|
}
|
|
@@ -52,17 +56,19 @@ var selectPluralForm = (message, count) => {
|
|
|
52
56
|
return !trimmed.match(/^[{\[]/);
|
|
53
57
|
});
|
|
54
58
|
if (simpleParts.length >= 2) {
|
|
55
|
-
return count === 1 ?
|
|
59
|
+
return first(simpleParts[count === 1 ? 0 : 1]);
|
|
56
60
|
} else if (simpleParts.length === 1) {
|
|
57
|
-
return simpleParts[0]
|
|
61
|
+
return first(simpleParts[0]);
|
|
58
62
|
}
|
|
59
|
-
const firstPart = parts[0]
|
|
63
|
+
const firstPart = first(parts[0]);
|
|
60
64
|
const exactMatch = firstPart.match(/^{\s*(\d+)\s*}\s*(.*)$/);
|
|
61
|
-
const rangeMatch = firstPart.match(
|
|
65
|
+
const rangeMatch = firstPart.match(
|
|
66
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/
|
|
67
|
+
);
|
|
62
68
|
if (exactMatch) {
|
|
63
|
-
return exactMatch
|
|
69
|
+
return group(exactMatch, 2).trim();
|
|
64
70
|
} else if (rangeMatch) {
|
|
65
|
-
return rangeMatch
|
|
71
|
+
return group(rangeMatch, 3).trim();
|
|
66
72
|
}
|
|
67
73
|
return firstPart;
|
|
68
74
|
};
|
|
@@ -82,12 +88,15 @@ var TranslationObjectSchema = import_zod.z.record(
|
|
|
82
88
|
import_zod.z.string(),
|
|
83
89
|
TranslationValueSchema
|
|
84
90
|
);
|
|
85
|
-
var TranslationDataSchema = import_zod.z.record(
|
|
86
|
-
var ReplacementParametersSchema = import_zod.z.record(
|
|
91
|
+
var TranslationDataSchema = import_zod.z.record(
|
|
87
92
|
import_zod.z.string(),
|
|
88
|
-
|
|
93
|
+
TranslationObjectSchema
|
|
89
94
|
);
|
|
90
|
-
var
|
|
95
|
+
var ReplacementParametersSchema = import_zod.z.record(import_zod.z.string(), import_zod.z.union([import_zod.z.string(), import_zod.z.number()]));
|
|
96
|
+
var CountOrReplacementsSchema = import_zod.z.union([
|
|
97
|
+
import_zod.z.number(),
|
|
98
|
+
ReplacementParametersSchema
|
|
99
|
+
]);
|
|
91
100
|
|
|
92
101
|
// src/translator.ts
|
|
93
102
|
var createTranslator = (translations = {}, locale = "en") => {
|
|
@@ -136,10 +145,11 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
136
145
|
};
|
|
137
146
|
const addTranslations = (locale2, translations2) => {
|
|
138
147
|
const validated = TranslationObjectSchema.parse(translations2);
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
currentTranslations[locale2] ??= {};
|
|
149
|
+
currentTranslations[locale2] = {
|
|
150
|
+
...currentTranslations[locale2],
|
|
151
|
+
...validated
|
|
152
|
+
};
|
|
143
153
|
};
|
|
144
154
|
const hasTranslation = (path, locale2 = null) => {
|
|
145
155
|
const checkLocale = locale2 ?? currentLocale;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,17 +2,6 @@
|
|
|
2
2
|
* Pluralization utilities for Laravel-style translation strings
|
|
3
3
|
* Handles complex pluralization rules with conditions and ranges
|
|
4
4
|
*/
|
|
5
|
-
/**
|
|
6
|
-
* Selects the appropriate plural form based on count
|
|
7
|
-
* Supports Laravel formats:
|
|
8
|
-
* - Simple: "item|items"
|
|
9
|
-
* - With conditions: "{0} none|{1} one|[2,*] many"
|
|
10
|
-
* - With ranges: "[1,19] some|[20,*] many"
|
|
11
|
-
*
|
|
12
|
-
* @param message - Translation string with plural forms
|
|
13
|
-
* @param count - Count to determine plural form
|
|
14
|
-
* @returns Selected plural form
|
|
15
|
-
*/
|
|
16
5
|
declare const selectPluralForm: (message: string, count: number) => string;
|
|
17
6
|
/**
|
|
18
7
|
* Apply pluralization rules to a translation
|
package/dist/index.d.ts
CHANGED
|
@@ -2,17 +2,6 @@
|
|
|
2
2
|
* Pluralization utilities for Laravel-style translation strings
|
|
3
3
|
* Handles complex pluralization rules with conditions and ranges
|
|
4
4
|
*/
|
|
5
|
-
/**
|
|
6
|
-
* Selects the appropriate plural form based on count
|
|
7
|
-
* Supports Laravel formats:
|
|
8
|
-
* - Simple: "item|items"
|
|
9
|
-
* - With conditions: "{0} none|{1} one|[2,*] many"
|
|
10
|
-
* - With ranges: "[1,19] some|[20,*] many"
|
|
11
|
-
*
|
|
12
|
-
* @param message - Translation string with plural forms
|
|
13
|
-
* @param count - Count to determine plural form
|
|
14
|
-
* @returns Selected plural form
|
|
15
|
-
*/
|
|
16
5
|
declare const selectPluralForm: (message: string, count: number) => string;
|
|
17
6
|
/**
|
|
18
7
|
* Apply pluralization rules to a translation
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
// src/pluralization.ts
|
|
2
|
+
var group = (match, index) => match[index] ?? "";
|
|
3
|
+
var first = (part) => part?.trim() ?? "";
|
|
2
4
|
var selectPluralForm = (message, count) => {
|
|
3
5
|
const parts = message.split("|");
|
|
4
6
|
for (const part of parts) {
|
|
5
7
|
const trimmedPart = part.trim();
|
|
6
8
|
const exactMatch2 = trimmedPart.match(/^{\s*(\d+)\s*}\s*(.*)$/);
|
|
7
|
-
const rangeMatch2 = trimmedPart.match(
|
|
9
|
+
const rangeMatch2 = trimmedPart.match(
|
|
10
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/
|
|
11
|
+
);
|
|
8
12
|
if (exactMatch2) {
|
|
9
|
-
const exactNumber = parseInt(exactMatch2
|
|
13
|
+
const exactNumber = parseInt(group(exactMatch2, 1), 10);
|
|
10
14
|
if (count === exactNumber) {
|
|
11
|
-
return exactMatch2
|
|
15
|
+
return group(exactMatch2, 2).trim();
|
|
12
16
|
}
|
|
13
17
|
} else if (rangeMatch2) {
|
|
14
|
-
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(rangeMatch2
|
|
15
|
-
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(rangeMatch2
|
|
18
|
+
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(group(rangeMatch2, 1), 10);
|
|
19
|
+
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(group(rangeMatch2, 2), 10);
|
|
16
20
|
if (count >= min && count <= max) {
|
|
17
|
-
return rangeMatch2
|
|
21
|
+
return group(rangeMatch2, 3).trim();
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
24
|
}
|
|
@@ -23,17 +27,19 @@ var selectPluralForm = (message, count) => {
|
|
|
23
27
|
return !trimmed.match(/^[{\[]/);
|
|
24
28
|
});
|
|
25
29
|
if (simpleParts.length >= 2) {
|
|
26
|
-
return count === 1 ?
|
|
30
|
+
return first(simpleParts[count === 1 ? 0 : 1]);
|
|
27
31
|
} else if (simpleParts.length === 1) {
|
|
28
|
-
return simpleParts[0]
|
|
32
|
+
return first(simpleParts[0]);
|
|
29
33
|
}
|
|
30
|
-
const firstPart = parts[0]
|
|
34
|
+
const firstPart = first(parts[0]);
|
|
31
35
|
const exactMatch = firstPart.match(/^{\s*(\d+)\s*}\s*(.*)$/);
|
|
32
|
-
const rangeMatch = firstPart.match(
|
|
36
|
+
const rangeMatch = firstPart.match(
|
|
37
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/
|
|
38
|
+
);
|
|
33
39
|
if (exactMatch) {
|
|
34
|
-
return exactMatch
|
|
40
|
+
return group(exactMatch, 2).trim();
|
|
35
41
|
} else if (rangeMatch) {
|
|
36
|
-
return rangeMatch
|
|
42
|
+
return group(rangeMatch, 3).trim();
|
|
37
43
|
}
|
|
38
44
|
return firstPart;
|
|
39
45
|
};
|
|
@@ -53,12 +59,15 @@ var TranslationObjectSchema = z.record(
|
|
|
53
59
|
z.string(),
|
|
54
60
|
TranslationValueSchema
|
|
55
61
|
);
|
|
56
|
-
var TranslationDataSchema = z.record(
|
|
57
|
-
var ReplacementParametersSchema = z.record(
|
|
62
|
+
var TranslationDataSchema = z.record(
|
|
58
63
|
z.string(),
|
|
59
|
-
|
|
64
|
+
TranslationObjectSchema
|
|
60
65
|
);
|
|
61
|
-
var
|
|
66
|
+
var ReplacementParametersSchema = z.record(z.string(), z.union([z.string(), z.number()]));
|
|
67
|
+
var CountOrReplacementsSchema = z.union([
|
|
68
|
+
z.number(),
|
|
69
|
+
ReplacementParametersSchema
|
|
70
|
+
]);
|
|
62
71
|
|
|
63
72
|
// src/translator.ts
|
|
64
73
|
var createTranslator = (translations = {}, locale = "en") => {
|
|
@@ -107,10 +116,11 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
107
116
|
};
|
|
108
117
|
const addTranslations = (locale2, translations2) => {
|
|
109
118
|
const validated = TranslationObjectSchema.parse(translations2);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
119
|
+
currentTranslations[locale2] ??= {};
|
|
120
|
+
currentTranslations[locale2] = {
|
|
121
|
+
...currentTranslations[locale2],
|
|
122
|
+
...validated
|
|
123
|
+
};
|
|
114
124
|
};
|
|
115
125
|
const hasTranslation = (path, locale2 = null) => {
|
|
116
126
|
const checkLocale = locale2 ?? currentLocale;
|
package/package.json
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zairakai/js-i18n",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
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",
|
|
7
7
|
"internationalization",
|
|
8
8
|
"localization",
|
|
9
|
-
"pluralization",
|
|
10
9
|
"translations",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
10
|
+
"pluralization",
|
|
11
|
+
"laravel",
|
|
12
|
+
"dot-notation",
|
|
13
|
+
"zod",
|
|
14
|
+
"typescript",
|
|
15
|
+
"browser"
|
|
14
16
|
],
|
|
15
17
|
"homepage": "https://js-i18n-8f9736.gitlab.io",
|
|
16
18
|
"bugs": {
|
|
@@ -40,6 +42,7 @@
|
|
|
40
42
|
"role": "Maintainer"
|
|
41
43
|
}
|
|
42
44
|
],
|
|
45
|
+
"sideEffects": false,
|
|
43
46
|
"type": "module",
|
|
44
47
|
"exports": {
|
|
45
48
|
".": {
|
|
@@ -74,8 +77,19 @@
|
|
|
74
77
|
"validate": "make quality"
|
|
75
78
|
},
|
|
76
79
|
"overrides": {
|
|
77
|
-
"
|
|
78
|
-
"
|
|
80
|
+
"brace-expansion": "5.0.12",
|
|
81
|
+
"colord": "2.9.7",
|
|
82
|
+
"esbuild": "^0.28.2",
|
|
83
|
+
"fast-uri": "3.1.9",
|
|
84
|
+
"js-yaml": {
|
|
85
|
+
".": "4.3.2"
|
|
86
|
+
},
|
|
87
|
+
"katex": "0.18.11",
|
|
88
|
+
"markdown-it": "14.3.3",
|
|
89
|
+
"minimatch": "11.0.6",
|
|
90
|
+
"nanoid": "3.3.20",
|
|
91
|
+
"re2js": "2.9.1",
|
|
92
|
+
"smol-toml": "1.9.0"
|
|
79
93
|
},
|
|
80
94
|
"dependencies": {
|
|
81
95
|
"zod": "^4.6.7"
|
|
@@ -83,20 +97,16 @@
|
|
|
83
97
|
"devDependencies": {
|
|
84
98
|
"@types/node": "^26.6.6",
|
|
85
99
|
"@vitest/coverage-v8": "^5.1.0",
|
|
86
|
-
"@zairakai/js-dev-tools": "^
|
|
87
|
-
"esbuild": "^0.28.
|
|
88
|
-
"knip": "^6.
|
|
100
|
+
"@zairakai/js-dev-tools": "^4.1.0",
|
|
101
|
+
"esbuild": "^0.28.2",
|
|
102
|
+
"knip": "^6.45.1",
|
|
89
103
|
"sort-package-json": "^4.1.0",
|
|
90
104
|
"tsup": "^9.0.0",
|
|
91
105
|
"typedoc": "^0.28.20",
|
|
92
106
|
"typescript": "^6.0.3",
|
|
93
107
|
"vitest": "^5.1.0"
|
|
94
108
|
},
|
|
95
|
-
"engines": {
|
|
96
|
-
"node": ">=24.0.0"
|
|
97
|
-
},
|
|
98
109
|
"publishConfig": {
|
|
99
110
|
"access": "public"
|
|
100
|
-
}
|
|
101
|
-
"sideEffects": false
|
|
111
|
+
}
|
|
102
112
|
}
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,9 @@ import type { TranslationData, Translator } from './types.js'
|
|
|
22
22
|
* @param locale - Default locale
|
|
23
23
|
* @returns Translator instance with __ function
|
|
24
24
|
*/
|
|
25
|
-
export const createI18n = (
|
|
25
|
+
export const createI18n = (
|
|
26
|
+
translations: TranslationData = {},
|
|
27
|
+
locale = 'en',
|
|
28
|
+
): Translator => {
|
|
26
29
|
return createTranslator(translations, locale)
|
|
27
30
|
}
|
package/src/pluralization.ts
CHANGED
|
@@ -14,6 +14,18 @@
|
|
|
14
14
|
* @param count - Count to determine plural form
|
|
15
15
|
* @returns Selected plural form
|
|
16
16
|
*/
|
|
17
|
+
/**
|
|
18
|
+
* Group of a regular expression match. The groups of the patterns used here
|
|
19
|
+
* always take part in the match: the empty string is only for the type.
|
|
20
|
+
*/
|
|
21
|
+
const group = (match: RegExpMatchArray, index: number): string =>
|
|
22
|
+
match[index] ?? ''
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Trimmed text of a part, empty when there is none
|
|
26
|
+
*/
|
|
27
|
+
const first = (part: string | undefined): string => part?.trim() ?? ''
|
|
28
|
+
|
|
17
29
|
export const selectPluralForm = (message: string, count: number): string => {
|
|
18
30
|
const parts = message.split('|')
|
|
19
31
|
|
|
@@ -24,21 +36,25 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
24
36
|
// Look for exact match conditions {n}
|
|
25
37
|
const exactMatch = trimmedPart.match(/^{\s*(\d+)\s*}\s*(.*)$/)
|
|
26
38
|
// Look for range conditions [n,m]
|
|
27
|
-
const rangeMatch = trimmedPart.match(
|
|
39
|
+
const rangeMatch = trimmedPart.match(
|
|
40
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/,
|
|
41
|
+
)
|
|
28
42
|
|
|
29
43
|
if (exactMatch) {
|
|
30
44
|
// Exact number match {5}
|
|
31
|
-
const exactNumber = parseInt(exactMatch
|
|
45
|
+
const exactNumber = parseInt(group(exactMatch, 1), 10)
|
|
32
46
|
if (count === exactNumber) {
|
|
33
|
-
return exactMatch
|
|
47
|
+
return group(exactMatch, 2).trim()
|
|
34
48
|
}
|
|
35
49
|
} else if (rangeMatch) {
|
|
36
50
|
// Range match [1,5] or [20,*]
|
|
37
|
-
const min =
|
|
38
|
-
|
|
51
|
+
const min =
|
|
52
|
+
rangeMatch[1] === '*' ? -Infinity : parseInt(group(rangeMatch, 1), 10)
|
|
53
|
+
const max =
|
|
54
|
+
rangeMatch[2] === '*' ? Infinity : parseInt(group(rangeMatch, 2), 10)
|
|
39
55
|
|
|
40
56
|
if (count >= min && count <= max) {
|
|
41
|
-
return rangeMatch
|
|
57
|
+
return group(rangeMatch, 3).trim()
|
|
42
58
|
}
|
|
43
59
|
}
|
|
44
60
|
}
|
|
@@ -52,22 +68,24 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
52
68
|
|
|
53
69
|
if (simpleParts.length >= 2) {
|
|
54
70
|
// English pluralization: only 1 = singular, others (including 0) = plural
|
|
55
|
-
return count === 1 ?
|
|
71
|
+
return first(simpleParts[count === 1 ? 0 : 1])
|
|
56
72
|
} else if (simpleParts.length === 1) {
|
|
57
|
-
return simpleParts[0]
|
|
73
|
+
return first(simpleParts[0])
|
|
58
74
|
}
|
|
59
75
|
|
|
60
76
|
// Last resort: return first part, extracting content from patterns
|
|
61
|
-
const firstPart = parts[0]
|
|
77
|
+
const firstPart = first(parts[0])
|
|
62
78
|
|
|
63
79
|
// Try to extract content from patterns
|
|
64
80
|
const exactMatch = firstPart.match(/^{\s*(\d+)\s*}\s*(.*)$/)
|
|
65
|
-
const rangeMatch = firstPart.match(
|
|
81
|
+
const rangeMatch = firstPart.match(
|
|
82
|
+
/^\[\s*(\d+|\*)\s*,\s*(\d+|\*)\s*\]\s*(.*)$/,
|
|
83
|
+
)
|
|
66
84
|
|
|
67
85
|
if (exactMatch) {
|
|
68
|
-
return exactMatch
|
|
86
|
+
return group(exactMatch, 2).trim()
|
|
69
87
|
} else if (rangeMatch) {
|
|
70
|
-
return rangeMatch
|
|
88
|
+
return group(rangeMatch, 3).trim()
|
|
71
89
|
}
|
|
72
90
|
|
|
73
91
|
return firstPart
|
|
@@ -79,8 +97,15 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
79
97
|
* @param count - Count for pluralization
|
|
80
98
|
* @returns Pluralized translation
|
|
81
99
|
*/
|
|
82
|
-
export const applyPluralization = (
|
|
83
|
-
|
|
100
|
+
export const applyPluralization = (
|
|
101
|
+
translation: string,
|
|
102
|
+
count: number | null,
|
|
103
|
+
): string => {
|
|
104
|
+
if (
|
|
105
|
+
typeof translation === 'string' &&
|
|
106
|
+
translation.includes('|') &&
|
|
107
|
+
count !== null
|
|
108
|
+
) {
|
|
84
109
|
return selectPluralForm(translation, count)
|
|
85
110
|
}
|
|
86
111
|
return translation
|
package/src/schemas.ts
CHANGED
|
@@ -3,13 +3,17 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { z } from 'zod'
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
ReplacementParameters,
|
|
8
|
+
TranslationData,
|
|
9
|
+
TranslationObject,
|
|
10
|
+
} from './types.js'
|
|
7
11
|
|
|
8
12
|
/**
|
|
9
13
|
* Recursive schema for a translation value (string or nested TranslationObject)
|
|
10
14
|
*/
|
|
11
|
-
const TranslationValueSchema: z.ZodType<string | TranslationObject> = z.lazy(
|
|
12
|
-
z.union([z.string(), z.record(z.string(), TranslationValueSchema)]),
|
|
15
|
+
const TranslationValueSchema: z.ZodType<string | TranslationObject> = z.lazy(
|
|
16
|
+
() => z.union([z.string(), z.record(z.string(), TranslationValueSchema)]),
|
|
13
17
|
) as z.ZodType<string | TranslationObject>
|
|
14
18
|
|
|
15
19
|
/**
|
|
@@ -23,18 +27,22 @@ export const TranslationObjectSchema: z.ZodType<TranslationObject> = z.record(
|
|
|
23
27
|
/**
|
|
24
28
|
* Schema for the full translation data map (locale → TranslationObject)
|
|
25
29
|
*/
|
|
26
|
-
export const TranslationDataSchema: z.ZodType<TranslationData> = z.record(
|
|
30
|
+
export const TranslationDataSchema: z.ZodType<TranslationData> = z.record(
|
|
31
|
+
z.string(),
|
|
32
|
+
TranslationObjectSchema,
|
|
33
|
+
)
|
|
27
34
|
|
|
28
35
|
/**
|
|
29
36
|
* Schema for placeholder replacement parameters
|
|
30
37
|
*/
|
|
31
|
-
export const ReplacementParametersSchema: z.ZodType<ReplacementParameters> =
|
|
32
|
-
z.string(),
|
|
33
|
-
z.union([z.string(), z.number()]),
|
|
34
|
-
)
|
|
38
|
+
export const ReplacementParametersSchema: z.ZodType<ReplacementParameters> =
|
|
39
|
+
z.record(z.string(), z.union([z.string(), z.number()]))
|
|
35
40
|
|
|
36
41
|
/**
|
|
37
42
|
* Schema for the second parameter of translate():
|
|
38
43
|
* either a count (number) for pluralization or a replacements object
|
|
39
44
|
*/
|
|
40
|
-
export const CountOrReplacementsSchema = z.union([
|
|
45
|
+
export const CountOrReplacementsSchema = z.union([
|
|
46
|
+
z.number(),
|
|
47
|
+
ReplacementParametersSchema,
|
|
48
|
+
])
|
package/src/translator.ts
CHANGED
|
@@ -23,7 +23,10 @@ import type {
|
|
|
23
23
|
* @param locale - Current locale
|
|
24
24
|
* @returns Translator instance
|
|
25
25
|
*/
|
|
26
|
-
export const createTranslator = (
|
|
26
|
+
export const createTranslator = (
|
|
27
|
+
translations: TranslationData = {},
|
|
28
|
+
locale = 'en',
|
|
29
|
+
): Translator => {
|
|
27
30
|
const currentTranslations = TranslationDataSchema.parse(translations)
|
|
28
31
|
let currentLocale = locale
|
|
29
32
|
|
|
@@ -61,7 +64,8 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
61
64
|
|
|
62
65
|
// Navigate through translation object using dot notation
|
|
63
66
|
const segments = path.split('.')
|
|
64
|
-
let translation: string | TranslationObject =
|
|
67
|
+
let translation: string | TranslationObject | undefined =
|
|
68
|
+
currentTranslations[currentLocale]
|
|
65
69
|
|
|
66
70
|
for (const segment of segments) {
|
|
67
71
|
if (
|
|
@@ -82,10 +86,14 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
82
86
|
// Replace placeholders (:key and {key} formats)
|
|
83
87
|
const result = pluralizedTranslation
|
|
84
88
|
.replace(/:(\w+)/g, (match, key) => {
|
|
85
|
-
return Object.prototype.hasOwnProperty.call(actualReplacements, key)
|
|
89
|
+
return Object.prototype.hasOwnProperty.call(actualReplacements, key)
|
|
90
|
+
? String(actualReplacements[key])
|
|
91
|
+
: match
|
|
86
92
|
})
|
|
87
93
|
.replace(/{(\w+)}/g, (match, key) => {
|
|
88
|
-
return Object.prototype.hasOwnProperty.call(actualReplacements, key)
|
|
94
|
+
return Object.prototype.hasOwnProperty.call(actualReplacements, key)
|
|
95
|
+
? String(actualReplacements[key])
|
|
96
|
+
: match
|
|
89
97
|
})
|
|
90
98
|
|
|
91
99
|
return result
|
|
@@ -114,7 +122,10 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
114
122
|
* @param locale - Locale code
|
|
115
123
|
* @param translations - Translation object
|
|
116
124
|
*/
|
|
117
|
-
const setTranslations = (
|
|
125
|
+
const setTranslations = (
|
|
126
|
+
locale: string,
|
|
127
|
+
translations: TranslationObject,
|
|
128
|
+
): void => {
|
|
118
129
|
currentTranslations[locale] = TranslationObjectSchema.parse(translations)
|
|
119
130
|
}
|
|
120
131
|
|
|
@@ -123,12 +134,16 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
123
134
|
* @param locale - Locale code
|
|
124
135
|
* @param translations - Translation object to merge
|
|
125
136
|
*/
|
|
126
|
-
const addTranslations = (
|
|
137
|
+
const addTranslations = (
|
|
138
|
+
locale: string,
|
|
139
|
+
translations: TranslationObject,
|
|
140
|
+
): void => {
|
|
127
141
|
const validated = TranslationObjectSchema.parse(translations)
|
|
128
|
-
|
|
129
|
-
|
|
142
|
+
currentTranslations[locale] ??= {}
|
|
143
|
+
currentTranslations[locale] = {
|
|
144
|
+
...currentTranslations[locale],
|
|
145
|
+
...validated,
|
|
130
146
|
}
|
|
131
|
-
currentTranslations[locale] = { ...currentTranslations[locale], ...validated }
|
|
132
147
|
}
|
|
133
148
|
|
|
134
149
|
/**
|
|
@@ -137,7 +152,10 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
137
152
|
* @param locale - Locale to check (optional, uses current if not provided)
|
|
138
153
|
* @returns True if key exists
|
|
139
154
|
*/
|
|
140
|
-
const hasTranslation = (
|
|
155
|
+
const hasTranslation = (
|
|
156
|
+
path: string,
|
|
157
|
+
locale: string | null = null,
|
|
158
|
+
): boolean => {
|
|
141
159
|
const checkLocale = locale ?? currentLocale
|
|
142
160
|
|
|
143
161
|
// Check if locale exists
|
|
@@ -146,7 +164,8 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
146
164
|
}
|
|
147
165
|
|
|
148
166
|
const segments = path.split('.')
|
|
149
|
-
let current: Record<string, unknown> | string =
|
|
167
|
+
let current: Record<string, unknown> | string =
|
|
168
|
+
currentTranslations[checkLocale]
|
|
150
169
|
|
|
151
170
|
// Navigate through the path
|
|
152
171
|
for (const segment of segments) {
|