@ultimat3/i18n 31.50.0 → 31.52.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/src/context.ts CHANGED
@@ -253,17 +253,44 @@ export function registerBaseCatalog(locale: Locale, catalog: Catalog): void {
253
253
  installBase();
254
254
  }
255
255
 
256
+ /**
257
+ * The base a registered locale stands on: its LANGUAGE's layer, then its own tag's, matched on the
258
+ * canonical key — so `pt-br` stands on the shipped `pt-BR`, and `es-MX` on `es`. `undefined` when
259
+ * the framework ships nothing in that language.
260
+ */
261
+ function baseFor(locale: Locale): Catalog | undefined {
262
+ const key = translatorKey(locale);
263
+ const language = key.split('-')[0] ?? key;
264
+ const byKey = new Map<string, Catalog[]>();
265
+ for (const [tag, catalog] of base) {
266
+ const at = translatorKey(tag);
267
+ byKey.set(at, [...(byKey.get(at) ?? []), catalog]);
268
+ }
269
+ const layers = (language === key ? [key] : [language, key]).flatMap(
270
+ (wanted) => byKey.get(wanted) ?? [],
271
+ );
272
+ return layers.length === 0 ? undefined : mergeCatalogs(...layers);
273
+ }
274
+
256
275
  /**
257
276
  * Base UNDER app, never over it — `mergeCatalogs` takes the later argument, so the registered
258
277
  * catalog goes second and an app's override of `errors.notFound.title` survives every install.
259
278
  * Merging rather than skipping an occupied locale is what makes this order-independent: a base
260
279
  * registered after an app's `defineCatalogs()` still lands, and a repeat is a no-op because the
261
280
  * app's value already won the same key.
281
+ *
282
+ * Only `DEFAULT_LOCALE` is seated on its own. Every other shipped language (`ro`, `es`, `pt-BR`)
283
+ * lands under a locale the APP registered, never beside it: seated alone, `ro` would be in
284
+ * `registeredLocales()` — the app's manifest — of an app that never offered Romanian.
262
285
  */
263
286
  function installBase(): void {
264
- for (const [locale, catalog] of base) {
287
+ const seated = new Set<Locale>(registry.keys());
288
+ if (base.has(DEFAULT_LOCALE)) seated.add(DEFAULT_LOCALE);
289
+ for (const locale of seated) {
290
+ const layer = baseFor(locale);
291
+ if (layer === undefined) continue;
265
292
  const existing = registry.get(locale);
266
- registry.set(locale, existing === undefined ? catalog : mergeCatalogs(catalog, existing));
293
+ registry.set(locale, existing === undefined ? layer : mergeCatalogs(layer, existing));
267
294
  translators.delete(translatorKey(locale));
268
295
  }
269
296
  }
@@ -273,7 +300,9 @@ function installBase(): void {
273
300
  * key, because the merge takes the argument last.
274
301
  */
275
302
  export function registerCatalog(locale: Locale, catalog: Catalog): void {
276
- const existing = registry.get(locale);
303
+ // A first registration stands on the framework's strings in its language, so `ro` gets the
304
+ // shipped `ui.*` in Romanian rather than `⟦ui.dismiss⟧`.
305
+ const existing = registry.get(locale) ?? baseFor(locale);
277
306
  registry.set(locale, existing === undefined ? catalog : mergeCatalogs(existing, catalog));
278
307
  translators.delete(translatorKey(locale));
279
308
  }
package/src/errors.ts CHANGED
@@ -11,6 +11,8 @@ export const I18N_ERROR_CODES = [
11
11
  'X_CATALOG_INVALID',
12
12
  'X_CATALOG_UNREGISTERED',
13
13
  'X_I18N_FAMILY_INCOMPLETE',
14
+ 'X_CATALOG_LAYOUT_MIXED',
15
+ 'X_CATALOG_KEY_DUPLICATE',
14
16
  ] as const;
15
17
 
16
18
  export type I18nErrorCode = (typeof I18N_ERROR_CODES)[number];
@@ -21,6 +23,8 @@ export const I18N_ERROR_TITLES: Readonly<Record<I18nErrorCode, string>> = {
21
23
  X_CATALOG_INVALID: 'a catalog entry is malformed',
22
24
  X_CATALOG_UNREGISTERED: 'a shipped catalog never reached the runtime registry',
23
25
  X_I18N_FAMILY_INCOMPLETE: 'a select or ordinal family has no _other',
26
+ X_CATALOG_LAYOUT_MIXED: 'a locale ships both a catalog file and a catalog directory',
27
+ X_CATALOG_KEY_DUPLICATE: 'a catalog key is defined in two files of one locale',
24
28
  };
25
29
 
26
30
  // Titles must be registered for format() to render the contract's first line. Every code above is
@@ -48,26 +52,41 @@ export function localeUnsupported(tag: string, supported: readonly string[]): I1
48
52
  });
49
53
  }
50
54
 
