@ultimat3/i18n 31.51.0 → 31.53.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/CLAUDE.md +11 -7
- package/README.md +32 -9
- package/package.json +2 -2
- package/src/catalog-files.ts +107 -0
- package/src/catalogs/en.json +4 -0
- package/src/catalogs/es.json +771 -0
- package/src/catalogs/pt-BR.json +771 -0
- package/src/catalogs/ro.json +774 -0
- package/src/context.ts +32 -3
- package/src/errors.ts +55 -6
- package/src/framework.ts +23 -2
- package/src/index.ts +10 -1
package/CLAUDE.md
CHANGED
|
@@ -9,6 +9,7 @@ Imported by every package that renders a string.
|
|
|
9
9
|
|---|---|
|
|
10
10
|
| `translator.ts` | lookup + delegate interpolation, and `TranslationKey`. No locale logic. |
|
|
11
11
|
| `catalog.ts` | nested → flat, merge, validate. No lookup. |
|
|
12
|
+
| `catalog-files.ts` | a split locale's namespace files joined into one catalog and partitioned back. Pure: the CLI reads the files. |
|
|
12
13
|
| `define-catalogs.ts` | the app's one registration call. Boot only, composes the four below. |
|
|
13
14
|
| `interpolate.ts` | `{var}` + CLDR plural selection. No catalog access (takes a `has` predicate). |
|
|
14
15
|
| `locales.ts` | supported set, normalize, negotiate, RTL. No catalogs. |
|
|
@@ -17,7 +18,7 @@ Imported by every package that renders a string.
|
|
|
17
18
|
| `app-catalogs.ts` | an app's declared locales, read by importing its catalog module — `loadAppCatalogs` (`undefined` when undeclared) and `appLocaleSet` (`UNDECLARED_LOCALES` then). The ONE reader: a CLI step never reads `localeConfig()`/`routedLocales()` after `loadApp`; it reads `LoadedApp.locales`. Tooling only: the `./app-catalogs` entry, never `index.ts` (it is in every island). |
|
|
18
19
|
| `registration.ts` | are the catalogs an app SHIPS in the registry it READS? No file access. |
|
|
19
20
|
| `extract.ts` | static scan + audit for `x verify`. |
|
|
20
|
-
| `catalogs
|
|
21
|
+
| `catalogs/<locale>.json` | data, not code: `en` the source, `ro`, `es`, `pt-BR` its translations. The framework's own stay flat; an APP's may be split (`catalog-files.ts`). |
|
|
21
22
|
| `recording-translator.ts` | a translator that records what it could not answer: `TranslationMiss` |
|
|
22
23
|
|
|
23
24
|
## Rules
|
|
@@ -36,11 +37,14 @@ Imported by every package that renders a string.
|
|
|
36
37
|
importing `@ultimat3/i18n` is what registers it, so no app can forget it and `resetCatalogs()`
|
|
37
38
|
puts it back. It was `registerFrameworkCatalog()`, whose ONE caller was `defineCatalogs`, so an
|
|
38
39
|
app whose catalog module nothing imported served `⟦errors.notFound.title⟧` on its 404 page and
|
|
39
|
-
`⟦ui.*⟧` in every design-system control, with `x verify` green (issue #249).
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
`⟦ui.*⟧` in every design-system control, with `x verify` green (issue #249). **Each catalog under
|
|
41
|
+
the language it is written in** (`FRAMEWORK_CATALOGS`: `en`, `ro`, `es`, `pt-BR`): `baseFor` in
|
|
42
|
+
`context.ts` lays an app locale on its language's layer, then its exact tag's, on the canonical
|
|
43
|
+
key (`pt-br` on `pt-BR`, `es-MX` on `es`). Never English under another language: it ran per locale
|
|
44
|
+
until 2026-08, so an `es`-only app served `Page not found` with `isMiss` reading FALSE — the
|
|
45
|
+
fallback chain the line above refuses. Only `DEFAULT_LOCALE` is seated alone; another shipped
|
|
46
|
+
language lands only under a locale the app registered, so it never enters `registeredLocales()`.
|
|
47
|
+
A new framework string goes into all four catalogs (`framework-locales.test.ts`).
|
|
44
48
|
- **The base layer merges UNDER whatever `registerCatalog` seated, never over it**, so an app's
|
|
45
49
|
override of a framework key survives every later install and the install is idempotent by
|
|
46
50
|
construction rather than by a module flag `resetCatalogs()` would go stale against.
|
|
@@ -112,7 +116,7 @@ Imported by every package that renders a string.
|
|
|
112
116
|
`attachLocale`/`localeOf` existed until 1.3.0 with zero callers; `@ultimat3/time` had the same
|
|
113
117
|
pair over a field name (`ctx['timeZone']`) that disagreed with core's `tz`, which is what made
|
|
114
118
|
every server-rendered date UTC. Never reintroduce either half.
|
|
115
|
-
- Adding a framework string: `catalogs/en.json`, feature-namespaced, then use `t('ns.key')`.
|
|
119
|
+
- Adding a framework string: `catalogs/en.json`, feature-namespaced, the same key in `ro.json`, `es.json` and `pt-BR.json`, then use `t('ns.key')`.
|
|
116
120
|
- **`catalogs/en.json` is gated in both directions**, on `x verify`'s `boundaries` step via
|
|
117
121
|
`scripts/i18n-catalog.ts`. A `t('literal')` in `packages/*/src` with no entry is
|
|
118
122
|
`X_CATALOG_MISSING_KEYS`; an entry nothing in framework source names, in a namespace the
|
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ strings: format those with `Intl` via [@ultimat3/money](../money) and [@ultimat3
|
|
|
12
12
|
| Registration | `define-catalogs.ts` | `defineCatalogs()` — one call, at boot, per app |
|
|
13
13
|
| Lookup + interpolation | `translator.ts` | `t(key, vars?)`, miss → `⟦key⟧` |
|
|
14
14
|
| Catalog shape | `catalog.ts` | nested authoring → flat dot-keys |
|
|
15
|
+
| Split catalogs | `catalog-files.ts` | a locale's namespace files joined, each key in one file |
|
|
15
16
|
| Placeholders + plurals | `interpolate.ts` | `{var}`, CLDR categories via `Intl.PluralRules` |
|
|
16
17
|
| Locale resolution | `locales.ts` | supported set, `normalizeLocale`, `negotiateLocale` |
|
|
17
18
|
| Request locale | `context.ts` | resolved once, read from ALS — never passed by hand |
|
|
@@ -37,7 +38,7 @@ export const useT = (): Translator<AppCatalog> => useI18n<AppCatalog>();
|
|
|
37
38
|
| One call | Because |
|
|
38
39
|
|---|---|
|
|
39
40
|
| validates + flattens every locale before registering any | a malformed catalog fails the boot whole, never half |
|
|
40
|
-
|
|
|
41
|
+
| stands each locale on the framework catalog in its language, the app's strings second | later wins, so app strings override framework strings |
|
|
41
42
|
| `configureLocales({ supported, fallback })` | the locale set is declared once, not twice |
|
|
42
43
|
| returns `{ default, locales, catalogs, keys() }` | the app's key space, for tests and tooling |
|
|
43
44
|
|
|
@@ -94,14 +95,34 @@ non-string leaf with `X_CATALOG_INVALID`. Registration order is framework then a
|
|
|
94
95
|
wins, which is how an app overrides `errors.notFound.title` without forking the framework.
|
|
95
96
|
|
|
96
97
|
`src/catalogs/en.json` ships the framework's own strings: `errors.*`, `auth.*`,
|
|
97
|
-
`pagination.*`, `admin.*`, `validation.*`, `common.*`, `time.cron
|
|
98
|
+
`pagination.*`, `admin.*`, `validation.*`, `common.*`, `time.cron.*`, `ui.*` — and
|
|
99
|
+
`ro.json`, `es.json`, `pt-BR.json` the same keys translated (`FRAMEWORK_CATALOGS`).
|
|
98
100
|
|
|
99
|
-
**
|
|
100
|
-
too. An app
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
**Each is registered under the language it is written in** — the golden rule applies to framework
|
|
102
|
+
strings too. An app's `ro`, `pt-br` or `es-MX` catalog stands on the shipped Romanian, Portuguese or
|
|
103
|
+
Spanish one, its own strings on top. A language the framework does not ship (`de`) renders
|
|
104
|
+
`⟦errors.notFound.title⟧` for a key the app has not translated, never `Page not found`: filling
|
|
105
|
+
every locale with the English catalog is a fallback chain, it reads as `isMiss === false`, and
|
|
106
|
+
`auditCatalogs()` cannot see it because `CatalogSet.catalogs` carries app strings only.
|
|
107
|
+
|
|
108
|
+
### Split catalogs
|
|
109
|
+
|
|
110
|
+
**A locale is one file or one directory of namespace files, never both.** `As of 2026-11`.
|
|
111
|
+
|
|
112
|
+
| Form | Holds | Index imports |
|
|
113
|
+
|---|---|---|
|
|
114
|
+
| flat | `catalogs/en.json`: every namespace | `import en from '../catalogs/en.json'` |
|
|
115
|
+
| split | `catalogs/en/<namespace>.json`: that namespace's subtree; `site/home.json` is `site.home.*` | `import { catalog as en } from '../catalogs/en'` — the generated barrel `catalogs/en/index.ts` |
|
|
116
|
+
|
|
117
|
+
`bunx x i18n split [<locale>]` migrates, losslessly and idempotently. This package reads no file:
|
|
118
|
+
the CLI hands each one over parsed, and these are the pure halves.
|
|
119
|
+
|
|
120
|
+
| Export | Does |
|
|
121
|
+
|---|---|
|
|
122
|
+
| `joinCatalogFiles(files)` | `{ path, name, value }[]` → one `Catalog`; a key two files define, or a leaf one file defines that another nests under, is `X_CATALOG_KEY_DUPLICATE` naming both |
|
|
123
|
+
| `partitionCatalog(catalog, names?)` | the inverse: file name → its keys, each key in the existing file of its longest prefix, else its namespace's own; a top-level key is `X_CATALOG_INVALID` |
|
|
124
|
+
| `catalogFilePrefix(name)` · `catalogFileName(prefix)` | `site/home.json` ↔ `site.home` |
|
|
125
|
+
| `catalogLayoutMixed(locale)` | `X_CATALOG_LAYOUT_MIXED`, raised by the CLI for `en.json` beside `en/` |
|
|
105
126
|
|
|
106
127
|
## A catalog slice for an island
|
|
107
128
|
|
|
@@ -156,6 +177,8 @@ X_CATALOG_MISSING_KEYS: catalog is incomplete
|
|
|
156
177
|
| `X_LOCALE_INVALID` | `defineCatalogs` or `configureLocales` was given a tag that is not well-formed BCP 47 (`en_US`). **`@ultimat3/core`'s code**, raised by its `assertLocale` — not this package's `I18nError` |
|
|
157
178
|
| `X_CATALOG_MISSING_KEYS` | a shipped locale lacks a key the source uses |
|
|
158
179
|
| `X_CATALOG_INVALID` | non-string leaf, bad key segment, or a dotted/nested collision |
|
|
180
|
+
| `X_CATALOG_LAYOUT_MIXED` | a locale has both `catalogs/<locale>.json` and `catalogs/<locale>/` |
|
|
181
|
+
| `X_CATALOG_KEY_DUPLICATE` | two namespace files of one split locale define one key |
|
|
159
182
|
|
|
160
183
|
### Error classes
|
|
161
184
|
|
|
@@ -164,7 +187,7 @@ a job boundary the class is gone and the `code` is what survives — match on th
|
|
|
164
187
|
|
|
165
188
|
| Class | Code | Declared in |
|
|
166
189
|
|---|---|---|
|
|
167
|
-
| `I18nError` | `X_LOCALE_UNSUPPORTED`, `X_CATALOG_MISSING_KEYS`, `X_CATALOG_INVALID`, `X_CATALOG_UNREGISTERED` | `src/errors.ts` |
|
|
190
|
+
| `I18nError` | `X_LOCALE_UNSUPPORTED`, `X_CATALOG_MISSING_KEYS`, `X_CATALOG_INVALID`, `X_CATALOG_UNREGISTERED`, `X_CATALOG_LAYOUT_MIXED`, `X_CATALOG_KEY_DUPLICATE` | `src/errors.ts` |
|
|
168
191
|
|
|
169
192
|
## Why it exists
|
|
170
193
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/i18n",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.53.0",
|
|
4
4
|
"description": "Dependency-free translator, catalog flattening, locale negotiation and loud missing-key rendering",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -48,6 +48,6 @@
|
|
|
48
48
|
"test": "bun test"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@ultimat3/core": "31.
|
|
51
|
+
"@ultimat3/core": "31.53.0"
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// A locale split across files — `catalogs/<locale>/<namespace>.json` — as pure data: the key
|
|
2
|
+
// prefix a file's path stands for, the files joined into one catalog with every key held by exactly
|
|
3
|
+
// one of them, and a catalog partitioned back into its files. This package never reads a file: the
|
|
4
|
+
// CLI hands each one over parsed, with its path.
|
|
5
|
+
|
|
6
|
+
import { type Catalog, catalogKeys, flattenCatalog, parseNestedCatalog } from './catalog';
|
|
7
|
+
import { catalogInvalid, catalogKeyDuplicate } from './errors';
|
|
8
|
+
|
|
9
|
+
/** One namespace file of a split locale, already parsed. */
|
|
10
|
+
export interface CatalogFile {
|
|
11
|
+
/** Where it lives, as the error that names it should print it (root-relative). */
|
|
12
|
+
readonly path: string;
|
|
13
|
+
/** Relative to the locale's directory, `/`-separated: `billing.json`, `site/home.json`. */
|
|
14
|
+
readonly name: string;
|
|
15
|
+
/** The parsed JSON — validated here, so a caller never trusts it. */
|
|
16
|
+
readonly value: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** `catalog.ts`'s key-segment rule: a directory or a file stem IS a key segment here. */
|
|
20
|
+
const SEGMENT = /^[A-Za-z0-9_-]+$/;
|
|
21
|
+
|
|
22
|
+
/** `site/home.json` → `site.home`: the key prefix every entry of that file is read under. */
|
|
23
|
+
export function catalogFilePrefix(name: string): string {
|
|
24
|
+
const segments = name.replace(/\.json$/, '').split('/');
|
|
25
|
+
if (!name.endsWith('.json') || !segments.every((segment) => SEGMENT.test(segment))) {
|
|
26
|
+
throw catalogInvalid(
|
|
27
|
+
name,
|
|
28
|
+
'a namespace file is named by its key segments: each directory and the file stem match /^[A-Za-z0-9_-]+$/',
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return segments.join('.');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `site.home` → `site/home.json`: the inverse, for a writer that places a new namespace. */
|
|
35
|
+
export const catalogFileName = (prefix: string): string => `${prefix.split('.').join('/')}.json`;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Every file of one locale as the one catalog it ships. A key two files define is
|
|
39
|
+
* `X_CATALOG_KEY_DUPLICATE`, naming both — and so is a leaf one file defines that another file
|
|
40
|
+
* nests under (`site.home` in `site.json`, `site/home.json` beside it): the app's index merges the
|
|
41
|
+
* files into one object, so either way one wording would vanish without a word.
|
|
42
|
+
*
|
|
43
|
+
* Files are joined in name order, so which file an error names first never depends on how the
|
|
44
|
+
* directory happened to list.
|
|
45
|
+
*/
|
|
46
|
+
export function joinCatalogFiles(files: readonly CatalogFile[]): Catalog {
|
|
47
|
+
const joined = Object.create(null) as Record<string, string>;
|
|
48
|
+
const owner = new Map<string, string>();
|
|
49
|
+
const ordered = [...files].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
50
|
+
for (const file of ordered) {
|
|
51
|
+
const prefix = catalogFilePrefix(file.name);
|
|
52
|
+
const catalog = flattenCatalog(parseNestedCatalog(file.value, prefix), prefix);
|
|
53
|
+
for (const [key, value] of Object.entries(catalog)) {
|
|
54
|
+
const prior = owner.get(key);
|
|
55
|
+
if (prior !== undefined) throw catalogKeyDuplicate(key, [prior, file.path]);
|
|
56
|
+
owner.set(key, file.path);
|
|
57
|
+
joined[key] = value;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (const [key, path] of owner) {
|
|
61
|
+
const segments = key.split('.');
|
|
62
|
+
for (let length = 1; length < segments.length; length += 1) {
|
|
63
|
+
const leafAt = owner.get(segments.slice(0, length).join('.'));
|
|
64
|
+
if (leafAt !== undefined) {
|
|
65
|
+
throw catalogKeyDuplicate(segments.slice(0, length).join('.'), [leafAt, path]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return joined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* `catalog` as the files a split locale holds it in, keyed by file name, each with its keys
|
|
74
|
+
* relative to the file's prefix. A key goes to the file of its LONGEST prefix among `names` — so
|
|
75
|
+
* `site.home.title` lands in `site/home.json` when that file exists — else to its namespace's own
|
|
76
|
+
* file, `billing.plans.pro` to `billing.json`, which the writer then creates. A top-level leaf has
|
|
77
|
+
* no namespace to live in, and is refused: a split catalog is namespaces all the way down.
|
|
78
|
+
*/
|
|
79
|
+
export function partitionCatalog(
|
|
80
|
+
catalog: Catalog,
|
|
81
|
+
names: readonly string[] = [],
|
|
82
|
+
): ReadonlyMap<string, Catalog> {
|
|
83
|
+
const prefixes = names.map(catalogFilePrefix).sort((a, b) => b.length - a.length);
|
|
84
|
+
const parts = new Map<string, Record<string, string>>();
|
|
85
|
+
for (const key of catalogKeys(catalog)) {
|
|
86
|
+
const value = catalog[key];
|
|
87
|
+
if (value === undefined) continue;
|
|
88
|
+
const prefix =
|
|
89
|
+
prefixes.find((candidate) => key.startsWith(`${candidate}.`)) ?? namespaceOf(key);
|
|
90
|
+
const name = catalogFileName(prefix);
|
|
91
|
+
const part = parts.get(name) ?? (Object.create(null) as Record<string, string>);
|
|
92
|
+
part[key.slice(prefix.length + 1)] = value;
|
|
93
|
+
parts.set(name, part);
|
|
94
|
+
}
|
|
95
|
+
return parts;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function namespaceOf(key: string): string {
|
|
99
|
+
const dot = key.indexOf('.');
|
|
100
|
+
if (dot === -1) {
|
|
101
|
+
throw catalogInvalid(
|
|
102
|
+
key,
|
|
103
|
+
`a split catalog keeps every key under a namespace, and "${key}" has none — nest it under one (common.${key}) and rename its t() calls`,
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return key.slice(0, dot);
|
|
107
|
+
}
|
package/src/catalogs/en.json
CHANGED