Files
HealthLog/src/lib/i18n/localised-label-index.ts
T
Marc-André Bombeck bec4cb3dfb fix(mcp): let five of the six shipped languages name a metric
Three resolvers turned a user-authored word into an internal code by
comparing it against a hand-typed English list. The app ships six
languages, so five of them resolved nothing: `Fer` and `Żelazo` came back
as `unknown_nutrient` while the settings card rendered exactly those
words, and `poids` and `sommeil` reached no series at all.

Stop transcribing. `nutrients.names.*` and `measurements.type*` already
label these concepts in every bundle, so the match index is derived from
`locales` instead — a seventh language works the day its bundle lands,
with no edit at any call site. The nutrient tool's English labels now come
out of `messages/en.json` too, so the wire wording cannot drift from the
screen.

The MCP contract is unchanged in shape and stays protocol-level English.
What moves is which words are understood: `unknown_nutrient` /
`unknown_metric` still mean "could not place this word", distinct from a
placed name answering `no_data`. Nothing turns absence into data.

Two findings the sweep did not predict:

- `Puls` resolved to pulse-wave velocity, because the English
  display-name pass matches substrings and "pulse-wave velocity" contains
  it. An exact name now beats a loose English fragment.
- French and Italian use one phrase for body fat and fat mass, and three
  bundles use one for fat-free mass and lean body mass. A word two
  metrics claim resolves to neither; answering kilograms to someone who
  asked for a percentage is worse than saying the word was not
  recognised. The known set is pinned so a new collision surfaces.

The medication needles are the exception: they are drug names, with no
bundle to derive from. They fold to a normalised INN stem rather than to
a language, so `Metformin`, `Metformine`, `Metformina` and `Metforminum`
are one entry, and `Semaglutyd` and `Atorwastatyna` cost nothing to add.
A guard asserts no two molecules collapse onto one stem. Iron is the one
class with no stem to fold to — it is named for the element — so its word
list is hand-tabulated and says so.

Every English resolution that worked before returns the same answer.
2026-08-23 14:46:10 +02:00

140 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Match indexes derived from the shipped message bundles.
*
* The recurring defect this closes: a resolver that has to turn a
* user-authored word ("Fer", "Żelazo", "Sommeil") into an internal code
* transcribes the ENGLISH display names into a hand-typed table in the
* source. Five of the six shipped locales then resolve to nothing, even
* though their translations are already on disk in `messages/<locale>.json`
* and are already what the app renders for the same concept.
*
* The fix is to stop transcribing. A caller names the i18n key that already
* labels the concept; this module reads that key out of EVERY entry in
* `locales` and builds the folded-name → value index. Adding a locale to
* `locales` and shipping its bundle is then the whole change — no edit here,
* and no edit at any call site.
*
* SERVER-ONLY, like `shared-resolve` (whose `allMessages` it reads): pulling
* this into a client chunk would drag all six catalogs back in. Both current
* consumers are MCP reads, which are server-side by construction.
*
* Deliberately NOT a translation layer for OUTPUT. The MCP surface answers in
* protocol-level English on purpose (see `nutrients-read.ts` and the
* `metric-status-registry`'s `displayName` comment); this module only widens
* what the resolver ACCEPTS.
*/
import { locales, type Locale } from "./config";
import { allMessages, resolveKey } from "./shared-resolve";
/**
* Letters Unicode decomposition leaves alone because their mark is part of
* the glyph rather than a combining character — a stroke, or a ligature. NFKD
* turns "ż" into "z" + a dot but leaves "ł" whole, so Polish would fold half
* way without this. Expressed as ASCII equivalents, the same target the
* combining-mark strip below produces.
*/
const UNDECOMPOSED_LETTERS: ReadonlyArray<readonly [RegExp, string]> = [
[/ł/g, "l"],
[/ø/g, "o"],
[/đ/g, "d"],
[/ß/g, "ss"],
[/æ/g, "ae"],
[/œ/g, "oe"],
];
/**
* Fold a label or a free-text term to its match form: compatibility-normalised
* (so "VO₂" and "VO2" agree), diacritics stripped (so "Glycémie" answers to
* "glycemie" and "Siła" to "sila"), lower-cased, and every run of
* non-alphanumerics collapsed to a single `_` with the ends trimmed (so
* "Pain (0–10)" and "pain 0 10" agree).
*
* Applied to BOTH sides of every comparison — a bundle label and the caller's
* word go through the same function, so the index cannot drift from the lookup.
*/
export function foldLabel(raw: string): string {
let folded = raw
.normalize("NFKD")
.replace(/\p{Diacritic}/gu, "")
.toLowerCase();
for (const [pattern, ascii] of UNDECOMPOSED_LETTERS) {
folded = folded.replace(pattern, ascii);
}
return folded.replace(/[^\p{L}\p{N}]+/gu, "_").replace(/^_+|_+$/gu, "");
}
/** One locale's rendering of `key`, or `null` when that bundle omits it. */
export function labelIn(locale: Locale, key: string): string | null {
const value = resolveKey(allMessages[locale], key);
return typeof value === "string" && value.trim() !== "" ? value : null;
}
/** Every shipped locale's rendering of `key`, skipping bundles that omit it. */
export function labelsInEveryLocale(key: string): string[] {
const out: string[] = [];
for (const locale of locales) {
const label = labelIn(locale, key);
if (label !== null) out.push(label);
}
return out;
}
/** One concept: a stable identity, the i18n key that labels it, its payload. */
export interface LocalisedLabelEntry<T> {
/**
* Stable identity of the concept. Two entries sharing an id are the same
* thing (a label they both fold to is not an ambiguity); two entries with
* DIFFERENT ids that fold to one string are, and the string is dropped.
*/
id: string;
/** The i18n key whose per-locale renderings become accepted spellings. */
messageKey: string;
/** What a match resolves to. */
value: T;
}
export interface LocalisedLabelIndex<T> {
/** Folded label → value. Ambiguous folds are absent, never guessed. */
index: ReadonlyMap<string, T>;
/**
* Folds two different ids both claimed. Dropped from `index` rather than
* resolved to whichever entry happened to be built first — a resolver that
* guesses between two concepts is worse than one that answers "not
* recognised". Consumers assert this is empty.
*/
ambiguous: ReadonlySet<string>;
}
/**
* Build the folded-label → value index across every shipped locale.
*
* A bundle that omits an entry's key simply contributes nothing for that
* locale — a partially translated bundle degrades to the locales that do
* carry the key instead of failing the module load.
*/
export function buildLocalisedLabelIndex<T>(
entries: readonly LocalisedLabelEntry<T>[],
): LocalisedLabelIndex<T> {
const index = new Map<string, T>();
const owner = new Map<string, string>();
const ambiguous = new Set<string>();
for (const entry of entries) {
for (const label of labelsInEveryLocale(entry.messageKey)) {
const folded = foldLabel(label);
if (folded === "") continue;
const claimed = owner.get(folded);
if (claimed === undefined) {
owner.set(folded, entry.id);
index.set(folded, entry.value);
continue;
}
if (claimed === entry.id) continue;
ambiguous.add(folded);
index.delete(folded);
}
}
return { index, ambiguous };
}