From 25f6bb423eceda3d8bfe2b8ef830b988df9275f2 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 30 Aug 2024 08:28:20 +0200 Subject: [PATCH] feat: improve language recognition and alias handling Updated browser language detection to handle full locale strings and mapped various Chinese variants to simplified or traditional Chinese. Renamed language files for consistency with the new alias system. This improves support for a wider array of language settings, ensuring users receive content in their preferred language variant when possible. --- index.php | 35 ++++++++++++++++++++++++++------- lang/{zh-cn.php => zh-hans.php} | 2 +- 2 files changed, 29 insertions(+), 8 deletions(-) rename lang/{zh-cn.php => zh-hans.php} (98%) diff --git a/index.php b/index.php index 9fd3b9c..41a1e51 100644 --- a/index.php +++ b/index.php @@ -43,10 +43,8 @@ foreach ($currencies as $currency) { } // Get the browser language -$lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : "en"; -if ($lang == 'zh' || $lang == 'pt') { - $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 5); -} +$lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "en"; +$lang = strtolower($lang); // Scan the lang/ directory for available language files $langFiles = glob('lang/*.php'); @@ -56,11 +54,34 @@ foreach ($langFiles as $file) { $acceptLang[] = strtolower($langCode); } -// Check if the browser language is available, otherwise use English -$lang = in_array($lang, $acceptLang) ? $lang : 'en'; -$lang = strtolower($lang); +// Aliases for different Chinese variants +$aliases = [ + 'zh' => 'zh-hans', + 'zh-hk' => 'zh-hant', + 'zh-tw' => 'zh-hant', + 'zh-cn' => 'zh-hans', + 'zh-sg' => 'zh-hans', + 'zh-mo' => 'zh-hant', +]; + +if (isset($aliases[$lang])) { + $lang = $aliases[$lang]; +} + +// Check if the browser language is supported +if (!in_array($lang, $acceptLang)) { + // Try again without the region code (if present, e.g. en-US -> en) + $lang = explode('-', $lang)[0]; + if (!in_array($lang, $acceptLang)) { + // Default to English if the browser language is not supported + $lang = 'en'; + } +} + require_once "lang/{$lang}.php"; +// Calculation through GET parameters + $xmr_in = isset($_GET["in"]) ? strtoupper(htmlspecialchars($_GET["in"])) : 'EUR'; $xmr_amount = isset($_GET["xmr"]) ? floatval($_GET["xmr"]) : 1; $fiat_amount = isset($_GET["fiat"]) ? floatval($_GET["fiat"]) : ''; diff --git a/lang/zh-cn.php b/lang/zh-hans.php similarity index 98% rename from lang/zh-cn.php rename to lang/zh-hans.php index 4e54b44..7680439 100644 --- a/lang/zh-cn.php +++ b/lang/zh-hans.php @@ -1,6 +1,6 @@ -$lang_meta = "zh"; // https://www.w3schools.com/tags/ref_language_codes.asp +$lang_meta = "zh-Hans"; // https://www.w3schools.com/tags/ref_language_codes.asp $page_title = "门罗币与欧元/BTC/CHF/美元等币种汇率"; // The browser tab title or search engine title $meta_description = "门罗币实时多币种汇率,免费提供。"; // Search engine description / text