@zairakai/js-i18n 1.0.14 → 1.0.16
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/LICENSE +21 -0
- package/README.md +42 -26
- package/dist/index.cjs +11 -11
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +11 -11
- package/package.json +13 -10
- package/src/index.ts +1 -1
- package/src/pluralization.ts +6 -6
- package/src/schemas.ts +3 -3
- package/src/translator.ts +6 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Stanislas Poisson (Zairakai)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# JS i18n
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
`@zairakai/js-i18n`
|
|
2
6
|
|
|
3
7
|
[![Main][pipeline-main-badge]][pipeline-main-link]
|
|
4
8
|
[![Develop][pipeline-develop-badge]][pipeline-develop-link]
|
|
@@ -12,8 +16,10 @@
|
|
|
12
16
|
[![Node.js][node-badge]][node]
|
|
13
17
|
[![ESLint][eslint-badge]][eslint]
|
|
14
18
|
[![Prettier][prettier-badge]][prettier]
|
|
19
|
+
[![Vitest][vitest-badge]][vitest]
|
|
20
|
+
[![Knip][knip-badge]][knip]
|
|
15
21
|
|
|
16
|
-
|
|
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`.
|
|
17
23
|
|
|
18
24
|
**Documentation: [js-i18n-8f9736.gitlab.io][docs]**
|
|
19
25
|
|
|
@@ -41,6 +47,12 @@ The documentation site has this guide and the reference of the API generated fro
|
|
|
41
47
|
npm install @zairakai/js-i18n
|
|
42
48
|
```
|
|
43
49
|
|
|
50
|
+
### In the browser
|
|
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
|
+
|
|
44
56
|
---
|
|
45
57
|
|
|
46
58
|
## Usage
|
|
@@ -135,14 +147,14 @@ The same applies to `setTranslations()`, `addTranslations()`, and `translate()`
|
|
|
135
147
|
|
|
136
148
|
The pluralization format follows Laravel's `trans_choice` pattern exactly.
|
|
137
149
|
|
|
138
|
-
| Pattern
|
|
139
|
-
|
|
|
140
|
-
| `{0}`
|
|
141
|
-
| `{1}`
|
|
142
|
-
| `{n}`
|
|
143
|
-
| `[2,*]`
|
|
144
|
-
| `[0,1]`
|
|
145
|
-
| `[1,10]`
|
|
150
|
+
| Pattern | Matches | Example |
|
|
151
|
+
| :--- | :--- | :--- |
|
|
152
|
+
| `{0}` | Exactly 0 | `{0} No items` |
|
|
153
|
+
| `{1}` | Exactly 1 | `{1} One item` |
|
|
154
|
+
| `{n}` | Exactly n | `{5} Exactly five` |
|
|
155
|
+
| `[2,*]` | 2 and above | `[2,*] :count items` |
|
|
156
|
+
| `[0,1]` | 0 or 1 | `[0,1] :count file` |
|
|
157
|
+
| `[1,10]` | Between 1 and 10 (inclusive) | `[1,10] a few items` |
|
|
146
158
|
|
|
147
159
|
Forms are separated by `|`. The `:count` placeholder is automatically replaced by the count value.
|
|
148
160
|
|
|
@@ -158,7 +170,7 @@ Forms are separated by `|`. The `:count` placeholder is automatically replaced b
|
|
|
158
170
|
### Factory functions
|
|
159
171
|
|
|
160
172
|
| Function | Description |
|
|
161
|
-
|
|
|
173
|
+
| :--- | :--- |
|
|
162
174
|
| `createI18n(translations?, locale?)` | Creates a translator instance (alias, preferred). |
|
|
163
175
|
| `createTranslator(translations?, locale?)` | Creates a translator instance (explicit name). |
|
|
164
176
|
|
|
@@ -166,25 +178,25 @@ Both return a `Translator` instance. Default locale is `'en'`.
|
|
|
166
178
|
|
|
167
179
|
### Translator methods
|
|
168
180
|
|
|
169
|
-
| Method
|
|
170
|
-
|
|
|
171
|
-
| `__(path, replacements?)`
|
|
172
|
-
| `__(path, count, replacements?)`
|
|
173
|
-
| `translate(path, replacements?)`
|
|
174
|
-
| `translate(path, count, replacements?)` | Alias for `__` with count.
|
|
175
|
-
| `setLocale(locale)`
|
|
176
|
-
| `getLocale()`
|
|
177
|
-
| `setTranslations(locale, translations)` | Replace all translations for a locale.
|
|
178
|
-
| `addTranslations(locale, translations)` | Merge translations into an existing locale.
|
|
179
|
-
| `hasTranslation(path, locale?)`
|
|
180
|
-
| `getAvailableLocales()`
|
|
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. |
|
|
181
193
|
|
|
182
194
|
### Pluralization utilities
|
|
183
195
|
|
|
184
|
-
| Function
|
|
185
|
-
|
|
|
196
|
+
| Function | Description |
|
|
197
|
+
| :--- | :--- |
|
|
186
198
|
| `applyPluralization(value, count)` | Apply pluralization to a raw string with count. |
|
|
187
|
-
| `selectPluralForm(forms, count)`
|
|
199
|
+
| `selectPluralForm(forms, count)` | Select the correct form from a pre-split array. |
|
|
188
200
|
|
|
189
201
|
---
|
|
190
202
|
|
|
@@ -238,6 +250,10 @@ See [CONTRIBUTING.md](./CONTRIBUTING.md) for the full development workflow.
|
|
|
238
250
|
[eslint]: https://eslint.org
|
|
239
251
|
[prettier-badge]: https://img.shields.io/badge/formatter-prettier-F7B93E.svg?logo=prettier
|
|
240
252
|
[prettier]: https://prettier.io
|
|
253
|
+
[vitest-badge]: https://img.shields.io/badge/test-vitest-6E9F18.svg?logo=vitest
|
|
254
|
+
[vitest]: https://vitest.dev
|
|
255
|
+
[knip-badge]: https://img.shields.io/badge/dead%20code-knip-EC4899.svg
|
|
256
|
+
[knip]: https://knip.dev
|
|
241
257
|
[ecosystem]: https://gitlab.com/zairakai
|
|
242
258
|
[docs]: https://js-i18n-8f9736.gitlab.io
|
|
243
259
|
[docs-badge]: https://img.shields.io/badge/docs-online-blue
|
package/dist/index.cjs
CHANGED
|
@@ -40,8 +40,8 @@ var selectPluralForm = (message, count) => {
|
|
|
40
40
|
return exactMatch2[2].trim();
|
|
41
41
|
}
|
|
42
42
|
} else if (rangeMatch2) {
|
|
43
|
-
const min =
|
|
44
|
-
const max =
|
|
43
|
+
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(rangeMatch2[1], 10);
|
|
44
|
+
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(rangeMatch2[2], 10);
|
|
45
45
|
if (count >= min && count <= max) {
|
|
46
46
|
return rangeMatch2[3].trim();
|
|
47
47
|
}
|
|
@@ -51,9 +51,9 @@ var selectPluralForm = (message, count) => {
|
|
|
51
51
|
const trimmed = part.trim();
|
|
52
52
|
return !trimmed.match(/^[{\[]/);
|
|
53
53
|
});
|
|
54
|
-
if (
|
|
55
|
-
return
|
|
56
|
-
} else if (
|
|
54
|
+
if (simpleParts.length >= 2) {
|
|
55
|
+
return count === 1 ? simpleParts[0].trim() : simpleParts[1].trim();
|
|
56
|
+
} else if (simpleParts.length === 1) {
|
|
57
57
|
return simpleParts[0].trim();
|
|
58
58
|
}
|
|
59
59
|
const firstPart = parts[0].trim();
|
|
@@ -67,7 +67,7 @@ var selectPluralForm = (message, count) => {
|
|
|
67
67
|
return firstPart;
|
|
68
68
|
};
|
|
69
69
|
var applyPluralization = (translation, count) => {
|
|
70
|
-
if ("string"
|
|
70
|
+
if (typeof translation === "string" && translation.includes("|") && count !== null) {
|
|
71
71
|
return selectPluralForm(translation, count);
|
|
72
72
|
}
|
|
73
73
|
return translation;
|
|
@@ -98,7 +98,7 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
98
98
|
ReplacementParametersSchema.parse(replacements);
|
|
99
99
|
let count = null;
|
|
100
100
|
let actualReplacements = replacements;
|
|
101
|
-
if ("number"
|
|
101
|
+
if (typeof countOrReplacements === "number") {
|
|
102
102
|
count = countOrReplacements;
|
|
103
103
|
actualReplacements = { count: countOrReplacements, ...replacements };
|
|
104
104
|
} else {
|
|
@@ -110,13 +110,13 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
110
110
|
const segments = path.split(".");
|
|
111
111
|
let translation = currentTranslations[currentLocale];
|
|
112
112
|
for (const segment of segments) {
|
|
113
|
-
if (translation && "object"
|
|
113
|
+
if (translation && typeof translation === "object" && Object.prototype.hasOwnProperty.call(translation, segment)) {
|
|
114
114
|
translation = translation[segment];
|
|
115
115
|
} else {
|
|
116
116
|
return path;
|
|
117
117
|
}
|
|
118
118
|
}
|
|
119
|
-
if ("string"
|
|
119
|
+
if (typeof translation === "string") {
|
|
120
120
|
const pluralizedTranslation = applyPluralization(translation, count);
|
|
121
121
|
const result = pluralizedTranslation.replace(/:(\w+)/g, (match, key) => {
|
|
122
122
|
return Object.prototype.hasOwnProperty.call(actualReplacements, key) ? String(actualReplacements[key]) : match;
|
|
@@ -149,13 +149,13 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
149
149
|
const segments = path.split(".");
|
|
150
150
|
let current = currentTranslations[checkLocale];
|
|
151
151
|
for (const segment of segments) {
|
|
152
|
-
if (current && "object"
|
|
152
|
+
if (current && typeof current === "object" && segment in current) {
|
|
153
153
|
current = current[segment];
|
|
154
154
|
} else {
|
|
155
155
|
return false;
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
|
-
return "string"
|
|
158
|
+
return typeof current === "string";
|
|
159
159
|
};
|
|
160
160
|
const getAvailableLocales = () => Object.keys(currentTranslations);
|
|
161
161
|
return {
|
package/dist/index.d.cts
CHANGED
|
@@ -74,7 +74,7 @@ declare const createTranslator: (translations?: TranslationData, locale?: string
|
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* @zairakai/npm-i18n
|
|
77
|
-
*
|
|
77
|
+
* Internationalization library with Laravel-style features
|
|
78
78
|
*/
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -85,4 +85,5 @@ declare const createTranslator: (translations?: TranslationData, locale?: string
|
|
|
85
85
|
*/
|
|
86
86
|
declare const createI18n: (translations?: TranslationData, locale?: string) => Translator;
|
|
87
87
|
|
|
88
|
-
export {
|
|
88
|
+
export { applyPluralization, createI18n, createTranslator, selectPluralForm };
|
|
89
|
+
export type { ReplacementParameters, TranslateFunction, TranslationData, TranslationObject, Translator };
|
package/dist/index.d.ts
CHANGED
|
@@ -74,7 +74,7 @@ declare const createTranslator: (translations?: TranslationData, locale?: string
|
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
76
|
* @zairakai/npm-i18n
|
|
77
|
-
*
|
|
77
|
+
* Internationalization library with Laravel-style features
|
|
78
78
|
*/
|
|
79
79
|
|
|
80
80
|
/**
|
|
@@ -85,4 +85,5 @@ declare const createTranslator: (translations?: TranslationData, locale?: string
|
|
|
85
85
|
*/
|
|
86
86
|
declare const createI18n: (translations?: TranslationData, locale?: string) => Translator;
|
|
87
87
|
|
|
88
|
-
export {
|
|
88
|
+
export { applyPluralization, createI18n, createTranslator, selectPluralForm };
|
|
89
|
+
export type { ReplacementParameters, TranslateFunction, TranslationData, TranslationObject, Translator };
|
package/dist/index.js
CHANGED
|
@@ -11,8 +11,8 @@ var selectPluralForm = (message, count) => {
|
|
|
11
11
|
return exactMatch2[2].trim();
|
|
12
12
|
}
|
|
13
13
|
} else if (rangeMatch2) {
|
|
14
|
-
const min =
|
|
15
|
-
const max =
|
|
14
|
+
const min = rangeMatch2[1] === "*" ? -Infinity : parseInt(rangeMatch2[1], 10);
|
|
15
|
+
const max = rangeMatch2[2] === "*" ? Infinity : parseInt(rangeMatch2[2], 10);
|
|
16
16
|
if (count >= min && count <= max) {
|
|
17
17
|
return rangeMatch2[3].trim();
|
|
18
18
|
}
|
|
@@ -22,9 +22,9 @@ var selectPluralForm = (message, count) => {
|
|
|
22
22
|
const trimmed = part.trim();
|
|
23
23
|
return !trimmed.match(/^[{\[]/);
|
|
24
24
|
});
|
|
25
|
-
if (
|
|
26
|
-
return
|
|
27
|
-
} else if (
|
|
25
|
+
if (simpleParts.length >= 2) {
|
|
26
|
+
return count === 1 ? simpleParts[0].trim() : simpleParts[1].trim();
|
|
27
|
+
} else if (simpleParts.length === 1) {
|
|
28
28
|
return simpleParts[0].trim();
|
|
29
29
|
}
|
|
30
30
|
const firstPart = parts[0].trim();
|
|
@@ -38,7 +38,7 @@ var selectPluralForm = (message, count) => {
|
|
|
38
38
|
return firstPart;
|
|
39
39
|
};
|
|
40
40
|
var applyPluralization = (translation, count) => {
|
|
41
|
-
if ("string"
|
|
41
|
+
if (typeof translation === "string" && translation.includes("|") && count !== null) {
|
|
42
42
|
return selectPluralForm(translation, count);
|
|
43
43
|
}
|
|
44
44
|
return translation;
|
|
@@ -69,7 +69,7 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
69
69
|
ReplacementParametersSchema.parse(replacements);
|
|
70
70
|
let count = null;
|
|
71
71
|
let actualReplacements = replacements;
|
|
72
|
-
if ("number"
|
|
72
|
+
if (typeof countOrReplacements === "number") {
|
|
73
73
|
count = countOrReplacements;
|
|
74
74
|
actualReplacements = { count: countOrReplacements, ...replacements };
|
|
75
75
|
} else {
|
|
@@ -81,13 +81,13 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
81
81
|
const segments = path.split(".");
|
|
82
82
|
let translation = currentTranslations[currentLocale];
|
|
83
83
|
for (const segment of segments) {
|
|
84
|
-
if (translation && "object"
|
|
84
|
+
if (translation && typeof translation === "object" && Object.prototype.hasOwnProperty.call(translation, segment)) {
|
|
85
85
|
translation = translation[segment];
|
|
86
86
|
} else {
|
|
87
87
|
return path;
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
|
-
if ("string"
|
|
90
|
+
if (typeof translation === "string") {
|
|
91
91
|
const pluralizedTranslation = applyPluralization(translation, count);
|
|
92
92
|
const result = pluralizedTranslation.replace(/:(\w+)/g, (match, key) => {
|
|
93
93
|
return Object.prototype.hasOwnProperty.call(actualReplacements, key) ? String(actualReplacements[key]) : match;
|
|
@@ -120,13 +120,13 @@ var createTranslator = (translations = {}, locale = "en") => {
|
|
|
120
120
|
const segments = path.split(".");
|
|
121
121
|
let current = currentTranslations[checkLocale];
|
|
122
122
|
for (const segment of segments) {
|
|
123
|
-
if (current && "object"
|
|
123
|
+
if (current && typeof current === "object" && segment in current) {
|
|
124
124
|
current = current[segment];
|
|
125
125
|
} else {
|
|
126
126
|
return false;
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
|
-
return "string"
|
|
129
|
+
return typeof current === "string";
|
|
130
130
|
};
|
|
131
131
|
const getAvailableLocales = () => Object.keys(currentTranslations);
|
|
132
132
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zairakai/js-i18n",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.16",
|
|
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",
|
|
@@ -74,26 +74,29 @@
|
|
|
74
74
|
"validate": "make quality"
|
|
75
75
|
},
|
|
76
76
|
"overrides": {
|
|
77
|
-
"esbuild": "^0.28.1"
|
|
77
|
+
"esbuild": "^0.28.1",
|
|
78
|
+
"re2js": "2.9.1"
|
|
78
79
|
},
|
|
79
80
|
"dependencies": {
|
|
80
81
|
"zod": "^4.6.7"
|
|
81
82
|
},
|
|
82
83
|
"devDependencies": {
|
|
83
|
-
"@types/node": "^26.6.
|
|
84
|
-
"@vitest/coverage-v8": "^5.0
|
|
85
|
-
"@zairakai/js-dev-tools": "^3.
|
|
86
|
-
"
|
|
84
|
+
"@types/node": "^26.6.6",
|
|
85
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
86
|
+
"@zairakai/js-dev-tools": "^3.21.0",
|
|
87
|
+
"esbuild": "^0.28.1",
|
|
88
|
+
"knip": "^6.44.2",
|
|
87
89
|
"sort-package-json": "^4.1.0",
|
|
88
|
-
"tsup": "^
|
|
90
|
+
"tsup": "^9.0.0",
|
|
89
91
|
"typedoc": "^0.28.20",
|
|
90
92
|
"typescript": "^6.0.3",
|
|
91
|
-
"vitest": "^5.0
|
|
93
|
+
"vitest": "^5.1.0"
|
|
92
94
|
},
|
|
93
95
|
"engines": {
|
|
94
96
|
"node": ">=24.0.0"
|
|
95
97
|
},
|
|
96
98
|
"publishConfig": {
|
|
97
99
|
"access": "public"
|
|
98
|
-
}
|
|
100
|
+
},
|
|
101
|
+
"sideEffects": false
|
|
99
102
|
}
|
package/src/index.ts
CHANGED
package/src/pluralization.ts
CHANGED
|
@@ -34,8 +34,8 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
34
34
|
}
|
|
35
35
|
} else if (rangeMatch) {
|
|
36
36
|
// Range match [1,5] or [20,*]
|
|
37
|
-
const min =
|
|
38
|
-
const max =
|
|
37
|
+
const min = rangeMatch[1] === '*' ? -Infinity : parseInt(rangeMatch[1], 10)
|
|
38
|
+
const max = rangeMatch[2] === '*' ? Infinity : parseInt(rangeMatch[2], 10)
|
|
39
39
|
|
|
40
40
|
if (count >= min && count <= max) {
|
|
41
41
|
return rangeMatch[3].trim()
|
|
@@ -50,10 +50,10 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
50
50
|
return !trimmed.match(/^[{\[]/) // No special condition markers
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
-
if (
|
|
53
|
+
if (simpleParts.length >= 2) {
|
|
54
54
|
// English pluralization: only 1 = singular, others (including 0) = plural
|
|
55
|
-
return
|
|
56
|
-
} else if (
|
|
55
|
+
return count === 1 ? simpleParts[0].trim() : simpleParts[1].trim()
|
|
56
|
+
} else if (simpleParts.length === 1) {
|
|
57
57
|
return simpleParts[0].trim()
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -80,7 +80,7 @@ export const selectPluralForm = (message: string, count: number): string => {
|
|
|
80
80
|
* @returns Pluralized translation
|
|
81
81
|
*/
|
|
82
82
|
export const applyPluralization = (translation: string, count: number | null): string => {
|
|
83
|
-
if ('string'
|
|
83
|
+
if (typeof translation === 'string' && translation.includes('|') && count !== null) {
|
|
84
84
|
return selectPluralForm(translation, count)
|
|
85
85
|
}
|
|
86
86
|
return translation
|
package/src/schemas.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type { ReplacementParameters, TranslationData, TranslationObject } from '
|
|
|
9
9
|
* Recursive schema for a translation value (string or nested TranslationObject)
|
|
10
10
|
*/
|
|
11
11
|
const TranslationValueSchema: z.ZodType<string | TranslationObject> = z.lazy(() =>
|
|
12
|
-
z.union([z.string(), z.record(z.string(), TranslationValueSchema)])
|
|
12
|
+
z.union([z.string(), z.record(z.string(), TranslationValueSchema)]),
|
|
13
13
|
) as z.ZodType<string | TranslationObject>
|
|
14
14
|
|
|
15
15
|
/**
|
|
@@ -17,7 +17,7 @@ const TranslationValueSchema: z.ZodType<string | TranslationObject> = z.lazy(()
|
|
|
17
17
|
*/
|
|
18
18
|
export const TranslationObjectSchema: z.ZodType<TranslationObject> = z.record(
|
|
19
19
|
z.string(),
|
|
20
|
-
TranslationValueSchema
|
|
20
|
+
TranslationValueSchema,
|
|
21
21
|
) as z.ZodType<TranslationObject>
|
|
22
22
|
|
|
23
23
|
/**
|
|
@@ -30,7 +30,7 @@ export const TranslationDataSchema: z.ZodType<TranslationData> = z.record(z.stri
|
|
|
30
30
|
*/
|
|
31
31
|
export const ReplacementParametersSchema: z.ZodType<ReplacementParameters> = z.record(
|
|
32
32
|
z.string(),
|
|
33
|
-
z.union([z.string(), z.number()])
|
|
33
|
+
z.union([z.string(), z.number()]),
|
|
34
34
|
)
|
|
35
35
|
|
|
36
36
|
/**
|
package/src/translator.ts
CHANGED
|
@@ -37,7 +37,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
37
37
|
const translate: TranslateFunction = (
|
|
38
38
|
path: string,
|
|
39
39
|
countOrReplacements: number | ReplacementParameters = {},
|
|
40
|
-
replacements: ReplacementParameters = {}
|
|
40
|
+
replacements: ReplacementParameters = {},
|
|
41
41
|
): string => {
|
|
42
42
|
CountOrReplacementsSchema.parse(countOrReplacements)
|
|
43
43
|
ReplacementParametersSchema.parse(replacements)
|
|
@@ -46,7 +46,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
46
46
|
let actualReplacements = replacements
|
|
47
47
|
|
|
48
48
|
// Handle parameter overloading
|
|
49
|
-
if ('number'
|
|
49
|
+
if (typeof countOrReplacements === 'number') {
|
|
50
50
|
count = countOrReplacements
|
|
51
51
|
// Automatically add count to replacements
|
|
52
52
|
actualReplacements = { count: countOrReplacements, ...replacements }
|
|
@@ -66,7 +66,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
66
66
|
for (const segment of segments) {
|
|
67
67
|
if (
|
|
68
68
|
translation &&
|
|
69
|
-
'object'
|
|
69
|
+
typeof translation === 'object' &&
|
|
70
70
|
Object.prototype.hasOwnProperty.call(translation, segment)
|
|
71
71
|
) {
|
|
72
72
|
translation = translation[segment]
|
|
@@ -75,7 +75,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
if ('string'
|
|
78
|
+
if (typeof translation === 'string') {
|
|
79
79
|
// Apply pluralization if count provided
|
|
80
80
|
const pluralizedTranslation = applyPluralization(translation, count)
|
|
81
81
|
|
|
@@ -150,7 +150,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
150
150
|
|
|
151
151
|
// Navigate through the path
|
|
152
152
|
for (const segment of segments) {
|
|
153
|
-
if (current && 'object'
|
|
153
|
+
if (current && typeof current === 'object' && segment in current) {
|
|
154
154
|
current = current[segment] as Record<string, unknown> | string
|
|
155
155
|
} else {
|
|
156
156
|
return false
|
|
@@ -158,7 +158,7 @@ export const createTranslator = (translations: TranslationData = {}, locale = 'e
|
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// Check if final value is a string
|
|
161
|
-
return 'string'
|
|
161
|
+
return typeof current === 'string'
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
/**
|