All files / src/lib/i18n translate.tsx

100% Statements 23/23
87.5% Branches 14/16
100% Functions 5/5
100% Lines 20/20

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80108x                                                       206327x 206327x   20333x             35824x 35824x 204914x                                         11251x 11251x 11251x 6745x 6745x 6745x   6745x 12728x 12728x 12728x     12728x   6745x 6745x    
"use client";
 
/**
 * The interface in the language picked in the menu bar (#782, phase 2).
 *
 * English is the source and the key: a dictionary maps each English text to
 * its translation (ui/src/messages/<lang>/<area>.json). A text with no
 * translation shows in English; __tests__/unit/lib/i18n-messages.test.ts
 * lists what each language is missing and holds legal terms to
 * ui/src/lib/i18n/ehds-terminology.json. The language is the picker's
 * (lib/use-language.ts); pages keep their URLs (ADR-070).
 *
 *   const t = useT("nav");   t("Dataset Catalog")        in a client component
 *   <T area="home" tags={{ b: <strong /> }}>Run <b>FHIR R4</b></T>
 *                                                      in a server component
 */
import { cloneElement, type ReactElement, type ReactNode } from "react";
import { useLanguage } from "@/lib/use-language";
import type { CatalogLanguage } from "@/lib/metadata-language";
import { MESSAGES, type Area } from "@/messages";
 
/** The text in a language, or the English one when it has none. */
export function translate(
  lang: CatalogLanguage,
  area: Area,
  text: string,
  vars?: Record<string, string | number>,
): string {
  const out = lang === "en" ? text : MESSAGES[lang]?.[area]?.[text] ?? text;
  return vars
    ? out.replace(/\{(\w+)\}/g, (m, k: string) =>
        k in vars ? String(vars[k]) : m,
      )
    : out;
}
 
/** A translator for one area, in the language the picker holds. */
export function useT(area: Area) {
  const lang = useLanguage();
  return (text: string, vars?: Record<string, string | number>) =>
    translate(lang, area, text, vars);
}
 
/**
 * A translated text, for a server component, which cannot call a hook.
 * `tags` turns marked parts into elements: in "Run <b>FHIR R4</b>" the part
 * "FHIR R4" is rendered inside a copy of tags.b. The elements are made by
 * the server component (a link, a <strong>), so the translation moves the
 * words and keeps the markup; tags do not nest.
 */
export function T({
  area,
  children,
  vars,
  tags,
}: {
  area: Area;
  children: string;
  vars?: Record<string, string | number>;
  tags?: Record<string, ReactElement<{ children?: ReactNode }>>;
}): ReactNode {
  const t = useT(area);
  const text = t(children, vars);
  if (!tags) return text;
  const parts: ReactNode[] = [];
  const re = /<(\w+)>([\s\S]*?)<\/\1>/g;
  let last = 0;
  let m: RegExpExecArray | null;
  while ((m = re.exec(text))) {
    if (m.index > last) parts.push(text.slice(last, m.index));
    const el = tags[m[1]];
    parts.push(
      el ? cloneElement(el, { key: `${m[1]}-${m.index}` }, m[2]) : m[2],
    );
    last = re.lastIndex;
  }
  if (last < text.length) parts.push(text.slice(last));
  return <>{parts}</>;
}