@opentf/afw-i18n 1.27.0 → 1.29.0

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.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +135 -0
  3. package/i18n.js +0 -0
  4. package/package.json +17 -5
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Open Tech Foundation and contributors
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,135 @@
1
+ <div align="center">
2
+
3
+ # @opentf/afw-i18n
4
+
5
+ ***ICU messages, `Intl` formatters and locale routing for AFW***
6
+
7
+ [Docs](https://afw.opentechf.org/packages/afw-i18n) | [API](https://afw.opentechf.org/api/afw-i18n) | [Changelog](https://github.com/Open-Tech-Foundation/afw/blob/main/packages/afw-i18n/CHANGELOG.md) | [GitHub](https://github.com/Open-Tech-Foundation/afw)
8
+
9
+ </div>
10
+
11
+ <div align="right">
12
+
13
+ *An [Open Tech Foundation](https://opentechf.org/) project*
14
+
15
+ </div>
16
+
17
+ > The locale lives in the URL (`/fr/about`), the router exposes it as a signal, and
18
+ > `t()` reads that signal. A translation in your markup is one binding, so changing
19
+ > locale updates exactly the translated text.
20
+
21
+ ```jsx
22
+ import { createI18n, fmt, t } from "@opentf/afw-i18n";
23
+
24
+ createI18n({
25
+ locales: ["en", "fr"],
26
+ defaultLocale: "en",
27
+ messages: {
28
+ en: { cart: "{count, plural, =0 {Your cart is empty} one {# item} other {# items}}" },
29
+ fr: { cart: "{count, plural, =0 {Votre panier est vide} one {# article} other {# articles}}" },
30
+ },
31
+ });
32
+
33
+ export default function Cart(props) {
34
+ return (
35
+ <p>
36
+ {t("cart", { count: props.items.length })} · {fmt.currency(props.total, "EUR")}
37
+ </p>
38
+ );
39
+ }
40
+ ```
41
+
42
+ On `/cart` this renders "3 items · €42.00"; on `/fr/cart`, "3 articles · 42,00 €".
43
+
44
+ > [!NOTE]
45
+ > **Alpha.** The API can change between minor versions; see the
46
+ > [changelog](https://github.com/Open-Tech-Foundation/afw/blob/main/packages/afw-i18n/CHANGELOG.md).
47
+
48
+ ## Why afw-i18n
49
+
50
+ | Need | How afw-i18n meets it |
51
+ | --- | --- |
52
+ | 🌍 **Real grammar** | Full ICU MessageFormat: plurals for every locale's rules, `select`, number and date skeletons |
53
+ | 🔗 **URL-prefix routing** | One locale per URL, so each page is cacheable, indexable and free of hydration mismatches |
54
+ | 🎯 **Fine-grained** | `t()` and `fmt.*` read the reactive locale; only translated text updates |
55
+ | 📦 **Lazy catalogs** | Load each locale's messages as its own chunk, on demand |
56
+ | 🪶 **Native formatting** | `fmt.*` wraps the browser's `Intl` APIs, memoized per locale |
57
+ | 🛟 **Never crashes** | Missing keys fall back from the active locale to the default, then to the key itself |
58
+
59
+ ## Install
60
+
61
+ ```sh
62
+ pnpm add @opentf/afw-i18n
63
+ ```
64
+
65
+ `@opentf/afw` is a peer dependency. Apps that don't import this package ship none
66
+ of it.
67
+
68
+ ## Locale routing
69
+
70
+ Declare the locales once and pass them to `mountApp` (and to your prerender or
71
+ request renderer):
72
+
73
+ ```js
74
+ // afw.config.js
75
+ export default { i18n: { locales: ["en", "fr", "ja"], defaultLocale: "en" } };
76
+ ```
77
+
78
+ ```js
79
+ // entry.js
80
+ import { mountApp } from "@opentf/afw";
81
+ import { pages, guard } from "@afw/routes";
82
+ import config from "./afw.config.js";
83
+
84
+ mountApp({ pages, guard, i18n: config.i18n, target: document.getElementById("app") });
85
+ ```
86
+
87
+ The default locale has no prefix (`/pricing`); the others do (`/fr/pricing`). The
88
+ router strips the prefix, exposes `router.locale`, and `<Link>` keeps links in
89
+ the active locale. Switching language is a navigation to the localized URL.
90
+
91
+ ## Lazy catalogs
92
+
93
+ ```js
94
+ createI18n({
95
+ locales: ["en", "fr", "ja"],
96
+ defaultLocale: "en",
97
+ load: (locale) => ({
98
+ en: () => import("./messages/en.json"),
99
+ fr: () => import("./messages/fr.json"),
100
+ ja: () => import("./messages/ja.json"),
101
+ })[locale](),
102
+ });
103
+
104
+ await loadLocale("fr"); // before rendering a page in that locale
105
+ ```
106
+
107
+ `load` registers the loader; it does not fetch by itself. Preload the active and
108
+ default locales in a route guard or your prerender script.
109
+
110
+ ## API
111
+
112
+ | Export | Purpose |
113
+ | --- | --- |
114
+ | `createI18n({ locales, defaultLocale, messages, load })` | Register catalogs, eager (`messages`) or lazy (`load`) |
115
+ | `t(key, values?)` | Translate and ICU-format; reactive on the active locale |
116
+ | `<T id values />` | `t()` as a component that renders a `<span>` |
117
+ | `loadLocale(locale)` | Load and cache a locale through `load` |
118
+ | `setCatalog(locale, catalog)` | Set a catalog directly, e.g. when preloading on the server |
119
+ | `getLocale()` | The active locale |
120
+ | `locales()` | The configured locales and default |
121
+ | `fmt.number`, `fmt.currency`, `fmt.percent` | Number formatting |
122
+ | `fmt.date`, `fmt.relativeTime` | Dates and relative times ("2 days ago") |
123
+ | `fmt.list`, `fmt.plural` | Lists ("a, b, and c") and plural categories |
124
+
125
+ ## Documentation
126
+
127
+ | Doc | Purpose |
128
+ | --- | --- |
129
+ | [Overview](https://afw.opentechf.org/packages/afw-i18n) | Setup, `t()`, `<T>` and ICU messages, with a live demo |
130
+ | [Locale routing](https://afw.opentechf.org/packages/afw-i18n/routing) | URL prefixes, `router.locale`, prerendering every locale |
131
+ | [Formatting](https://afw.opentechf.org/packages/afw-i18n/formatting) | Every `fmt.*` helper |
132
+
133
+ ## License
134
+
135
+ MIT — see [LICENSE](https://github.com/Open-Tech-Foundation/afw/blob/main/LICENSE).
package/i18n.js CHANGED
Binary file
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@opentf/afw-i18n",
3
- "version": "1.27.0",
4
- "description": "Internationalization for AFW — ICU messages, Intl formatters, and a reactive t() that pairs with the core's URL-prefix locale routing.",
3
+ "version": "1.29.0",
4
+ "description": "ICU messages, Intl formatters and locale routing for AFW.",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
7
7
  "type": "module",
8
+ "scripts": {
9
+ "build": "cp ../../LICENSE LICENSE"
10
+ },
8
11
  "exports": {
9
12
  ".": "./index.js"
10
13
  },
@@ -16,6 +19,7 @@
16
19
  "!**/*.test.js"
17
20
  ],
18
21
  "keywords": [
22
+ "afw",
19
23
  "opentf",
20
24
  "i18n",
21
25
  "internationalization",
@@ -23,17 +27,25 @@
23
27
  "intl",
24
28
  "localization"
25
29
  ],
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/Open-Tech-Foundation/afw.git",
33
+ "directory": "packages/afw-i18n"
34
+ },
35
+ "homepage": "https://afw.opentechf.org/packages/afw-i18n",
36
+ "bugs": "https://github.com/Open-Tech-Foundation/afw/issues",
37
+ "license": "MIT",
26
38
  "dependencies": {
27
39
  "intl-messageformat": "^10.7.0"
28
40
  },
29
41
  "peerDependencies": {
30
- "@opentf/afw": "0.32.0"
42
+ "@opentf/afw": "0.34.0"
31
43
  },
32
44
  "publishConfig": {
33
45
  "access": "public"
34
46
  },
35
47
  "devDependencies": {
36
- "@opentf/afw": "0.32.0",
37
- "@opentf/afw-test": "1.27.0"
48
+ "@opentf/afw": "0.34.0",
49
+ "@opentf/afw-test": "1.29.0"
38
50
  }
39
51
  }