mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-11 05:42:19 +00:00
`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.
48 lines
1.4 KiB
TypeScript
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);
|
|
}
|