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