@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
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { computed, signal } from "@pyreon/reactivity";
|
|
2
|
+
import { Fragment, createContext, h, onUnmount, popContext, pushContext, useContext } from "@pyreon/core";
|
|
3
|
+
|
|
4
|
+
//#region src/interpolation.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Replace `{{key}}` placeholders in a string with values from the given record.
|
|
8
|
+
* Supports optional whitespace inside braces: `{{ name }}` works too.
|
|
9
|
+
* Unmatched placeholders are left as-is.
|
|
10
|
+
*/
|
|
11
|
+
function interpolate(template, values) {
|
|
12
|
+
if (!values || !template.includes("{{")) return template;
|
|
13
|
+
return template.replace(INTERPOLATION_RE, (_, key) => {
|
|
14
|
+
const trimmed = key.trim();
|
|
15
|
+
const value = values[trimmed];
|
|
16
|
+
if (value === void 0) return `{{${trimmed}}}`;
|
|
17
|
+
try {
|
|
18
|
+
return typeof value === "object" && value !== null ? JSON.stringify(value) : `${value}`;
|
|
19
|
+
} catch {
|
|
20
|
+
return `{{${trimmed}}}`;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/pluralization.ts
|
|
27
|
+
/**
|
|
28
|
+
* Resolve the plural category for a given count and locale.
|
|
29
|
+
*
|
|
30
|
+
* Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.
|
|
31
|
+
* Returns CLDR plural categories: "zero", "one", "two", "few", "many", "other".
|
|
32
|
+
*/
|
|
33
|
+
function resolvePluralCategory(locale, count, customRules) {
|
|
34
|
+
if (customRules?.[locale]) return customRules[locale](count);
|
|
35
|
+
if (typeof Intl !== "undefined" && Intl.PluralRules) try {
|
|
36
|
+
return new Intl.PluralRules(locale).select(count);
|
|
37
|
+
} catch {}
|
|
38
|
+
return count === 1 ? "one" : "other";
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/create-i18n.ts
|
|
43
|
+
/**
|
|
44
|
+
* Resolve a dot-separated key path in a nested dictionary.
|
|
45
|
+
* E.g. "user.greeting" → dictionary.user.greeting
|
|
46
|
+
*/
|
|
47
|
+
function resolveKey(dict, keyPath) {
|
|
48
|
+
const parts = keyPath.split(".");
|
|
49
|
+
let current = dict;
|
|
50
|
+
for (const part of parts) {
|
|
51
|
+
if (current == null || typeof current === "string") return void 0;
|
|
52
|
+
current = current[part];
|
|
53
|
+
}
|
|
54
|
+
return typeof current === "string" ? current : void 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Deep-merge source into target (mutates target).
|
|
58
|
+
*/
|
|
59
|
+
function deepMerge(target, source) {
|
|
60
|
+
for (const key of Object.keys(source)) {
|
|
61
|
+
if (key === "__proto__" || key === "constructor" || key === "prototype") continue;
|
|
62
|
+
const sourceVal = source[key];
|
|
63
|
+
const targetVal = target[key];
|
|
64
|
+
if (typeof sourceVal === "object" && sourceVal !== null && typeof targetVal === "object" && targetVal !== null) deepMerge(targetVal, sourceVal);else target[key] = sourceVal;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create a reactive i18n instance.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* const i18n = createI18n({
|
|
72
|
+
* locale: 'en',
|
|
73
|
+
* fallbackLocale: 'en',
|
|
74
|
+
* messages: {
|
|
75
|
+
* en: { greeting: 'Hello {{name}}!' },
|
|
76
|
+
* de: { greeting: 'Hallo {{name}}!' },
|
|
77
|
+
* },
|
|
78
|
+
* })
|
|
79
|
+
*
|
|
80
|
+
* // Reactive translation — re-evaluates on locale change
|
|
81
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hello Alice!"
|
|
82
|
+
* i18n.locale.set('de')
|
|
83
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hallo Alice!"
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* // Async namespace loading
|
|
87
|
+
* const i18n = createI18n({
|
|
88
|
+
* locale: 'en',
|
|
89
|
+
* loader: async (locale, namespace) => {
|
|
90
|
+
* const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
91
|
+
* return mod.default
|
|
92
|
+
* },
|
|
93
|
+
* })
|
|
94
|
+
* await i18n.loadNamespace('auth')
|
|
95
|
+
* i18n.t('auth:errors.invalid') // looks up "errors.invalid" in "auth" namespace
|
|
96
|
+
*/
|
|
97
|
+
function createI18n(options) {
|
|
98
|
+
const {
|
|
99
|
+
fallbackLocale,
|
|
100
|
+
loader,
|
|
101
|
+
defaultNamespace = "common",
|
|
102
|
+
pluralRules,
|
|
103
|
+
onMissingKey
|
|
104
|
+
} = options;
|
|
105
|
+
const locale = signal(options.locale);
|
|
106
|
+
const store = /* @__PURE__ */new Map();
|
|
107
|
+
const storeVersion = signal(0);
|
|
108
|
+
const pendingLoads = signal(0);
|
|
109
|
+
const loadedNsVersion = signal(0);
|
|
110
|
+
const pendingPromises = /* @__PURE__ */new Map();
|
|
111
|
+
const isLoading = computed(() => pendingLoads() > 0);
|
|
112
|
+
const loadedNamespaces = computed(() => {
|
|
113
|
+
loadedNsVersion();
|
|
114
|
+
const currentLocale = locale();
|
|
115
|
+
const nsMap = store.get(currentLocale);
|
|
116
|
+
return new Set(nsMap ? nsMap.keys() : []);
|
|
117
|
+
});
|
|
118
|
+
const availableLocales = computed(() => {
|
|
119
|
+
storeVersion();
|
|
120
|
+
return [...store.keys()];
|
|
121
|
+
});
|
|
122
|
+
if (options.messages) for (const [loc, dict] of Object.entries(options.messages)) {
|
|
123
|
+
const nsMap = /* @__PURE__ */new Map();
|
|
124
|
+
nsMap.set(defaultNamespace, dict);
|
|
125
|
+
store.set(loc, nsMap);
|
|
126
|
+
}
|
|
127
|
+
function getNamespaceMap(loc) {
|
|
128
|
+
let nsMap = store.get(loc);
|
|
129
|
+
if (!nsMap) {
|
|
130
|
+
nsMap = /* @__PURE__ */new Map();
|
|
131
|
+
store.set(loc, nsMap);
|
|
132
|
+
}
|
|
133
|
+
return nsMap;
|
|
134
|
+
}
|
|
135
|
+
function lookupKey(loc, namespace, keyPath) {
|
|
136
|
+
const nsMap = store.get(loc);
|
|
137
|
+
if (!nsMap) return void 0;
|
|
138
|
+
const dict = nsMap.get(namespace);
|
|
139
|
+
if (!dict) return void 0;
|
|
140
|
+
return resolveKey(dict, keyPath);
|
|
141
|
+
}
|
|
142
|
+
function resolveTranslation(key, values) {
|
|
143
|
+
const currentLocale = locale();
|
|
144
|
+
storeVersion();
|
|
145
|
+
let namespace = defaultNamespace;
|
|
146
|
+
let keyPath = key;
|
|
147
|
+
const colonIndex = key.indexOf(":");
|
|
148
|
+
if (colonIndex > 0) {
|
|
149
|
+
namespace = key.slice(0, colonIndex);
|
|
150
|
+
keyPath = key.slice(colonIndex + 1);
|
|
151
|
+
}
|
|
152
|
+
if (values && "count" in values) {
|
|
153
|
+
const category = resolvePluralCategory(currentLocale, Number(values.count), pluralRules);
|
|
154
|
+
const pluralKey = `${keyPath}_${category}`;
|
|
155
|
+
const pluralResult = lookupKey(currentLocale, namespace, pluralKey) ?? (fallbackLocale ? lookupKey(fallbackLocale, namespace, pluralKey) : void 0);
|
|
156
|
+
if (pluralResult) return interpolate(pluralResult, values);
|
|
157
|
+
}
|
|
158
|
+
const result = lookupKey(currentLocale, namespace, keyPath) ?? (fallbackLocale ? lookupKey(fallbackLocale, namespace, keyPath) : void 0);
|
|
159
|
+
if (result !== void 0) return interpolate(result, values);
|
|
160
|
+
if (onMissingKey) {
|
|
161
|
+
const custom = onMissingKey(currentLocale, key, namespace);
|
|
162
|
+
if (custom !== void 0) return custom;
|
|
163
|
+
}
|
|
164
|
+
return key;
|
|
165
|
+
}
|
|
166
|
+
const t = (key, values) => {
|
|
167
|
+
return resolveTranslation(key, values);
|
|
168
|
+
};
|
|
169
|
+
const loadNamespace = async (namespace, loc) => {
|
|
170
|
+
if (!loader) return;
|
|
171
|
+
const targetLocale = loc ?? locale.peek();
|
|
172
|
+
const cacheKey = `${targetLocale}:${namespace}`;
|
|
173
|
+
const nsMap = getNamespaceMap(targetLocale);
|
|
174
|
+
if (nsMap.has(namespace)) return;
|
|
175
|
+
const existing = pendingPromises.get(cacheKey);
|
|
176
|
+
if (existing) return existing;
|
|
177
|
+
pendingLoads.update(n => n + 1);
|
|
178
|
+
const promise = loader(targetLocale, namespace).then(dict => {
|
|
179
|
+
if (dict) {
|
|
180
|
+
nsMap.set(namespace, dict);
|
|
181
|
+
storeVersion.update(n => n + 1);
|
|
182
|
+
loadedNsVersion.update(n => n + 1);
|
|
183
|
+
}
|
|
184
|
+
}).finally(() => {
|
|
185
|
+
pendingPromises.delete(cacheKey);
|
|
186
|
+
pendingLoads.update(n => n - 1);
|
|
187
|
+
});
|
|
188
|
+
pendingPromises.set(cacheKey, promise);
|
|
189
|
+
return promise;
|
|
190
|
+
};
|
|
191
|
+
const exists = key => {
|
|
192
|
+
const currentLocale = locale.peek();
|
|
193
|
+
let namespace = defaultNamespace;
|
|
194
|
+
let keyPath = key;
|
|
195
|
+
const colonIndex = key.indexOf(":");
|
|
196
|
+
if (colonIndex > 0) {
|
|
197
|
+
namespace = key.slice(0, colonIndex);
|
|
198
|
+
keyPath = key.slice(colonIndex + 1);
|
|
199
|
+
}
|
|
200
|
+
return lookupKey(currentLocale, namespace, keyPath) !== void 0 || (fallbackLocale ? lookupKey(fallbackLocale, namespace, keyPath) !== void 0 : false);
|
|
201
|
+
};
|
|
202
|
+
const addMessages = (loc, messages, namespace) => {
|
|
203
|
+
const ns = namespace ?? defaultNamespace;
|
|
204
|
+
const nsMap = getNamespaceMap(loc);
|
|
205
|
+
const existing = nsMap.get(ns);
|
|
206
|
+
if (existing) deepMerge(existing, messages);else {
|
|
207
|
+
const cloned = {};
|
|
208
|
+
deepMerge(cloned, messages);
|
|
209
|
+
nsMap.set(ns, cloned);
|
|
210
|
+
}
|
|
211
|
+
storeVersion.update(n => n + 1);
|
|
212
|
+
loadedNsVersion.update(n => n + 1);
|
|
213
|
+
};
|
|
214
|
+
return {
|
|
215
|
+
t,
|
|
216
|
+
locale,
|
|
217
|
+
loadNamespace,
|
|
218
|
+
isLoading,
|
|
219
|
+
loadedNamespaces,
|
|
220
|
+
exists,
|
|
221
|
+
addMessages,
|
|
222
|
+
availableLocales
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/context.ts
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Provide an i18n instance to the component tree.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })
|
|
234
|
+
*
|
|
235
|
+
* // In JSX:
|
|
236
|
+
* <I18nProvider instance={i18n}>
|
|
237
|
+
* <App />
|
|
238
|
+
* </I18nProvider>
|
|
239
|
+
*/
|
|
240
|
+
function I18nProvider(props) {
|
|
241
|
+
pushContext(new Map([[I18nContext.id, props.instance]]));
|
|
242
|
+
onUnmount(() => popContext());
|
|
243
|
+
const ch = props.children;
|
|
244
|
+
return typeof ch === "function" ? ch() : ch;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Access the i18n instance from the nearest I18nProvider.
|
|
248
|
+
* Must be called within a component tree wrapped by I18nProvider.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* function Greeting() {
|
|
252
|
+
* const { t, locale } = useI18n()
|
|
253
|
+
* return <h1>{t('greeting', { name: 'World' })}</h1>
|
|
254
|
+
* }
|
|
255
|
+
*/
|
|
256
|
+
function useI18n() {
|
|
257
|
+
const instance = useContext(I18nContext);
|
|
258
|
+
if (!instance) throw new Error("[@pyreon/i18n] useI18n() must be used within an <I18nProvider>.");
|
|
259
|
+
return instance;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/trans.ts
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Parse a translated string into an array of plain text and rich tag segments.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* parseRichText("Hello <bold>world</bold>, click <link>here</link>")
|
|
270
|
+
* // → ["Hello ", { tag: "bold", children: "world" }, ", click ", { tag: "link", children: "here" }]
|
|
271
|
+
*/
|
|
272
|
+
function parseRichText(text) {
|
|
273
|
+
const parts = [];
|
|
274
|
+
let lastIndex = 0;
|
|
275
|
+
for (const match of text.matchAll(TAG_RE)) {
|
|
276
|
+
const before = text.slice(lastIndex, match.index);
|
|
277
|
+
if (before) parts.push(before);
|
|
278
|
+
parts.push({
|
|
279
|
+
tag: match[1],
|
|
280
|
+
children: match[2]
|
|
281
|
+
});
|
|
282
|
+
lastIndex = match.index + match[0].length;
|
|
283
|
+
}
|
|
284
|
+
const after = text.slice(lastIndex);
|
|
285
|
+
if (after) parts.push(after);
|
|
286
|
+
return parts;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Rich JSX interpolation component for translations.
|
|
290
|
+
*
|
|
291
|
+
* Allows embedding JSX components within translated strings using XML-like tags.
|
|
292
|
+
* The `t` function resolves the translation and interpolates `{{values}}` first,
|
|
293
|
+
* then `<tag>content</tag>` patterns are mapped to the provided components.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* // Translation: "You have <bold>{{count}}</bold> unread messages"
|
|
297
|
+
* const { t } = useI18n()
|
|
298
|
+
* <Trans
|
|
299
|
+
* t={t}
|
|
300
|
+
* i18nKey="messages.unread"
|
|
301
|
+
* values={{ count: 5 }}
|
|
302
|
+
* components={{
|
|
303
|
+
* bold: (children) => <strong>{children}</strong>,
|
|
304
|
+
* }}
|
|
305
|
+
* />
|
|
306
|
+
* // Renders: You have <strong>5</strong> unread messages
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* // Translation: "Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>"
|
|
310
|
+
* <Trans
|
|
311
|
+
* t={t}
|
|
312
|
+
* i18nKey="legal"
|
|
313
|
+
* components={{
|
|
314
|
+
* terms: (children) => <a href="/terms">{children}</a>,
|
|
315
|
+
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
316
|
+
* }}
|
|
317
|
+
* />
|
|
318
|
+
*/
|
|
319
|
+
function Trans(props) {
|
|
320
|
+
const translated = props.t(props.i18nKey, props.values);
|
|
321
|
+
if (!props.components) return translated;
|
|
322
|
+
const parts = parseRichText(translated);
|
|
323
|
+
if (parts.length === 1 && typeof parts[0] === "string") return parts[0];
|
|
324
|
+
return h(Fragment, null, ...parts.map(part => {
|
|
325
|
+
if (typeof part === "string") return part;
|
|
326
|
+
const component = props.components[part.tag];
|
|
327
|
+
if (!component) return part.children;
|
|
328
|
+
return component(part.children);
|
|
329
|
+
}));
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
//#endregion
|
|
333
|
+
export { I18nContext, I18nProvider, Trans, createI18n, interpolate, parseRichText, resolvePluralCategory, useI18n };
|
|
334
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/interpolation.ts","../../src/pluralization.ts","../../src/create-i18n.ts","../../src/context.ts","../../src/trans.ts"],"mappings":";;;;;;;;;;AASA,SAAgB,WAAA,CACd,QAAA,EACA,MAAA,EACQ;EACR,IAAI,CAAC,MAAA,IAAU,CAAC,QAAA,CAAS,QAAA,CAAS,IAAA,CAAK,EAAE,OAAO,QAAA;EAChD,OAAO,QAAA,CAAS,OAAA,CAAQ,gBAAA,EAAA,CAAmB,CAAA,EAAG,GAAA,KAAgB;IAC5D,MAAM,OAAA,GAAU,GAAA,CAAI,IAAA,CAAA,CAAM;IAC1B,MAAM,KAAA,GAAQ,MAAA,CAAO,OAAA,CAAA;IACrB,IAAI,KAAA,KAAU,KAAA,CAAA,EAAW,OAAO,KAAK,OAAA,IAAQ;IAE7C,IAAI;MACF,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,IAAA,GAC1C,IAAA,CAAK,SAAA,CAAU,KAAA,CAAM,GACrB,GAAG,KAAA,EAAA;YACD;MACN,OAAO,KAAK,OAAA,IAAQ;;IAEtB;;;;;;;;;;;AClBJ,SAAgB,qBAAA,CACd,MAAA,EACA,KAAA,EACA,WAAA,EACQ;EAER,IAAI,WAAA,GAAc,MAAA,CAAA,EAChB,OAAO,WAAA,CAAY,MAAA,CAAA,CAAQ,KAAA,CAAM;EAInC,IAAI,OAAO,IAAA,KAAS,WAAA,IAAe,IAAA,CAAK,WAAA,EACtC,IAAI;IAEF,OADW,IAAI,IAAA,CAAK,WAAA,CAAY,MAAA,CAAO,CAC7B,MAAA,CAAO,KAAA,CAAM;UACjB,CAAA;EAMV,OAAO,KAAA,KAAU,CAAA,GAAI,KAAA,GAAQ,OAAA;;;;;;;;;ACf/B,SAAS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CAAM,KAAA,GAAS,KAAA,CAAM,CAAA,CAAA,CAAG,MAAA;;EAGtC,MAAM,KAAA,GAAQ,IAAA,CAAK,KAAA,CAAM,SAAA,CAAU;EACnC,IAAI,KAAA,EAAO,KAAA,CAAM,IAAA,CAAK,KAAA,CAAM;EAE5B,OAAO,KAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DT,SAAgB,KAAA,CAAM,KAAA,EAAmC;EACvD,MAAM,UAAA,GAAa,KAAA,CAAM,CAAA,CAAE,KAAA,CAAM,OAAA,EAAS,KAAA,CAAM,MAAA,CAAO;EAEvD,IAAI,CAAC,KAAA,CAAM,UAAA,EAAY,OAAO,UAAA;EAE9B,MAAM,KAAA,GAAQ,aAAA,CAAc,UAAA,CAAW;EAGvC,IAAI,KAAA,CAAM,MAAA,KAAW,CAAA,IAAK,OAAO,KAAA,CAAM,CAAA,CAAA,KAAO,QAAA,EAAU,OAAO,KAAA,CAAM,CAAA,CAAA;EAUrE,OAAO,CAAA,CAAE,QAAA,EAAU,IAAA,EAAM,GARR,KAAA,CAAM,GAAA,CAAK,IAAA,IAAS;IACnC,IAAI,OAAO,IAAA,KAAS,QAAA,EAAU,OAAO,IAAA;IACrC,MAAM,SAAA,GAAY,KAAA,CAAM,UAAA,CAAY,IAAA,CAAK,GAAA,CAAA;IAEzC,IAAI,CAAC,SAAA,EAAW,OAAO,IAAA,CAAK,QAAA;IAC5B,OAAO,SAAA,CAAU,IAAA,CAAK,QAAA,CAAS;IAC/B,CAEmC"}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { Computed, Signal } from "@pyreon/reactivity";
|
|
2
|
+
import * as _pyreon_core0 from "@pyreon/core";
|
|
3
|
+
import { Props, VNode, VNodeChild } from "@pyreon/core";
|
|
4
|
+
|
|
5
|
+
//#region src/types.d.ts
|
|
6
|
+
/** A nested dictionary of translation strings. */
|
|
7
|
+
type TranslationDictionary = {
|
|
8
|
+
[key: string]: string | TranslationDictionary;
|
|
9
|
+
};
|
|
10
|
+
/** Map of locale → dictionary (or namespace → dictionary). */
|
|
11
|
+
type TranslationMessages = Record<string, TranslationDictionary>;
|
|
12
|
+
/**
|
|
13
|
+
* Async function that loads translations for a locale and namespace.
|
|
14
|
+
* Return the dictionary for that namespace, or undefined if not found.
|
|
15
|
+
*/
|
|
16
|
+
type NamespaceLoader = (locale: string, namespace: string) => Promise<TranslationDictionary | undefined>;
|
|
17
|
+
/** Interpolation values for translation strings. */
|
|
18
|
+
type InterpolationValues = Record<string, string | number>;
|
|
19
|
+
/** Pluralization rules map — locale → function that picks the plural form. */
|
|
20
|
+
type PluralRules = Record<string, (count: number) => string>;
|
|
21
|
+
/** Options for creating an i18n instance. */
|
|
22
|
+
interface I18nOptions {
|
|
23
|
+
/** The initial locale (e.g. "en"). */
|
|
24
|
+
locale: string;
|
|
25
|
+
/** Fallback locale used when a key is missing in the active locale. */
|
|
26
|
+
fallbackLocale?: string;
|
|
27
|
+
/** Static messages keyed by locale. */
|
|
28
|
+
messages?: Record<string, TranslationDictionary>;
|
|
29
|
+
/**
|
|
30
|
+
* Async loader for namespace-based translation loading.
|
|
31
|
+
* Called with (locale, namespace) when `loadNamespace()` is invoked.
|
|
32
|
+
*/
|
|
33
|
+
loader?: NamespaceLoader;
|
|
34
|
+
/**
|
|
35
|
+
* Default namespace used when `t()` is called without a namespace prefix.
|
|
36
|
+
* Defaults to "common".
|
|
37
|
+
*/
|
|
38
|
+
defaultNamespace?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Custom plural rules per locale.
|
|
41
|
+
* If not provided, uses `Intl.PluralRules` where available.
|
|
42
|
+
*/
|
|
43
|
+
pluralRules?: PluralRules;
|
|
44
|
+
/**
|
|
45
|
+
* Missing key handler — called when a translation key is not found.
|
|
46
|
+
* Useful for logging, reporting, or returning a custom fallback.
|
|
47
|
+
*/
|
|
48
|
+
onMissingKey?: (locale: string, key: string, namespace?: string) => string | undefined;
|
|
49
|
+
}
|
|
50
|
+
/** The public i18n instance returned by `createI18n()`. */
|
|
51
|
+
interface I18nInstance {
|
|
52
|
+
/**
|
|
53
|
+
* Translate a key with optional interpolation.
|
|
54
|
+
* Reads the current locale reactively — re-evaluates in effects/computeds.
|
|
55
|
+
*
|
|
56
|
+
* Key format: "key" (uses default namespace) or "namespace:key".
|
|
57
|
+
* Nested keys use dots: "user.greeting" or "auth:errors.invalid".
|
|
58
|
+
*
|
|
59
|
+
* Interpolation: "Hello {{name}}" + { name: "Alice" } → "Hello Alice"
|
|
60
|
+
* Pluralization: key with "_one", "_other" etc. suffixes + { count: N }
|
|
61
|
+
*/
|
|
62
|
+
t: (key: string, values?: InterpolationValues) => string;
|
|
63
|
+
/** Current locale (reactive signal). */
|
|
64
|
+
locale: Signal<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Load a namespace's translations for the given locale (or current locale).
|
|
67
|
+
* Returns a promise that resolves when loading is complete.
|
|
68
|
+
*/
|
|
69
|
+
loadNamespace: (namespace: string, locale?: string) => Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Whether any namespace is currently being loaded.
|
|
72
|
+
*/
|
|
73
|
+
isLoading: Computed<boolean>;
|
|
74
|
+
/**
|
|
75
|
+
* Set of namespaces that have been loaded for the current locale.
|
|
76
|
+
*/
|
|
77
|
+
loadedNamespaces: Computed<Set<string>>;
|
|
78
|
+
/**
|
|
79
|
+
* Check if a translation key exists in the current locale.
|
|
80
|
+
*/
|
|
81
|
+
exists: (key: string) => boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Add translations for a locale (merged with existing).
|
|
84
|
+
* Useful for adding translations at runtime without async loading.
|
|
85
|
+
*/
|
|
86
|
+
addMessages: (locale: string, messages: TranslationDictionary, namespace?: string) => void;
|
|
87
|
+
/**
|
|
88
|
+
* Get all available locales (those with any registered messages).
|
|
89
|
+
*/
|
|
90
|
+
availableLocales: Computed<string[]>;
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/create-i18n.d.ts
|
|
94
|
+
/**
|
|
95
|
+
* Create a reactive i18n instance.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* const i18n = createI18n({
|
|
99
|
+
* locale: 'en',
|
|
100
|
+
* fallbackLocale: 'en',
|
|
101
|
+
* messages: {
|
|
102
|
+
* en: { greeting: 'Hello {{name}}!' },
|
|
103
|
+
* de: { greeting: 'Hallo {{name}}!' },
|
|
104
|
+
* },
|
|
105
|
+
* })
|
|
106
|
+
*
|
|
107
|
+
* // Reactive translation — re-evaluates on locale change
|
|
108
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hello Alice!"
|
|
109
|
+
* i18n.locale.set('de')
|
|
110
|
+
* i18n.t('greeting', { name: 'Alice' }) // "Hallo Alice!"
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* // Async namespace loading
|
|
114
|
+
* const i18n = createI18n({
|
|
115
|
+
* locale: 'en',
|
|
116
|
+
* loader: async (locale, namespace) => {
|
|
117
|
+
* const mod = await import(`./locales/${locale}/${namespace}.json`)
|
|
118
|
+
* return mod.default
|
|
119
|
+
* },
|
|
120
|
+
* })
|
|
121
|
+
* await i18n.loadNamespace('auth')
|
|
122
|
+
* i18n.t('auth:errors.invalid') // looks up "errors.invalid" in "auth" namespace
|
|
123
|
+
*/
|
|
124
|
+
declare function createI18n(options: I18nOptions): I18nInstance;
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/interpolation.d.ts
|
|
127
|
+
/**
|
|
128
|
+
* Replace `{{key}}` placeholders in a string with values from the given record.
|
|
129
|
+
* Supports optional whitespace inside braces: `{{ name }}` works too.
|
|
130
|
+
* Unmatched placeholders are left as-is.
|
|
131
|
+
*/
|
|
132
|
+
declare function interpolate(template: string, values?: InterpolationValues): string;
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/pluralization.d.ts
|
|
135
|
+
/**
|
|
136
|
+
* Resolve the plural category for a given count and locale.
|
|
137
|
+
*
|
|
138
|
+
* Uses custom rules if provided, otherwise falls back to `Intl.PluralRules`.
|
|
139
|
+
* Returns CLDR plural categories: "zero", "one", "two", "few", "many", "other".
|
|
140
|
+
*/
|
|
141
|
+
declare function resolvePluralCategory(locale: string, count: number, customRules?: PluralRules): string;
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region src/context.d.ts
|
|
144
|
+
declare const I18nContext: _pyreon_core0.Context<I18nInstance | null>;
|
|
145
|
+
interface I18nProviderProps extends Props {
|
|
146
|
+
instance: I18nInstance;
|
|
147
|
+
children?: VNodeChild;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Provide an i18n instance to the component tree.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* const i18n = createI18n({ locale: 'en', messages: { en: { hello: 'Hello' } } })
|
|
154
|
+
*
|
|
155
|
+
* // In JSX:
|
|
156
|
+
* <I18nProvider instance={i18n}>
|
|
157
|
+
* <App />
|
|
158
|
+
* </I18nProvider>
|
|
159
|
+
*/
|
|
160
|
+
declare function I18nProvider(props: I18nProviderProps): VNode;
|
|
161
|
+
/**
|
|
162
|
+
* Access the i18n instance from the nearest I18nProvider.
|
|
163
|
+
* Must be called within a component tree wrapped by I18nProvider.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* function Greeting() {
|
|
167
|
+
* const { t, locale } = useI18n()
|
|
168
|
+
* return <h1>{t('greeting', { name: 'World' })}</h1>
|
|
169
|
+
* }
|
|
170
|
+
*/
|
|
171
|
+
declare function useI18n(): I18nInstance;
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/trans.d.ts
|
|
174
|
+
interface RichPart {
|
|
175
|
+
tag: string;
|
|
176
|
+
children: string;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Parse a translated string into an array of plain text and rich tag segments.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* parseRichText("Hello <bold>world</bold>, click <link>here</link>")
|
|
183
|
+
* // → ["Hello ", { tag: "bold", children: "world" }, ", click ", { tag: "link", children: "here" }]
|
|
184
|
+
*/
|
|
185
|
+
declare function parseRichText(text: string): (string | RichPart)[];
|
|
186
|
+
interface TransProps extends Props {
|
|
187
|
+
/** Translation key (supports namespace:key syntax). */
|
|
188
|
+
i18nKey: string;
|
|
189
|
+
/** Interpolation values for {{placeholder}} syntax. */
|
|
190
|
+
values?: InterpolationValues;
|
|
191
|
+
/**
|
|
192
|
+
* Component map for rich interpolation.
|
|
193
|
+
* Keys match tag names in the translation string.
|
|
194
|
+
* Values are component functions: `(children: any) => VNode`
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* // Translation: "Read the <terms>terms</terms> and <privacy>policy</privacy>"
|
|
198
|
+
* components={{
|
|
199
|
+
* terms: (children) => <a href="/terms">{children}</a>,
|
|
200
|
+
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
201
|
+
* }}
|
|
202
|
+
*/
|
|
203
|
+
components?: Record<string, (children: any) => any>;
|
|
204
|
+
/**
|
|
205
|
+
* The i18n instance's `t` function.
|
|
206
|
+
* Can be obtained from `useI18n()` or passed directly.
|
|
207
|
+
*/
|
|
208
|
+
t: (key: string, values?: InterpolationValues) => string;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Rich JSX interpolation component for translations.
|
|
212
|
+
*
|
|
213
|
+
* Allows embedding JSX components within translated strings using XML-like tags.
|
|
214
|
+
* The `t` function resolves the translation and interpolates `{{values}}` first,
|
|
215
|
+
* then `<tag>content</tag>` patterns are mapped to the provided components.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* // Translation: "You have <bold>{{count}}</bold> unread messages"
|
|
219
|
+
* const { t } = useI18n()
|
|
220
|
+
* <Trans
|
|
221
|
+
* t={t}
|
|
222
|
+
* i18nKey="messages.unread"
|
|
223
|
+
* values={{ count: 5 }}
|
|
224
|
+
* components={{
|
|
225
|
+
* bold: (children) => <strong>{children}</strong>,
|
|
226
|
+
* }}
|
|
227
|
+
* />
|
|
228
|
+
* // Renders: You have <strong>5</strong> unread messages
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* // Translation: "Read our <terms>terms of service</terms> and <privacy>privacy policy</privacy>"
|
|
232
|
+
* <Trans
|
|
233
|
+
* t={t}
|
|
234
|
+
* i18nKey="legal"
|
|
235
|
+
* components={{
|
|
236
|
+
* terms: (children) => <a href="/terms">{children}</a>,
|
|
237
|
+
* privacy: (children) => <a href="/privacy">{children}</a>,
|
|
238
|
+
* }}
|
|
239
|
+
* />
|
|
240
|
+
*/
|
|
241
|
+
declare function Trans(props: TransProps): VNode | string;
|
|
242
|
+
//#endregion
|
|
243
|
+
export { I18nContext, type I18nInstance, type I18nOptions, I18nProvider, type I18nProviderProps, type InterpolationValues, type NamespaceLoader, type PluralRules, Trans, type TransProps, type TranslationDictionary, type TranslationMessages, createI18n, interpolate, parseRichText, resolvePluralCategory, useI18n };
|
|
244
|
+
//# sourceMappingURL=index2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index2.d.ts","names":[],"sources":["../../src/types.ts","../../src/create-i18n.ts","../../src/interpolation.ts","../../src/pluralization.ts","../../src/context.ts","../../src/trans.ts"],"mappings":";;;;;;KAGY,qBAAA;EAAA,CACT,GAAA,oBAAuB,qBAAA;AAAA;AAD1B;AAAA,KAKY,mBAAA,GAAsB,MAAA,SAAe,qBAAA;;;;AAAjD;KAMY,eAAA,IACV,MAAA,UACA,SAAA,aACG,OAAA,CAAQ,qBAAA;;KAGD,mBAAA,GAAsB,MAAA;;KAGtB,WAAA,GAAc,MAAA,UAAgB,KAAA;;UAGzB,WAAA;EATL;EAWV,MAAA;EAZA;EAcA,cAAA;EAbW;EAeX,QAAA,GAAW,MAAA,SAAe,qBAAA;EAfM;AAGlC;;;EAiBE,MAAA,GAAS,eAAA;EAjB6B;AAGxC;;;EAmBE,gBAAA;EAnBqD;AAGvD;;;EAqBE,WAAA,GAAc,WAAA;EAfH;;;;EAoBX,YAAA,IACE,MAA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CAAA,GAAI,GAAA,UAAa,MAAA,GAAS,mBAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;ALG5B;;;;;;;;;iBK+BgB,KAAA,CAAM,KAAA,EAAO,UAAA,GAAa,KAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pyreon/i18n",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Reactive internationalization for Pyreon with async namespace loading",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/pyreon/fundamentals.git",
|
|
9
|
+
"directory": "packages/i18n"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/pyreon/fundamentals/tree/main/packages/i18n#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/pyreon/fundamentals/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"src",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"main": "./lib/index.js",
|
|
27
|
+
"module": "./lib/index.js",
|
|
28
|
+
"types": "./lib/types/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"bun": "./src/index.ts",
|
|
32
|
+
"import": "./lib/index.js",
|
|
33
|
+
"types": "./lib/types/index.d.ts"
|
|
34
|
+
},
|
|
35
|
+
"./devtools": {
|
|
36
|
+
"bun": "./src/devtools.ts",
|
|
37
|
+
"import": "./lib/devtools.js",
|
|
38
|
+
"types": "./lib/types/devtools.d.ts"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "vl_rolldown_build",
|
|
43
|
+
"dev": "vl_rolldown_build-watch",
|
|
44
|
+
"test": "vitest run",
|
|
45
|
+
"typecheck": "tsc --noEmit"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@pyreon/core": "^0.2.1",
|
|
49
|
+
"@pyreon/reactivity": "^0.2.1"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@happy-dom/global-registrator": "^20.8.3",
|
|
53
|
+
"@pyreon/core": "^0.2.1",
|
|
54
|
+
"@pyreon/reactivity": "^0.2.1",
|
|
55
|
+
"@pyreon/runtime-dom": "^0.2.1"
|
|
56
|
+
}
|
|
57
|
+
}
|