@ultimat3/i18n 31.47.1 → 31.49.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/README.md CHANGED
@@ -106,7 +106,8 @@ app renders into your own catalog — that is the one path, and it is the same m
106
106
  ## A catalog slice for an island
107
107
 
108
108
  A translator is a function and cannot be an island's prop. `catalogSubset(t, keys)` resolves the
109
- templates `keys` name — every plural variant included — into `{ locale, catalog }`, plain JSON;
109
+ templates `keys` name — every plural variant and every `_select_`/`_ordinal_` family member
110
+ included (found through `t.keys()`) — into `{ locale, catalog }`, plain JSON;
110
111
  the island builds `subsetTranslator(subset)` over it: the same lookup, interpolation, CLDR plural
111
112
  pick and loud `⟦key⟧` miss as the server's. Both are on `@ultimat3/i18n/subset`, an entry that
112
113
  installs no framework catalog (the barrel does), so an island chunk can import it.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/i18n",
3
- "version": "31.47.1",
3
+ "version": "31.49.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.47.1"
51
+ "@ultimat3/core": "31.49.0"
52
52
  }
53
53
  }
package/src/errors.ts CHANGED
@@ -58,7 +58,7 @@ export function catalogMissingKeys(locale: string, keys: readonly string[]): I18
58
58
  return new I18nError({
59
59
  code: 'X_CATALOG_MISSING_KEYS',
60
60
  cause: `packages/i18n/catalogs/${locale}.json is missing ${keys.length} key(s) source names or the reference locale defines: ${shown.join(', ')}${suffix}`,
61
- fix: `x i18n sync ${renderFixShellArg(locale, '<locale>')} # writes each key above as ⟦key⟧ — replace every one with the real string`,
61
+ 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
62
  });
63
63
  }
64
64
 
package/src/subset.ts CHANGED
@@ -23,21 +23,60 @@ export type CatalogSubset = {
23
23
  /** The two members of a translator a subset is read through — any app's typed `t` has them. */
24
24
  export interface RawTranslator {
25
25
  raw(key: string): string | undefined;
26
+ /**
27
+ * Every key of the catalog. A `select` family's values are an open set, so its members can only
28
+ * be found by listing: a translator without `keys()` yields a subset with no families.
29
+ */
30
+ keys?(): string[];
26
31
  readonly locale: Locale;
27
32
  }
28
33
 
34
+ /** The infix of a family member: `<key>_select_<value>`, `<key>_ordinal_<category>`. */
35
+ const FAMILY = /_(?:select|ordinal)_/g;
36
+
29
37
  /**
30
- * The templates `keys` name in `t`'s locale, each with every plural variant the catalog holds, so
31
- * `t(key, { count })` in the island picks the form the server would. A key the catalog lacks is
32
- * left out, and renders `⟦key⟧` in the island exactly as it would on the server.
38
+ * Each translator's family members by stem, listed once: a subset is cut per island per request,
39
+ * and scanning a 4,000-key catalog each time cost 0.78 ms where the lookups cost 0.006. A
40
+ * translator's catalog is fixed for its life (a reload builds a new one), so the index never stales.
41
+ */
42
+ const families = new WeakMap<RawTranslator, ReadonlyMap<string, readonly string[]>>();
43
+
44
+ function familiesOf(t: RawTranslator): ReadonlyMap<string, readonly string[]> {
45
+ const known = families.get(t);
46
+ if (known !== undefined) return known;
47
+ const index = new Map<string, string[]>();
48
+ for (const name of t.keys?.() ?? []) {
49
+ // Every infix, never only the first: a named key may itself spell `_select_`.
50
+ for (const infix of name.matchAll(FAMILY)) {
51
+ const stem = name.slice(0, infix.index);
52
+ // In place: a copy per member made one family of n members cost n²/2.
53
+ const members = index.get(stem);
54
+ if (members === undefined) index.set(stem, [name]);
55
+ else members.push(name);
56
+ }
57
+ }
58
+ families.set(t, index);
59
+ return index;
60
+ }
61
+
62
+ /**
63
+ * The templates `keys` name in `t`'s locale, each with every plural variant and every `select` and
64
+ * `ordinal` family member the catalog holds, so `t(key, { count, select, ordinal })` in the island
65
+ * picks the form the server would. A key the catalog lacks is left out, and renders `⟦key⟧` in the
66
+ * island exactly as it would on the server.
33
67
  */
34
68
  export function catalogSubset(t: RawTranslator, keys: readonly string[]): CatalogSubset {
35
69
  const catalog: Record<string, string> = Object.create(null) as Record<string, string>;
70
+ const copy = (name: string): void => {
71
+ const template = t.raw(name);
72
+ if (template !== undefined) catalog[name] = template;
73
+ };
36
74
  for (const key of keys) {
37
- for (const name of [key, ...pluralVariantsOf(key)]) {
38
- const template = t.raw(name);
39
- if (template !== undefined) catalog[name] = template;
40
- }
75
+ for (const name of [key, ...pluralVariantsOf(key)]) copy(name);
76
+ }
77
+ const index = familiesOf(t);
78
+ for (const key of keys) {
79
+ for (const name of index.get(key) ?? []) copy(name);
41
80
  }
42
81
  return { locale: t.locale, catalog };
43
82
  }
package/src/translator.ts CHANGED
@@ -64,6 +64,9 @@ export interface Translator<TCatalog = Catalog> {
64
64
  readonly locale: Locale;
65
65
  }
66
66
 
67
+ /** What every `pluralKeyCandidates` chain ends on, as suffixes of a stem. */
68
+ const CHAIN_ENDS = ['', '_other', '_plural', '_one'] as const;
69
+
67
70
  export function catalogTranslator(catalog: Catalog, locale: Locale = DEFAULT_LOCALE): Translator {
68
71
  const hasExact = (key: string): boolean => Object.hasOwn(catalog, key);
69
72
 
@@ -84,13 +87,12 @@ export function catalogTranslator(catalog: Catalog, locale: Locale = DEFAULT_LOC
84
87
  };
85
88
 
86
89
  return Object.assign(translate, {
90
+ // The stems `selectMessageKey` falls back to, each with the four forms every plural chain ends
91
+ // on: a family authored only in plural forms (`invite_select_other_one`) renders, so it is had.
87
92
  has: (key: string): boolean =>
88
- hasExact(key) ||
89
- hasExact(`${key}_other`) ||
90
- hasExact(`${key}_plural`) ||
91
- hasExact(`${key}_one`) ||
92
- hasExact(`${key}_select_other`) ||
93
- hasExact(`${key}_ordinal_other`),
93
+ [key, `${key}_select_other`, `${key}_ordinal_other`].some((stem) =>
94
+ CHAIN_ENDS.some((end) => hasExact(stem + end)),
95
+ ),
94
96
  raw: (key: string): string | undefined => (hasExact(key) ? catalog[key] : undefined),
95
97
  keys: (): string[] => Object.keys(catalog).sort(),
96
98
  locale,