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.
This commit is contained in:
Kumi 2024-08-30 09:16:07 +02:00
parent 25f6bb423e
commit 38586826e2
Signed by: kumi
GPG key ID: ECBCC9082395383F
8 changed files with 26 additions and 50 deletions

View file

@ -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';

View file

@ -1,4 +1,3 @@
```php
<?php
// For the HTML meta specification, e.g. <!DOCTYPE html><html lang="en">
$lang_meta = "cs"; // https://www.w3schools.com/tags/ref_language_codes.asp
@ -17,10 +16,6 @@ $servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='te
$clipboard_copy_tooltip = "Kopírovat do schránky";
$l_fiatSelect = "Currency choice";
$l_fiatInput = "Fiat value input field";
$l_xmrInput = "Monero value input field";
// Tooltip Titel
$l_eur = "Euro";
$l_btc = "Bitcoin";
@ -88,4 +83,3 @@ $getmonero = '<a class="text-white" href="https://www.getmonero.org/" hreflang="
$countrymonero = ' | <a class="text-white" href="https://t.me/monero" hreflang="en" target="_blank" rel="external">Telegram - Monero XMR</a>';
?>
```

View file

@ -16,10 +16,6 @@ $servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='te
$clipboard_copy_tooltip = "Αντιγραφή στο πρόχειρο";
$l_fiatSelect = "Currency choice";
$l_fiatInput = "Fiat value input field";
$l_xmrInput = "Monero value input field";
// Tooltip Titel
$l_eur = "Ευρώ";
$l_btc = "Bitcoin";

View file

@ -16,10 +16,6 @@ $servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='te
$clipboard_copy_tooltip = "Copiar al portapapeles";
$l_fiatSelect = "Currency choice";
$l_fiatInput = "Fiat value input field";
$l_xmrInput = "Monero value input field";
// Tooltip Titel
$l_eur = "Euro";
$l_btc = "Bitcoin";

View file

@ -16,10 +16,6 @@ $servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='te
$clipboard_copy_tooltip = "کپی به کلیپ‌بورد";
$l_fiatSelect = "Currency choice";
$l_fiatInput = "Fiat value input field";
$l_xmrInput = "Monero value input field";
// Tooltip Titel
$l_eur = "یورو";
$l_btc = "بیت‌کوین";

View file

@ -16,10 +16,6 @@ $servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='te
$clipboard_copy_tooltip = "Copia negli appunti";
$l_fiatSelect = "Currency choice";
$l_fiatInput = "Fiat value input field";
$l_xmrInput = "Monero value input field";
// Tooltip Titel
$l_eur = "Euro";
$l_btc = "Bitcoin";

View file

@ -1,6 +1,6 @@
<?php
// For the HTML meta specification, e.g. <!DOCTYPE html><html lang="en">
$lang_meta = "pt"; // https://www.w3schools.com/tags/ref_language_codes.asp
$lang_meta = "pt-BR"; // https://www.w3schools.com/tags/ref_language_codes.asp
$page_title = "Converter XMR para BRL/EUR/BTC/USD e muito mais"; // The browser tab title or search engine title
$meta_description = "A cotação em tempo real de Monero em várias moedas, de graça a todos."; // Search engine description / text

View file

@ -14,12 +14,6 @@ $moneroooTable = "Service provided by <a href='https://moner.ooo/'>Moner.ooo</a>
$info = "本站提供的汇率仅供参考。其精确程度不被保证,且可能在您不注意的情况下发生变更。汇率大约每分钟更新一次。最近一次在 Europe/Berlin 时间 <u title='Hours:Minutes:Seconds (hh:mm:ss)'>".$time."</u> 。数据由 <a class='text-white' href='https://www.coingecko.com/zh/%E6%95%B0%E5%AD%97%E8%B4%A7%E5%B8%81/%E9%97%A8%E7%BD%97%E5%B8%81' hreflang='zh' rel='external' target='_blank'>CoinGecko</a>。<br/><a target='_blank' href='https://kuno.anne.media/donate/onml/' rel='external' hreflang='en'><img loading='lazy' src='./img/kuno-monero-26x26.png' width='17' height='17' alt='Kuno - Moner.ooo donation page'></a>&nbsp;<a target='_blank' href='https://kuno.anne.media/' class='text-white' rel='external' hreflang='en'>Kuno Fundraise with Monero</a> | <a class='text-white' href='{$github_url}' hreflang='en' rel='external' target='_blank'>GitHub</a>";
$servers_guru = " | <a style='text-decoration:none; font-weight:bold;' class='text-white' href='https://servers.guru/' hreflang='en' rel='external' target='_blank'>Webhosting provided by<img loading='lazy' src='./img/servers-guru.svg' height='19' alt='Servers Guru' title='Servers Guru' /></a>";
$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 = "比特币";