Show the language picker on every page, targeting the nearest page that answers GET
CI / full-gate (push) Successful in 2m39s

This commit is contained in:
2026-08-04 09:37:03 +02:00
parent 7e4c6940c9
commit 37b88b2fe6
7 changed files with 93 additions and 27 deletions
+4 -7
View File
@@ -32,7 +32,9 @@ export interface I18nRequest {
locale: string;
localeHref: (href: string) => string;
locales: string[];
method: string; // a page rendered in response to a POST has no linkable URL — see localeSwitch
// Where the language picker points — "this page", except after a POST, whose URL may answer no
// GET at all; the host then resolves the nearest page that does (app.ts → switchBase).
switchBase: string;
t: Translate;
url: URL;
}
@@ -50,11 +52,6 @@ export const ENGLISH_LOCALS: I18nLocals = {
};
export function i18nLocals(ctx: I18nRequest): I18nLocals {
const here = `${ctx.url.pathname}${ctx.url.search}`;
// The picker links to this same page in another language. After a POST that page's URL often has
// no GET at all (the admin's recovery-code screen, say), so linking there would dead-end on a 405
// — and on a re-rendered form it would silently discard what the user typed. Offer nothing.
const linkable = ctx.method === "GET" || ctx.method === "HEAD";
// ctx.localeHref is a no-op unless the URL asked for a locale, so it is also the honest answer to
// "did it?" — asking the function that decides keeps the two from drifting apart.
const carried = ctx.localeHref("/") === "/" ? null : ctx.locale;
@@ -63,7 +60,7 @@ export function i18nLocals(ctx: I18nRequest): I18nLocals {
locale: ctx.locale,
localeHref: (href) => ctx.localeHref(href),
localeParam: carried,
localeSwitch: linkable ? ctx.locales.map((tag) => ({ current: tag === ctx.locale, href: localeHref(here, tag), label: localeLabel(tag), tag })) : [],
localeSwitch: ctx.locales.map((tag) => ({ current: tag === ctx.locale, href: localeHref(ctx.switchBase, tag), label: localeLabel(tag), tag })),
locales: ctx.locales,
t: ctx.t,
};