@better-intl/core 0.1.0 → 0.1.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/README.md +89 -0
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @better-intl/core
|
|
2
|
+
|
|
3
|
+
Core runtime for **better-intl** — hash, catalog, interpolation, pluralization.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @better-intl/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Translation runtime
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createIntlRuntime } from "@better-intl/core";
|
|
17
|
+
|
|
18
|
+
const runtime = createIntlRuntime("en", {
|
|
19
|
+
greeting: "Hello {name}",
|
|
20
|
+
items: "{count, plural, one {# item} other {# items}}",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
runtime.t("greeting", { name: "Alice" }); // "Hello Alice"
|
|
24
|
+
runtime.t("items", { count: 3 }); // "3 items"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Catalog
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { Catalog } from "@better-intl/core";
|
|
31
|
+
|
|
32
|
+
const catalog = new Catalog("en");
|
|
33
|
+
catalog.load("en", { greeting: "Hello" });
|
|
34
|
+
catalog.load("pt-BR", { greeting: "Olá" });
|
|
35
|
+
|
|
36
|
+
catalog.resolve("greeting", "pt-BR"); // "Olá"
|
|
37
|
+
catalog.has("greeting", "en"); // true
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Interpolation
|
|
41
|
+
|
|
42
|
+
Supports ICU-lite syntax — variables, plurals, and select:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { interpolate } from "@better-intl/core";
|
|
46
|
+
|
|
47
|
+
interpolate("Hello {name}", { name: "Alice" });
|
|
48
|
+
// "Hello Alice"
|
|
49
|
+
|
|
50
|
+
interpolate("{count, plural, one {# item} other {# items}}", { count: 5 });
|
|
51
|
+
// "5 items"
|
|
52
|
+
|
|
53
|
+
interpolate("{gender, select, male {He} female {She} other {They}}", { gender: "female" });
|
|
54
|
+
// "She"
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Locale negotiation
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { negotiateLocale, parseAcceptLanguage } from "@better-intl/core";
|
|
61
|
+
|
|
62
|
+
const preferred = parseAcceptLanguage("pt-BR,pt;q=0.9,en;q=0.8");
|
|
63
|
+
negotiateLocale(preferred, ["en", "pt-BR", "es"], "en"); // "pt-BR"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Stable ID generation
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { generateId } from "@better-intl/core";
|
|
70
|
+
|
|
71
|
+
generateId({ text: "Hello world", filePath: "src/Hero.tsx" });
|
|
72
|
+
// "0rr2b7c" — deterministic, short, collision-resistant
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## API
|
|
76
|
+
|
|
77
|
+
| Export | Description |
|
|
78
|
+
|--------|-------------|
|
|
79
|
+
| `createIntlRuntime(defaultLocale, messages?)` | Creates runtime with `t()` function |
|
|
80
|
+
| `Catalog` | Multi-locale message store |
|
|
81
|
+
| `generateId(input)` | Stable FNV-1a hash for message IDs |
|
|
82
|
+
| `interpolate(message, values?)` | ICU-lite interpolation |
|
|
83
|
+
| `negotiateLocale(preferred, supported, default)` | Best locale from Accept-Language |
|
|
84
|
+
| `parseAcceptLanguage(header)` | Parse header into ordered list |
|
|
85
|
+
| `DEFAULT_CONFIG` | Default `BetterIntlConfig` |
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-intl/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Core runtime for better-intl — hash, catalog, interpolation, pluralization",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
18
19
|
],
|
|
19
20
|
"sideEffects": false,
|
|
20
21
|
"license": "MIT",
|