@coherent.js/i18n 1.0.0-rc.2 → 1.0.0-rc.4
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 +18 -24
- package/package.json +6 -3
- package/types/index.d.ts +10 -38
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ Internationalization utilities
|
|
|
30
30
|
### Example Usage
|
|
31
31
|
|
|
32
32
|
```javascript
|
|
33
|
-
import {
|
|
33
|
+
import { createTranslator, createFormatters, createLocaleManager } from '@coherent.js/i18n';
|
|
34
34
|
```
|
|
35
35
|
|
|
36
36
|
> **Note**: All exports are tree-shakable. Import only what you need for optimal bundle size.
|
|
@@ -38,44 +38,38 @@ import { createI18n } from '@coherent.js/i18n';
|
|
|
38
38
|
|
|
39
39
|
JavaScript (ESM):
|
|
40
40
|
```js
|
|
41
|
-
import { createTranslator } from '@coherent.js/i18n
|
|
41
|
+
import { createTranslator } from '@coherent.js/i18n';
|
|
42
42
|
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}, { locale: 'en' });
|
|
43
|
+
const translator = createTranslator({ defaultLocale: 'en' });
|
|
44
|
+
translator.addTranslations('en', { hello: 'Hello, {{name}}!' });
|
|
45
|
+
translator.addTranslations('fr', { hello: 'Bonjour, {{name}} !' });
|
|
47
46
|
|
|
48
|
-
console.log(t('hello', { name: 'Coherent' }));
|
|
47
|
+
console.log(translator.t('hello', { name: 'Coherent' })); // Hello, Coherent!
|
|
48
|
+
translator.setLocale('fr');
|
|
49
|
+
console.log(translator.t('hello', { name: 'Coherent' })); // Bonjour, Coherent !
|
|
49
50
|
```
|
|
50
51
|
|
|
51
52
|
TypeScript:
|
|
52
53
|
```ts
|
|
53
|
-
import { createTranslator } from '@coherent.js/i18n
|
|
54
|
+
import { createTranslator } from '@coherent.js/i18n';
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
};
|
|
56
|
+
const translator = createTranslator({ defaultLocale: 'en' });
|
|
57
|
+
translator.addTranslations('en', { hello: 'Hello, {{name}}!' });
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
en: { hello: 'Hello, {name}!' },
|
|
61
|
-
fr: { hello: 'Bonjour, {name} !' }
|
|
62
|
-
}, { locale: 'en' });
|
|
63
|
-
|
|
64
|
-
console.log(t('hello', { name: 'TS' }));
|
|
59
|
+
console.log(translator.t('hello', { name: 'TS' }));
|
|
65
60
|
```
|
|
66
61
|
|
|
67
62
|
### Formatters and locale
|
|
68
63
|
|
|
69
64
|
```js
|
|
70
|
-
import { createFormatters } from '@coherent.js/i18n
|
|
71
|
-
import { createLocaleManager } from '@coherent.js/i18n/locale';
|
|
65
|
+
import { createFormatters, createLocaleManager } from '@coherent.js/i18n';
|
|
72
66
|
|
|
73
|
-
const
|
|
74
|
-
const fmt = createFormatters(
|
|
67
|
+
const locales = createLocaleManager({ defaultLocale: 'en-US' });
|
|
68
|
+
const fmt = createFormatters(locales.getLocale());
|
|
75
69
|
|
|
76
|
-
fmt.date(new Date());
|
|
77
|
-
fmt.number(12345.678);
|
|
78
|
-
fmt.currency(1999.99
|
|
70
|
+
fmt.date.format(new Date()); // 1/15/2026
|
|
71
|
+
fmt.number.format(12345.678); // 12,345.678
|
|
72
|
+
fmt.currency.format(1999.99); // $1,999.99
|
|
79
73
|
```
|
|
80
74
|
|
|
81
75
|
## Exports
|
package/package.json
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@coherent.js/i18n",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.4",
|
|
4
4
|
"description": "Internationalization support for Coherent.js applications",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"exports": {
|
|
8
|
-
".":
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./types/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
9
12
|
"./translator": "./dist/translator.js",
|
|
10
13
|
"./formatters": "./dist/formatters.js",
|
|
11
14
|
"./locale": "./dist/locale.js"
|
|
@@ -20,7 +23,7 @@
|
|
|
20
23
|
"author": "Coherent.js Team",
|
|
21
24
|
"license": "MIT",
|
|
22
25
|
"peerDependencies": {
|
|
23
|
-
"@coherent.js/core": "1.0.0-rc.
|
|
26
|
+
"@coherent.js/core": "1.0.0-rc.4"
|
|
24
27
|
},
|
|
25
28
|
"repository": {
|
|
26
29
|
"type": "git",
|
package/types/index.d.ts
CHANGED
|
@@ -58,14 +58,6 @@ export interface TranslationFunction {
|
|
|
58
58
|
(key: TranslationKey, params: Record<string, string | number>, count: number): string;
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
/**
|
|
62
|
-
* Create a typed translator for a specific message shape
|
|
63
|
-
* @template T - The translation messages type
|
|
64
|
-
*/
|
|
65
|
-
export function createTypedTranslator<T extends TranslationMessages>(
|
|
66
|
-
messages: T
|
|
67
|
-
): (key: FlattenKeys<T>, params?: Record<string, string | number>) => string;
|
|
68
|
-
|
|
69
61
|
// ============================================================================
|
|
70
62
|
// I18n Configuration
|
|
71
63
|
// ============================================================================
|
|
@@ -179,20 +171,6 @@ export interface I18nInstance {
|
|
|
179
171
|
getMessages(): TranslationMessages;
|
|
180
172
|
}
|
|
181
173
|
|
|
182
|
-
/**
|
|
183
|
-
* Create an i18n instance
|
|
184
|
-
*/
|
|
185
|
-
export function createI18n(config: I18nConfig): I18nInstance;
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Hook to get the translation function (for component usage)
|
|
189
|
-
*/
|
|
190
|
-
export function useTranslation(): TranslationFunction;
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Hook to get and set the current locale
|
|
194
|
-
*/
|
|
195
|
-
export function useLocale(): [string, (locale: string) => Promise<void>];
|
|
196
174
|
|
|
197
175
|
// ============================================================================
|
|
198
176
|
// Translator Class
|
|
@@ -447,6 +425,16 @@ export function detectLocale(): string;
|
|
|
447
425
|
/**
|
|
448
426
|
* Normalize a locale code (e.g., 'en_US' -> 'en-US')
|
|
449
427
|
*/
|
|
428
|
+
export function getLocaleDirection(locale: string): 'ltr' | 'rtl';
|
|
429
|
+
|
|
430
|
+
export function getLocaleDisplayName(locale: string, displayLocale?: string): string;
|
|
431
|
+
|
|
432
|
+
export function getSupportedLocales(): string[];
|
|
433
|
+
|
|
434
|
+
export function matchLocale(requestedLocale: string, availableLocales: string[], defaultLocale?: string): string;
|
|
435
|
+
|
|
436
|
+
export function parseLocale(locale: string): { language: string; region: string | null; script: string | null; full: string };
|
|
437
|
+
|
|
450
438
|
export function normalizeLocale(locale: string): string;
|
|
451
439
|
|
|
452
440
|
/**
|
|
@@ -458,19 +446,3 @@ export function isRTL(locale: string): boolean;
|
|
|
458
446
|
// Component Helpers
|
|
459
447
|
// ============================================================================
|
|
460
448
|
|
|
461
|
-
/**
|
|
462
|
-
* Create a translated component node
|
|
463
|
-
*/
|
|
464
|
-
export function translatedNode(
|
|
465
|
-
key: TranslationKey,
|
|
466
|
-
tagName?: string,
|
|
467
|
-
params?: Record<string, string | number>
|
|
468
|
-
): CoherentNode;
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* Create an i18n context provider component
|
|
472
|
-
*/
|
|
473
|
-
export function createI18nProvider(
|
|
474
|
-
i18n: I18nInstance,
|
|
475
|
-
children: CoherentNode
|
|
476
|
-
): CoherentNode;
|