Centralize language loading with English fallback (#361)

This commit is contained in:
Fabian
2026-07-31 06:12:33 +01:00
committed by GitHub
parent 4f9bc3c37c
commit a1a714b062
19 changed files with 260 additions and 40 deletions
+3 -2
View File
@@ -21,7 +21,8 @@
#################################################################################
include_once("config.php");
include_once("Lang/" . LANG . ".php");
require_once __DIR__ . "/Lang/loader.php";
tz_load_language(LANG);
/**
* Ensure input exists (avoid undefined variable issues)
@@ -207,4 +208,4 @@ $bbcoded = preg_replace_callback(
$bbcoded = preg_replace('/\[message\]/', '', $bbcoded);
$bbcoded = preg_replace('/\[\/message\]/', '', $bbcoded);
?>
?>
+53
View File
@@ -0,0 +1,53 @@
<?php
/**
* Load one interface language and fill its gaps from English.
*
* Language files define constants through tz_def(), so loading the selected
* locale first preserves translated constants while English defines only the
* missing ones. Their legacy $lang arrays need an explicit merge because those
* assignments are not guarded.
*/
if (!function_exists('tz_language_array_from_file')) {
function tz_language_array_from_file($file)
{
$lang = [];
require $file;
return is_array($lang) ? $lang : [];
}
}
if (!function_exists('tz_load_language')) {
function tz_load_language($language, $languageDirectory = __DIR__)
{
global $lang;
$language = strtolower(trim((string) $language));
if (!preg_match('/\A[a-z_]+\z/', $language)) {
$language = 'en';
}
$languageDirectory = rtrim($languageDirectory, '/\\');
$englishFile = $languageDirectory . '/en.php';
if (!is_file($englishFile)) {
throw new RuntimeException('English language fallback not found: ' . $englishFile);
}
$selectedFile = $languageDirectory . '/' . $language . '.php';
if (!is_file($selectedFile)) {
$selectedFile = $englishFile;
}
$localized = tz_language_array_from_file($selectedFile);
if ($selectedFile === $englishFile) {
$lang = $localized;
return $lang;
}
$english = tz_language_array_from_file($englishFile);
$lang = array_replace_recursive($english, $localized);
return $lang;
}
}
+3 -7
View File
@@ -60,12 +60,8 @@ include_once("Form.php");
include_once("Generator.php");
include_once("Multisort.php");
include_once("Ranking.php");
include_once("Lang/" . LANG . ".php");
// en.php is an idempotent fallback (tz_def only defines missing keys); loading
// it after the player language guarantees every interface constant is defined,
// so a key missing from a translation degrades to English instead of a PHP 8.3
// "undefined constant" fatal (blank page). See issue #189.
include_once("Lang/en.php");
require_once __DIR__ . "/Lang/loader.php";
tz_load_language(LANG);
include_once("Logging.php");
include_once("Message.php");
include_once("Alliance.php");
@@ -585,4 +581,4 @@ if (!empty($_SESSION['id_user'])) {
$message = new Message;
$user = new User((int)$_SESSION['id_user'], $database);
}
?>
?>