Files
HealthLog/src/lib/i18n/translate-code.ts
T
Marc-André Bombeck 6e54f33901 refactor(i18n): ask the catalogue for a sentence in one helper
`t()` answers a missing key with the key, so "does this code have a
sentence" was a comparison written inline at every site that wanted a
code's sentence or a fallback, twenty-odd times in several spellings.
translateOrNull, translateCodeOrNull and translateCode in
src/lib/i18n/translate-code.ts now hold that probe, every site uses
them, and a guard refuses the inline comparison anywhere else.
2026-10-20 09:53:31 +02:00

48 lines
1.4 KiB
TypeScript

/**
* Asking the catalogue whether it has a sentence.
*
* `t()` answers a missing key with the key itself, so the only way to learn
* whether a key exists is to compare the answer with the question. That probe
* lives here and nowhere else: it had been written inline at each call site
* that wanted "a code's sentence, or else the fallback", once per site.
* `src/__tests__/translate-probe-guard.test.ts` refuses it outside this file.
*/
type Translate = (
key: string,
params?: Record<string, string | number>,
) => string;
/** The catalogue's sentence for `key`, or null when the bundle has none. */
export function translateOrNull(
t: Translate,
key: string,
params?: Record<string, string | number>,
): string | null {
const value = t(key, params);
return value === key ? null : value;
}
/**
* The sentence for a machine code under `namespace` (`apiErrors`,
* `settings.testConnection.errors`, …), or null when there is no code or the
* code has no sentence of its own.
*/
export function translateCodeOrNull(
t: Translate,
namespace: string,
code: string | null | undefined,
): string | null {
return code ? translateOrNull(t, `${namespace}.${code}`) : null;
}
/** The code's sentence, or the fallback key's when it has none. */
export function translateCode(
t: Translate,
namespace: string,
code: string | null | undefined,
fallbackKey: string,
): string {
return translateCodeOrNull(t, namespace, code) ?? t(fallbackKey);
}