@opentf/afw-i18n 0.0.0-stage → 1.28.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.
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 CHANGED
@@ -1,3 +1,135 @@
1
- # Temporary Holding Version
1
+ <div align="center">
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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/T.jsx ADDED
@@ -0,0 +1,14 @@
1
+ // <T id values /> — inline translation component (docs/I18N.md §5).
2
+ //
3
+ // Sugar over `t(id, values)`: renders the translated string into a <span>, wired
4
+ // reactively to the active locale like any text binding. For messages that
5
+ // interpolate a *signal* (e.g. a live `{count}`), prefer the bare `{t("key", { count })}`
6
+ // form in markup so the compiler tracks the signal directly.
7
+ //
8
+ // Shipped as JSX source and compiled by the consuming app's pipeline (like <Link>).
9
+
10
+ import { t } from "./i18n.js";
11
+
12
+ export default function T({ id, values }) {
13
+ return <span>{t(id, values)}</span>;
14
+ }
package/format.js ADDED
@@ -0,0 +1,47 @@
1
+ //! Locale-aware formatters over the browser-native `Intl` APIs (docs/I18N.md §5).
2
+ //
3
+ // Each formatter reads the active locale from `getLocale()` (which tracks the
4
+ // reactive `router.locale`), so `fmt.currency(...)` inside a binding re-renders on
5
+ // a locale-changing navigation. `Intl.*` instances are memoized per
6
+ // (kind, locale, options) — constructing them is comparatively expensive.
7
+
8
+ import { getLocale } from "./i18n.js";
9
+
10
+ const cache = new Map();
11
+
12
+ function intl(kind, Ctor, options) {
13
+ const locale = getLocale();
14
+ const key = `${kind} ${locale} ${options ? JSON.stringify(options) : ""}`;
15
+ let inst = cache.get(key);
16
+ if (!inst) {
17
+ inst = new Ctor(locale, options);
18
+ cache.set(key, inst);
19
+ }
20
+ return inst;
21
+ }
22
+
23
+ export const fmt = {
24
+ /** Locale-grouped number: `fmt.number(1234.5)`. */
25
+ number: (value, options) => intl("n", Intl.NumberFormat, options).format(value),
26
+
27
+ /** Currency: `fmt.currency(189, "USD")` → "$189.00" / "189,00 $US". */
28
+ currency: (value, currency, options) =>
29
+ intl("c", Intl.NumberFormat, { style: "currency", currency, ...options }).format(value),
30
+
31
+ /** Percent: `fmt.percent(0.42)` → "42%". */
32
+ percent: (value, options) =>
33
+ intl("pc", Intl.NumberFormat, { style: "percent", ...options }).format(value),
34
+
35
+ /** Date/time: `fmt.date(new Date(), { dateStyle: "long" })`. */
36
+ date: (value, options) => intl("d", Intl.DateTimeFormat, options).format(value),
37
+
38
+ /** Relative time: `fmt.relativeTime(-3, "day")` → "3 days ago". */
39
+ relativeTime: (value, unit, options) =>
40
+ intl("r", Intl.RelativeTimeFormat, options).format(value, unit),
41
+
42
+ /** Plural category for the active locale: `fmt.plural(2)` → "other". */
43
+ plural: (value, options) => intl("p", Intl.PluralRules, options).select(value),
44
+
45
+ /** List: `fmt.list(["a","b","c"])` → "a, b, and c". */
46
+ list: (values, options) => intl("l", Intl.ListFormat, options).format(values),
47
+ };
package/i18n.js ADDED
Binary file
package/index.js ADDED
@@ -0,0 +1,15 @@
1
+ // Public entry for @opentf/afw-i18n — ICU messages + Intl formatters that pair
2
+ // with the core router's URL-prefix locale routing (docs/I18N.md).
3
+ //
4
+ // import { createI18n, t, fmt } from "@opentf/afw-i18n";
5
+ // createI18n({ locales: ["en", "fr"], defaultLocale: "en", messages: { … } });
6
+ // t("greeting", { name }); // reactive on router.locale
7
+ // fmt.currency(189, "USD");
8
+
9
+ export { createI18n, getLocale, loadLocale, locales, setCatalog, t } from "./i18n.js";
10
+ export { fmt } from "./format.js";
11
+
12
+ // `<T>` is a pure JSX component (compiled by the app's pipeline). The bare import
13
+ // retains its Custom Element registration side effect; the re-export exposes it.
14
+ import "./T.jsx";
15
+ export { default as T } from "./T.jsx";
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@opentf/afw-i18n",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.28.0",
4
+ "description": "ICU messages, Intl formatters and locale routing for AFW.",
5
+ "main": "index.js",
6
+ "module": "index.js",
7
+ "type": "module",
8
+ "scripts": {
9
+ "build": "cp ../../LICENSE LICENSE"
10
+ },
11
+ "exports": {
12
+ ".": "./index.js"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "i18n.js",
17
+ "format.js",
18
+ "T.jsx",
19
+ "!**/*.test.js"
20
+ ],
21
+ "keywords": [
22
+ "afw",
23
+ "opentf",
24
+ "i18n",
25
+ "internationalization",
26
+ "icu",
27
+ "intl",
28
+ "localization"
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",
38
+ "dependencies": {
39
+ "intl-messageformat": "^10.7.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@opentf/afw": "0.33.0"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "devDependencies": {
48
+ "@opentf/afw": "0.33.0",
49
+ "@opentf/afw-test": "1.28.0"
50
+ }
51
+ }