All files / src localize.ts

100% Statements 6/6
100% Branches 9/9
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                                                  2588547x 10792x 10792x 10792x         2580422x    
/**
 * Picks a text from the caller's locale, or from English.
 *
 * Languages are locale objects (src/locales/), passed to `validate()` and
 * `describeCifOrganisation()` by the caller. English is the only one this
 * module imports, so a bundle contains only the languages the application
 * imports itself (scripts/check-tree-shaking.mjs checks it).
 *
 * Only `validate()` and `describeCifOrganisation()` import this module, so
 * the boolean validators don't bundle any text.
 */
import { en } from "./locales/en";
import type { NifLocale } from "./types";
 
/**
 * Returns `pick(locale)` when `locale` is an object that gives a non-empty
 * string for it, and `pick(en)` otherwise. So the default (`undefined`), a
 * language code string such as `"es"` (pre-release docs) and a locale
 * object that lacks the text or whose message function throws (plain
 * JavaScript) all give English. Never throws.
 */
export function localize(
  locale: unknown,
  pick: (locale: NifLocale) => string
): string {
  if (typeof locale === "object" && locale !== null && locale !== en) {
    try {
      const text = pick(locale as NifLocale);
      if (typeof text === "string" && text !== "") return text;
    } catch {
      // A malformed locale object: English below.
    }
  }
  return pick(en);
}