From 38586826e26583cab2e86a551a967d6df1cc7523 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 30 Aug 2024 09:16:07 +0200 Subject: [PATCH] feat(language): improve language detection and file loading Simplify and enhance language detection and file loading mechanism: - Parse primary language from browser settings more accurately. - Remove redundant scanning of language files. - Load default English language file as a base. - Add support for region-specific language files if available. - Remove unused language strings from various translation files. These changes enhance the robustness and efficiency of language handling, ensuring better support for user language preferences. --- index.php | 44 ++++++++++++++++++++++++-------------------- lang/cs.php | 8 +------- lang/el.php | 4 ---- lang/es.php | 4 ---- lang/fa.php | 4 ---- lang/it.php | 4 ---- lang/pt-br.php | 2 +- lang/zh-hans.php | 6 ------ 8 files changed, 26 insertions(+), 50 deletions(-) diff --git a/index.php b/index.php index 41a1e51..d27e711 100644 --- a/index.php +++ b/index.php @@ -42,18 +42,12 @@ foreach ($currencies as $currency) { $exchangeRates[$currency] = $api_cg[strtolower($currency)]['lastValue']; } -// Get the browser language +// Get the primary language from the browser $lang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "en"; +$lang = explode(",", $lang)[0]; +$lang = explode(";", $lang)[0]; $lang = strtolower($lang); -// Scan the lang/ directory for available language files -$langFiles = glob('lang/*.php'); -$acceptLang = []; -foreach ($langFiles as $file) { - $langCode = basename($file, '.php'); - $acceptLang[] = strtolower($langCode); -} - // Aliases for different Chinese variants $aliases = [ 'zh' => 'zh-hans', @@ -64,22 +58,32 @@ $aliases = [ 'zh-mo' => 'zh-hant', ]; -if (isset($aliases[$lang])) { - $lang = $aliases[$lang]; +// Load the language files +// Take English as a base + +require_once 'lang/en.php'; + +// Get the browser language code (e.g. 'de' for 'de-DE') +// Load that language file if available + +$language_code = explode('-', $lang)[0]; + +if (file_exists('lang/' . $language_code . '.php')) { + require_once 'lang/' . $language_code . '.php'; } -// 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'; +// If a region-specific language file is available, load that one + +if ($language_code != $lang) { + if (isset($aliases[$language_code])) { + $lang = $aliases[$language_code]; + } + + if (file_exists('lang/' . $lang . '.php')) { + require_once 'lang/' . $lang . '.php'; } } -require_once "lang/{$lang}.php"; - // Calculation through GET parameters $xmr_in = isset($_GET["in"]) ? strtoupper(htmlspecialchars($_GET["in"])) : 'EUR'; diff --git a/lang/cs.php b/lang/cs.php index 3e212db..c7d1f2c 100644 --- a/lang/cs.php +++ b/lang/cs.php @@ -1,4 +1,3 @@ -```php $lang_meta = "cs"; // https://www.w3schools.com/tags/ref_language_codes.asp @@ -17,10 +16,6 @@ $servers_guru = " | Oficiální web | Systém komunitního crowdfundingu (CCS) | Monero Observer | Monero Talk'; $countrymonero = ' | Telegram - Monero XMR'; -?> -``` \ No newline at end of file +?> \ No newline at end of file diff --git a/lang/el.php b/lang/el.php index 67671f8..978415d 100644 --- a/lang/el.php +++ b/lang/el.php @@ -16,10 +16,6 @@ $servers_guru = " | Moner.ooo $info = "本站提供的汇率仅供参考。其精确程度不被保证,且可能在您不注意的情况下发生变更。汇率大约每分钟更新一次。最近一次在 Europe/Berlin 时间 ".$time." 。数据由 CoinGecko
Kuno - Moner.ooo donation page Kuno – Fundraise with Monero | GitHub"; $servers_guru = " | Webhosting provided byServers Guru"; -$clipboard_copy_tooltip = "Copy to clipboard"; - -$l_fiatSelect = "Currency choice"; -$l_fiatInput = "Fiat value input field"; -$l_xmrInput = "Monero value input field"; - // Tooltip Titel $l_eur = "欧元"; $l_btc = "比特币";