my-homelab-configs/apps/website/lang_helper.php

30 lines
782 B
PHP

<?php
// lang_helper.php
// Include this at the top of every page.
// Provides: $lang, $text, $en, $availableLangs
$availableLangs = array_map(
fn($f) => basename($f, '.php'),
glob(__DIR__ . '/lang/*.php')
);
function getLang($supported) {
if (isset($_GET['lang']) && in_array($_GET['lang'], $supported)) {
return $_GET['lang'];
}
$browser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? 'nah', 0, 2);
return in_array($browser, $supported) ? $browser : 'nah';
}
$lang = getLang($availableLangs);
$file = __DIR__ . "/lang/$lang.php";
if (!file_exists($file)) {
$lang = 'nah';
$file = __DIR__ . "/lang/nah.php";
}
// Always load English as translation source
$en = include __DIR__ . '/lang/en.php';
$text = array_replace($en, include $file);