rinkplanner/web/bootstrap.php
torsten 9819311403 feat: Website und Hub zweisprachig (Deutsch/Englisch)
Website (rinkplanner.programmschmiede.de) liegt jetzt im Repo unter web/:
Sprachweiche nach Accept-Language (der User-Agent enthält keine Sprache),
/de/ und /en/ als eigene URLs mit hreflang, Cookie für die manuelle Wahl,
Texte in web/i18n.php, Markup in web/template.php. Deployment über
tools/deploy_web.sh (--sync, --check, --all); /index.html leitet weiter.

Hub: optionale englische Oberfläche über den DE/EN-Umschalter, Cookie
RHUB_LANG, Route /language. Alle Texte (Views, Meldungen, Validierung,
API-messages) stehen in hub/app/i18n_strings.php und laufen über t();
Datum und Zahlen folgen der Sprache. Die API behält ihre code-Felder und
antwortet ohne Cookie weiter deutsch.

deploy_hub.sh --check prüft jetzt Schlüsselparität und fehlende t()-Schlüssel.
Doku: docs/website.md neu, docs/hub.md und AGENTS.md ergänzt.
2026-11-12 22:38:32 +01:00

121 lines
3.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// Sprachwahl der Rinkplanner-Website.
//
// Die Sprache kommt aus Accept-Language (der User-Agent enthält keine
// Sprachinformation). Reihenfolge der Entscheidung:
// 1. ?lang=de|en – explizite Wahl, wird als Cookie gespeichert
// 2. Cookie lang – frühere Wahl des Besuchers
// 3. Accept-Language
// 4. DEFAULT_LANG
//
// /de/ und /en/ sind eigenständige URLs. "/" leitet nur weiter.
declare(strict_types=1);
const SUPPORTED_LANGS = ['de', 'en'];
const DEFAULT_LANG = 'de';
const LANG_COOKIE = 'lang';
const COOKIE_LIFETIME = 365 * 86400;
/** "de-DE", "EN", "de_AT" → "de"; unbekannte Sprachen → null. */
function normalize_lang(string $value): ?string
{
$primary = substr(strtolower(trim($value)), 0, 2);
return in_array($primary, SUPPORTED_LANGS, true) ? $primary : null;
}
function cookie_lang(): ?string
{
$raw = $_COOKIE[LANG_COOKIE] ?? null;
return is_string($raw) ? normalize_lang($raw) : null;
}
function set_lang_cookie(string $lang): void
{
if (headers_sent()) {
return;
}
$https = ($_SERVER['HTTPS'] ?? '') !== '' && ($_SERVER['HTTPS'] ?? '') !== 'off';
setcookie(LANG_COOKIE, $lang, [
'expires' => time() + COOKIE_LIFETIME,
'path' => '/',
'secure' => $https,
'httponly' => false,
'samesite' => 'Lax',
]);
}
/** Beste unterstützte Sprache aus dem Accept-Language-Header, q-Werte beachtet. */
function accept_language(): ?string
{
$header = $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '';
if (!is_string($header) || trim($header) === '') {
return null;
}
$candidates = [];
foreach (explode(',', $header) as $position => $entry) {
$parts = explode(';', trim($entry));
$lang = normalize_lang($parts[0]);
if ($lang === null) {
continue;
}
$quality = 1.0;
foreach (array_slice($parts, 1) as $parameter) {
if (preg_match('/^\s*q\s*=\s*([0-9.]+)\s*$/i', $parameter, $match) === 1) {
$quality = (float) $match[1];
}
}
if ($quality <= 0) {
continue;
}
// Pro Sprache zählt der beste Treffer; bei Gleichstand gewinnt die
// Reihenfolge im Header.
if (!isset($candidates[$lang]) || $quality > $candidates[$lang][0]) {
$candidates[$lang] = [$quality, $position];
}
}
if ($candidates === []) {
return null;
}
uasort($candidates, static fn (array $a, array $b): int => $b[0] <=> $a[0] ?: $a[1] <=> $b[1]);
return (string) array_key_first($candidates);
}
function redirect_lang(string $lang): never
{
header('Vary: Accept-Language, Cookie');
header('Location: /' . $lang . '/', true, 302);
exit;
}
/** Explizite Sprachwahl aus der URL, z. B. /de/?lang=en. */
function requested_lang(): ?string
{
$raw = $_GET['lang'] ?? null;
return is_string($raw) ? normalize_lang($raw) : null;
}
function render_page(string $lang): never
{
$requested = requested_lang();
if ($requested !== null && $requested !== $lang) {
set_lang_cookie($requested);
redirect_lang($requested);
}
if ($requested === $lang) {
set_lang_cookie($lang);
}
$strings = require __DIR__ . '/i18n.php';
$t = $strings[$lang];
$alt = $lang === 'de' ? 'en' : 'de';
header('Content-Type: text/html; charset=utf-8');
require __DIR__ . '/template.php';
exit;
}