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 | 111x 111x 111x 111x 63x 63x 18388x 18388x 37541x 37541x 18388x 18388x 651x 18388x 18388x 37541x | "use client";
/**
* The language the visitor reads the hub in (#782), picked in the menu.
*
* Today it sets the language of the dataset descriptions; the interface is
* English until it is translated (#782, phase 2), and then this is its
* language too. Order: ?lang= in the address, the stored choice, the
* browser's language, English (initialLanguage in metadata-language.ts).
* A module-level EventTarget tells every component in the tab at once, as
* use-demo-persona.ts does (the storage event only reaches other tabs).
*/
import { useEffect, useState } from "react";
import {
type CatalogLanguage,
initialLanguage,
storedLanguage,
storeLanguage,
} from "@/lib/metadata-language";
/** The languages the menu offers: those the hub has descriptions in. */
export const OFFERED_LANGUAGES: CatalogLanguage[] = [
"en",
"de",
"fr",
"nl",
"es",
];
const emitter: EventTarget | null =
typeof window !== "undefined" ? new EventTarget() : null;
const CHANGE = "language-change";
/** The visitor picks a language: stored, and every reader in the tab told. */
export function setLanguage(lang: CatalogLanguage): void {
storeLanguage(lang);
emitter?.dispatchEvent(new CustomEvent(CHANGE, { detail: lang }));
}
function currentLanguage(): CatalogLanguage {
const fromUrl =
typeof window === "undefined"
? null
: new URLSearchParams(window.location.search).get("lang");
return initialLanguage(OFFERED_LANGUAGES, fromUrl, storedLanguage());
}
/** The language to show. English on the server and the first render. */
export function useLanguage(): CatalogLanguage {
const [lang, setLang] = useState<CatalogLanguage>("en");
useEffect(() => {
setLang(currentLanguage());
const onChange = (e: Event) =>
setLang((e as CustomEvent<CatalogLanguage>).detail);
emitter?.addEventListener(CHANGE, onChange);
return () => emitter?.removeEventListener(CHANGE, onChange);
}, []);
return lang;
}
|