@pyreon/i18n 0.0.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/LICENSE +21 -0
- package/README.md +227 -0
- package/lib/analysis/devtools.js.html +5406 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/devtools.js +81 -0
- package/lib/devtools.js.map +1 -0
- package/lib/index.js +330 -0
- package/lib/index.js.map +1 -0
- package/lib/types/devtools.d.ts +74 -0
- package/lib/types/devtools.d.ts.map +1 -0
- package/lib/types/devtools2.d.ts +30 -0
- package/lib/types/devtools2.d.ts.map +1 -0
- package/lib/types/index.d.ts +334 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +244 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +57 -0
- package/src/context.ts +57 -0
- package/src/create-i18n.ts +309 -0
- package/src/devtools.ts +94 -0
- package/src/index.ts +18 -0
- package/src/interpolation.ts +28 -0
- package/src/pluralization.ts +31 -0
- package/src/tests/devtools.test.ts +166 -0
- package/src/tests/i18n.test.ts +859 -0
- package/src/tests/setup.ts +1 -0
- package/src/trans.ts +111 -0
- package/src/types.ts +112 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Vit Bokisch
|
|
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
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# @pyreon/i18n
|
|
2
|
+
|
|
3
|
+
Reactive internationalization for Pyreon. Async namespace loading, pluralization, interpolation, and rich JSX text.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @pyreon/i18n
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createI18n } from "@pyreon/i18n"
|
|
15
|
+
|
|
16
|
+
const i18n = createI18n({
|
|
17
|
+
locale: "en",
|
|
18
|
+
fallbackLocale: "en",
|
|
19
|
+
messages: {
|
|
20
|
+
en: {
|
|
21
|
+
greeting: "Hello, {{name}}!",
|
|
22
|
+
items_one: "{{count}} item",
|
|
23
|
+
items_other: "{{count}} items",
|
|
24
|
+
},
|
|
25
|
+
de: {
|
|
26
|
+
greeting: "Hallo, {{name}}!",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
i18n.t("greeting", { name: "Alice" }) // "Hello, Alice!"
|
|
32
|
+
i18n.t("items", { count: 3 }) // "3 items"
|
|
33
|
+
|
|
34
|
+
i18n.locale.set("de")
|
|
35
|
+
i18n.t("greeting", { name: "Alice" }) // "Hallo, Alice!"
|
|
36
|
+
i18n.t("items", { count: 1 }) // "1 item" (fallback to en)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## API
|
|
40
|
+
|
|
41
|
+
### `createI18n(options)`
|
|
42
|
+
|
|
43
|
+
Create a reactive i18n instance with static messages and/or an async namespace loader.
|
|
44
|
+
|
|
45
|
+
| Parameter | Type | Description |
|
|
46
|
+
| --- | --- | --- |
|
|
47
|
+
| `options.locale` | `string` | Initial locale (e.g. `"en"`) |
|
|
48
|
+
| `options.fallbackLocale` | `string` | Locale to try when key is missing in active locale |
|
|
49
|
+
| `options.messages` | `Record<string, TranslationDictionary>` | Static messages keyed by locale |
|
|
50
|
+
| `options.loader` | `NamespaceLoader` | `(locale, namespace) => Promise<TranslationDictionary>` |
|
|
51
|
+
| `options.defaultNamespace` | `string` | Default namespace for `t()` (default: `"common"`) |
|
|
52
|
+
| `options.pluralRules` | `PluralRules` | Custom plural rules per locale |
|
|
53
|
+
| `options.onMissingKey` | `(locale, key, namespace?) => string \| undefined` | Missing key handler |
|
|
54
|
+
|
|
55
|
+
**Returns:** `I18nInstance` with:
|
|
56
|
+
|
|
57
|
+
| Property | Type | Description |
|
|
58
|
+
| --- | --- | --- |
|
|
59
|
+
| `t(key, values?)` | `(key: string, values?: InterpolationValues) => string` | Translate a key |
|
|
60
|
+
| `locale` | `Signal<string>` | Current locale (reactive, writable) |
|
|
61
|
+
| `loadNamespace(ns, locale?)` | `(ns: string, locale?: string) => Promise<void>` | Load a namespace |
|
|
62
|
+
| `isLoading` | `Computed<boolean>` | Whether any namespace is loading |
|
|
63
|
+
| `loadedNamespaces` | `Computed<Set<string>>` | Namespaces loaded for current locale |
|
|
64
|
+
| `exists(key)` | `(key: string) => boolean` | Check if a key exists |
|
|
65
|
+
| `addMessages(locale, messages, ns?)` | `Function` | Add messages at runtime |
|
|
66
|
+
| `availableLocales` | `Computed<string[]>` | All locales with registered messages |
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const i18n = createI18n({
|
|
70
|
+
locale: "en",
|
|
71
|
+
loader: async (locale, namespace) => {
|
|
72
|
+
const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
73
|
+
return mod.default
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
await i18n.loadNamespace("auth")
|
|
77
|
+
i18n.t("auth:errors.invalid") // namespace:key.path syntax
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### `interpolate(template, values?)`
|
|
81
|
+
|
|
82
|
+
Replace `{{key}}` placeholders in a string. Supports whitespace inside braces. Unmatched placeholders are left as-is.
|
|
83
|
+
|
|
84
|
+
| Parameter | Type | Description |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| `template` | `string` | Template string with `{{placeholders}}` |
|
|
87
|
+
| `values` | `InterpolationValues` | Key-value pairs for substitution |
|
|
88
|
+
|
|
89
|
+
**Returns:** `string`
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
interpolate("Hello, {{ name }}!", { name: "World" })
|
|
93
|
+
// "Hello, World!"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `resolvePluralCategory(locale, count, customRules?)`
|
|
97
|
+
|
|
98
|
+
Resolve the CLDR plural category for a count. Uses custom rules if provided, then `Intl.PluralRules`, then a basic `one`/`other` fallback.
|
|
99
|
+
|
|
100
|
+
| Parameter | Type | Description |
|
|
101
|
+
| --- | --- | --- |
|
|
102
|
+
| `locale` | `string` | Locale code |
|
|
103
|
+
| `count` | `number` | The number to pluralize for |
|
|
104
|
+
| `customRules` | `PluralRules` | Optional custom rules per locale |
|
|
105
|
+
|
|
106
|
+
**Returns:** `string` — one of `"zero"`, `"one"`, `"two"`, `"few"`, `"many"`, `"other"`
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
resolvePluralCategory("en", 1) // "one"
|
|
110
|
+
resolvePluralCategory("en", 5) // "other"
|
|
111
|
+
resolvePluralCategory("ar", 3) // "few" (via Intl.PluralRules)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `I18nProvider` / `useI18n()`
|
|
115
|
+
|
|
116
|
+
Context pattern for providing an i18n instance to the component tree.
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
// Root:
|
|
120
|
+
<I18nProvider instance={i18n}>
|
|
121
|
+
<App />
|
|
122
|
+
</I18nProvider>
|
|
123
|
+
|
|
124
|
+
// Any descendant:
|
|
125
|
+
function Greeting() {
|
|
126
|
+
const { t, locale } = useI18n()
|
|
127
|
+
return () => <h1>{t("greeting", { name: "World" })}</h1>
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`I18nContext` is also exported for advanced usage with `useContext` directly.
|
|
132
|
+
|
|
133
|
+
### `Trans`
|
|
134
|
+
|
|
135
|
+
Rich JSX interpolation component. Resolves `{{values}}` first, then maps `<tag>content</tag>` patterns to component functions.
|
|
136
|
+
|
|
137
|
+
| Parameter | Type | Description |
|
|
138
|
+
| --- | --- | --- |
|
|
139
|
+
| `t` | `(key, values?) => string` | Translation function (from `useI18n()`) |
|
|
140
|
+
| `i18nKey` | `string` | Translation key |
|
|
141
|
+
| `values` | `InterpolationValues` | Interpolation values |
|
|
142
|
+
| `components` | `Record<string, (children) => VNode>` | Component map for rich tags |
|
|
143
|
+
|
|
144
|
+
```tsx
|
|
145
|
+
// Translation: "Read our <terms>terms</terms> and <privacy>policy</privacy>"
|
|
146
|
+
<Trans
|
|
147
|
+
t={t}
|
|
148
|
+
i18nKey="legal"
|
|
149
|
+
components={{
|
|
150
|
+
terms: (children) => <a href="/terms">{children}</a>,
|
|
151
|
+
privacy: (children) => <a href="/privacy">{children}</a>,
|
|
152
|
+
}}
|
|
153
|
+
/>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `parseRichText(text)`
|
|
157
|
+
|
|
158
|
+
Parse a string into an array of plain text and `{ tag, children }` segments. Used internally by `Trans`.
|
|
159
|
+
|
|
160
|
+
| Parameter | Type | Description |
|
|
161
|
+
| --- | --- | --- |
|
|
162
|
+
| `text` | `string` | String with `<tag>content</tag>` patterns |
|
|
163
|
+
|
|
164
|
+
**Returns:** `(string | { tag: string, children: string })[]`
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
parseRichText("Hello <bold>world</bold>!")
|
|
168
|
+
// ["Hello ", { tag: "bold", children: "world" }, "!"]
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Patterns
|
|
172
|
+
|
|
173
|
+
### Namespace-Based Loading
|
|
174
|
+
|
|
175
|
+
Split translations by feature and load them lazily.
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
const i18n = createI18n({
|
|
179
|
+
locale: "en",
|
|
180
|
+
loader: (locale, ns) => fetch(`/locales/${locale}/${ns}.json`).then(r => r.json()),
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// Load on route entry:
|
|
184
|
+
await i18n.loadNamespace("dashboard")
|
|
185
|
+
i18n.t("dashboard:widgets.chart")
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Runtime Message Addition
|
|
189
|
+
|
|
190
|
+
Add messages without async loading (e.g. from server-rendered data).
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
i18n.addMessages("en", { newFeature: "Try our new feature!" })
|
|
194
|
+
i18n.addMessages("en", { errors: { timeout: "Request timed out" } }, "api")
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Pluralization
|
|
198
|
+
|
|
199
|
+
Use `_one`, `_other` (and `_zero`, `_two`, `_few`, `_many` for complex locales) suffixes with a `count` value.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
// messages: { items_one: "{{count}} item", items_other: "{{count}} items" }
|
|
203
|
+
i18n.t("items", { count: 1 }) // "1 item"
|
|
204
|
+
i18n.t("items", { count: 5 }) // "5 items"
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Types
|
|
208
|
+
|
|
209
|
+
| Type | Description |
|
|
210
|
+
| --- | --- |
|
|
211
|
+
| `I18nInstance` | Public API returned by `createI18n()` |
|
|
212
|
+
| `I18nOptions` | Options for `createI18n()` |
|
|
213
|
+
| `TranslationDictionary` | `{ [key: string]: string \| TranslationDictionary }` |
|
|
214
|
+
| `TranslationMessages` | `Record<string, TranslationDictionary>` |
|
|
215
|
+
| `NamespaceLoader` | `(locale: string, namespace: string) => Promise<TranslationDictionary \| undefined>` |
|
|
216
|
+
| `InterpolationValues` | `Record<string, string \| number>` |
|
|
217
|
+
| `PluralRules` | `Record<string, (count: number) => string>` |
|
|
218
|
+
| `I18nProviderProps` | Props for `I18nProvider`: `{ instance: I18nInstance }` |
|
|
219
|
+
| `TransProps` | Props for `Trans` component |
|
|
220
|
+
|
|
221
|
+
## Gotchas
|
|
222
|
+
|
|
223
|
+
- `t()` reads `locale` reactively — it re-evaluates inside effects and computeds when the locale changes.
|
|
224
|
+
- Concurrent loads for the same locale:namespace are deduplicated — calling `loadNamespace("auth")` twice returns the same promise.
|
|
225
|
+
- Missing keys return the key string itself as a visual fallback (e.g. `"auth:missing.key"`).
|
|
226
|
+
- The default namespace is `"common"` — keys without a `namespace:` prefix look up in the `"common"` namespace.
|
|
227
|
+
- `addMessages` deep-merges into existing translations. It does not replace the entire namespace.
|