@ngx-runtime-i18n/core 1.0.2 → 1.0.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 +64 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,31 +1,80 @@
|
|
|
1
|
-
# @ngx-runtime-i18n
|
|
1
|
+
# @ngx-runtime-i18n/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework‑agnostic primitives for runtime internationalisation:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
- Tiny, dependency‑free **ICU‑lite** formatter (interpolation + basic `plural`).
|
|
6
|
+
- Shared types used by the Angular wrapper.
|
|
7
|
+
|
|
8
|
+
This package is designed to be used directly or via `@ngx-runtime-i18n/angular`.
|
|
9
|
+
|
|
10
|
+
---
|
|
6
11
|
|
|
7
12
|
## Install
|
|
8
13
|
|
|
9
14
|
```bash
|
|
10
|
-
npm i @ngx-runtime-i18n
|
|
15
|
+
npm i @ngx-runtime-i18n/core
|
|
11
16
|
```
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
14
21
|
|
|
15
22
|
```ts
|
|
16
|
-
import { formatIcu } from '@ngx-runtime-i18n/core';
|
|
23
|
+
import { formatIcu, type Catalog } from '@ngx-runtime-i18n/core';
|
|
17
24
|
|
|
18
|
-
const catalog = {
|
|
19
|
-
hello: 'Hello {name}!',
|
|
20
|
-
|
|
25
|
+
const catalog: Catalog = {
|
|
26
|
+
hello: { user: 'Hello, {name}!' },
|
|
27
|
+
cart: { items: '{count, plural, one {1 item} other {# items}}' },
|
|
21
28
|
};
|
|
22
29
|
|
|
23
|
-
formatIcu('en', 'hello', catalog, { name: 'Ashwin' });
|
|
24
|
-
//
|
|
30
|
+
formatIcu('en', 'hello.user', catalog, { name: 'Ashwin' }); // "Hello, Ashwin!"
|
|
31
|
+
formatIcu('en', 'cart.items', catalog, { count: 2 }); // "2 items"
|
|
25
32
|
```
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
- `key` supports dotted paths (e.g., `hello.user`).
|
|
35
|
+
- `plural` supports `one`, `other`, and exact matches like `=0`, `=2`.
|
|
36
|
+
- The function is pure and side‑effect free.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## API
|
|
41
|
+
|
|
42
|
+
### `formatIcu(lang, key, catalog, params?, onMissingKey?)`
|
|
43
|
+
|
|
44
|
+
- **`lang: string`** — current language (for plural rules and future features).
|
|
45
|
+
- **`key: string`** — dotted path into the catalog.
|
|
46
|
+
- **`catalog: Catalog`** — a nested object of strings/objects.
|
|
47
|
+
- **`params?: Record<string, unknown>`** — interpolation values.
|
|
48
|
+
- **`onMissingKey?: (key: string) => string`** — transform for missing keys (defaults to returning the key).
|
|
49
|
+
|
|
50
|
+
### Types
|
|
51
|
+
|
|
52
|
+
- **`Catalog`** — `Record<string, unknown>` (nested object).
|
|
53
|
+
- **`RuntimeI18nConfig`** — shape shared with the Angular wrapper for consistency.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## Catalog structure
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"hello": { "user": "Hello, {name}!" },
|
|
62
|
+
"cart": { "items": "{count, plural, one {1 item} other {# items}}" }
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
> Keep catalogs per language (e.g., `en.json`, `hi.json`).
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Pitfalls & Notes
|
|
71
|
+
|
|
72
|
+
- Not a full ICU implementation; aims to cover common 80% with a tiny footprint.
|
|
73
|
+
- If you need Angular binding or SSR helpers, prefer `@ngx-runtime-i18n/angular`.
|
|
74
|
+
- Keep your catalogs **flat-ish and predictable** to avoid fragile deep paths.
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## License
|
|
28
79
|
|
|
29
|
-
|
|
30
|
-
- Type definitions (`Catalog`, `RuntimeI18nConfig`)
|
|
31
|
-
- Utilities for Angular bindings (`@ngx-runtime-i18n/angular`)
|
|
80
|
+
MIT
|