55
+ /**
56
+ * A locale's catalog as a cause names it when the caller says nothing more: the flat file. A split
57
+ * locale (`catalogs/<locale>/`, one file per namespace) is named by the CLI, the half that reads
58
+ * the disk — this package never does.
59
+ */
60
+ const catalogFileOfLocale = (locale: string): string => `packages/i18n/catalogs/${locale}.json`;
61
+
51
62
  /**
52
63
  * `fix` must name the locale file and the exact keys — an agent reading this
53
64
  * error should not have to diff two catalogs to learn what to write.
54
65
  */
55
- export function catalogMissingKeys(locale: string, keys: readonly string[]): I18nError {
66
+ export function catalogMissingKeys(
67
+ locale: string,
68
+ keys: readonly string[],
69
+ file: string = catalogFileOfLocale(locale),
70
+ ): I18nError {
56
71
  const shown = keys.slice(0, 12);
57
72
  const suffix = keys.length > shown.length ? `, +${keys.length - shown.length} more` : '';
58
73
  return new I18nError({
59
74
  code: 'X_CATALOG_MISSING_KEYS',
60
- cause: `packages/i18n/catalogs/${locale}.json is missing ${keys.length} key(s) source names or the reference locale defines: ${shown.join(', ')}${suffix}`,
75
+ cause: `${file} is missing ${keys.length} key(s) source names or the reference locale defines: ${shown.join(', ')}${suffix}`,
61
76
  fix: `x i18n sync ${renderFixShellArg(locale, '<locale>')} # writes each key above as a placeholder to replace with the real string: ⟦key⟧ in the default locale, ⟦the default locale’s string⟧ in any other`,
62
77
  });
63
78
  }
64
79
 
65
80
  /** A select or ordinal family with no `_other`: a value it does not name renders `⟦key⟧`. */
66
- export function familyIncomplete(locale: string, owed: readonly string[]): I18nError {
81
+ export function familyIncomplete(
82
+ locale: string,
83
+ owed: readonly string[],
84
+ file: string = catalogFileOfLocale(locale),
85
+ ): I18nError {
67
86
  return new I18nError({
68
87
  code: 'X_I18N_FAMILY_INCOMPLETE',
69
- cause: `packages/i18n/catalogs/${locale}.json defines a select or ordinal family with no _other, so a value it does not name renders the loud miss: ${shownKeys(owed)}`,
70
- fix: `x i18n check --json # then add each key named here (${owed[0] ?? '<key>_select_other'}) to packages/i18n/catalogs/${locale}.json`,
88
+ cause: `${file} defines a select or ordinal family with no _other, so a value it does not name renders the loud miss: ${shownKeys(owed)}`,
89
+ fix: `x i18n check --json # then add each key named here (${owed[0] ?? '<key>_select_other'}) to ${file}`,
71
90
  });
72
91
  }
73
92
 
@@ -99,10 +118,12 @@ export function catalogUnregistered(input: {
99
118
  locale: string;
100
119
  shipped: number;
101
120
  missing: readonly string[];
121
+ /** Where the locale's catalog lives on disk; the flat file when not named. */
122
+ file?: string;
102
123
  }): I18nError {
103
124
  return new I18nError({
104
125
  code: 'X_CATALOG_UNREGISTERED',
105
- cause: `packages/i18n/catalogs/${input.locale}.json defines ${input.shipped} key(s) and ${input.missing.length} of them are not in the runtime registry after the app's own modules loaded: ${shownKeys(input.missing)} — each renders \u27e6key\u27e7`,
126
+ cause: `${input.file ?? catalogFileOfLocale(input.locale)} defines ${input.shipped} key(s) and ${input.missing.length} of them are not in the runtime registry after the app's own modules loaded: ${shownKeys(input.missing)} — each renders \u27e6key\u27e7`,
106
127
  fix: REGISTRATION_FIX,
107
128
  });
108
129
  }
@@ -151,3 +172,31 @@ function normalizeForFix(tag: string): string {
151
172
  const primary = tag.split('-')[0] ?? tag;
152
173
  return primary.toLowerCase().replace(/[^a-z]/g, '') || 'xx';
153
174
  }
175
+
176
+ /**
177
+ * A locale held as both `catalogs/<locale>.json` and `catalogs/<locale>/`. Refused rather than
178
+ * merged: the index imports ONE of them, so the other is strings on disk that no page renders, and
179
+ * which one won would depend on a line nobody reads. `x i18n split` is the repair — it moves the
180
+ * flat file's keys into the directory, refusing any key both hold.
181
+ */
182
+ export function catalogLayoutMixed(locale: string): I18nError {
183
+ return new I18nError({
184
+ code: 'X_CATALOG_LAYOUT_MIXED',
185
+ cause: `locale "${locale}" has both packages/i18n/catalogs/${locale}.json and packages/i18n/catalogs/${locale}/ — a locale is one file or one directory of namespace files, never both, so one of them is strings no page renders`,
186
+ fix: `x i18n split ${renderFixShellArg(locale, '<locale>')} # moves every key of the .json file into its namespace file in the directory and deletes the file; a key both hold is refused`,
187
+ });
188
+ }
189
+
190
+ /**
191
+ * One key from two files of a split locale — the same key twice, or a leaf in one file that the
192
+ * other nests under (`site.home` in `site.json`, `site/home.json` beside it). The index merges the
193
+ * files into one object, so the loser vanishes without a word: refused here, naming both files.
194
+ */
195
+ export function catalogKeyDuplicate(key: string, files: readonly [string, string]): I18nError {
196
+ const [first, second] = files;
197
+ return new I18nError({
198
+ code: 'X_CATALOG_KEY_DUPLICATE',
199
+ cause: `catalog key "${key}" is defined by both ${first} and ${second}, so the app ships whichever its index merges last and the other wording is never rendered`,
200
+ fix: `grep -n ${renderFixShellArg(key.split('.').pop() ?? key, '<leaf>')} ${renderFixShellArg(first, '<file>')} ${renderFixShellArg(second, '<file>')} # delete "${key}" from one of the two, keeping the wording that ships; then: x i18n check --json`,
201
+ });
202
+ }
package/src/framework.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  /**
2
- * The framework's own strings, flattened and installed as the base layer at import.
3
- * Under every app catalog, so an app overrides any framework key and forgets none.
2
+ * The framework's own strings, flattened and installed as the base layer at import, one catalog per
3
+ * shipped locale. Under every app catalog, so an app overrides any framework key and forgets none.
4
4
  */
5
5
 
6
6
  import { DEFAULT_LOCALE } from '@ultimat3/core';
7
7
  import { type Catalog, loadCatalog } from './catalog';
8
8
  import en from './catalogs/en.json';
9
+ import es from './catalogs/es.json';
10
+ import ptBR from './catalogs/pt-BR.json';
11
+ import ro from './catalogs/ro.json';
9
12
  import { registerBaseCatalog } from './context';
13
+ import type { Locale } from './locales';
10
14
 
11
15
  /**
12
16
  * `errors.*`, `auth.*`, `pagination.*`, `admin.*`, `validation.*`, `common.*`, `time.*`, `ui.*`.
@@ -17,10 +21,27 @@ import { registerBaseCatalog } from './context';
17
21
  */
18
22
  export const FRAMEWORK_CATALOG: Catalog = loadCatalog(en);
19
23
 
24
+ /**
25
+ * Every locale the framework translates its own strings into, `en` the source. Each one carries
26
+ * every `en` key — `framework.test.ts` refuses a gap and a placeholder that drifted — and lands
27
+ * under an app catalog in the same language: `pt-br`, `pt-BR` and `pt` all stand on `pt-BR`'s.
28
+ */
29
+ export const FRAMEWORK_CATALOGS: Readonly<Record<Locale, Catalog>> = {
30
+ [DEFAULT_LOCALE]: FRAMEWORK_CATALOG,
31
+ es: loadCatalog(es),
32
+ 'pt-BR': loadCatalog(ptBR),
33
+ ro: loadCatalog(ro),
34
+ };
35
+
20
36
  // At module scope, and that is the whole point: `index.ts` re-exports this file, so anything that
21
37
  // can call `t()` has already imported the module that installs these strings. It was a function
22
38
  // (`registerFrameworkCatalog`) that only `defineCatalogs` called until 5.1.0 — so an app whose
23
39
  // catalog module nothing imported rendered `⟦errors.notFound.title⟧` on its 404 page, `⟦ui.*⟧` in
24
40
  // every design-system control, and had no way to notice (issue #249). Registration that an app
25
41
  // can forget is not registration; the framework's half of it is now unforgettable by construction.
42
+ // The source first, as its own statement: the side-effects scan reads a top-level call as this
43
+ // module's import-time effect, and a loop alone hid it.
26
44
  registerBaseCatalog(DEFAULT_LOCALE, FRAMEWORK_CATALOG);
45
+ for (const [locale, catalog] of Object.entries(FRAMEWORK_CATALOGS)) {
46
+ if (locale !== DEFAULT_LOCALE) registerBaseCatalog(locale, catalog);
47
+ }
package/src/index.ts CHANGED
@@ -20,6 +20,13 @@ export {
20
20
  nestCatalog,
21
21
  parseNestedCatalog,
22
22
  } from './catalog';
23
+ export {
24
+ type CatalogFile,
25
+ catalogFileName,
26
+ catalogFilePrefix,
27
+ joinCatalogFiles,
28
+ partitionCatalog,
29
+ } from './catalog-files';
23
30
  export {
24
31
  catalogFor,
25
32
  configureLocales,
@@ -51,6 +58,8 @@ export {
51
58
  } from './define-catalogs';
52
59
  export {
53
60
  catalogInvalid,
61
+ catalogKeyDuplicate,
62
+ catalogLayoutMixed,
54
63
  catalogMissingKeys,
55
64
  catalogsNeverRegistered,
56
65
  catalogUnregistered,
@@ -71,7 +80,7 @@ export {
71
80
  type LocaleAudit,
72
81
  mergeExtractions,
73
82
  } from './extract';
74
- export { FRAMEWORK_CATALOG } from './framework';
83
+ export { FRAMEWORK_CATALOG, FRAMEWORK_CATALOGS } from './framework';
75
84
  export {
76
85
  type InterpolationValue,
77
86
  type InterpolationVars,