mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-27 11:09:20 +03:00
Improved language detection code
This commit is contained in:
parent
02e6198f17
commit
3bac68dffb
3 changed files with 18 additions and 36 deletions
|
|
@ -30,38 +30,15 @@ trait Localization
|
|||
|
||||
public function ValidateLanguage(string $sLanguage, string $sDefault = '', bool $bAdmin = false, bool $bAllowEmptyResult = false): string
|
||||
{
|
||||
$aLang = \SnappyMail\L10n::getLanguages($bAdmin);
|
||||
$sResult = \SnappyMail\L10n::validLanguage($sLanguage, $bAdmin)
|
||||
?: \SnappyMail\L10n::validLanguage($sDefault, $bAdmin);
|
||||
|
||||
$sLanguage = \strtr($sLanguage, '_', '-');
|
||||
$sDefault = \strtr($sDefault, '_', '-');
|
||||
|
||||
if (\in_array($sLanguage, $aLang)) {
|
||||
return $sLanguage;
|
||||
}
|
||||
|
||||
if (\str_contains($sLanguage, '-')) {
|
||||
$sLanguage = \strtok($sLanguage, '-');
|
||||
if (\in_array($sLanguage, $aLang)) {
|
||||
return $sLanguage;
|
||||
}
|
||||
}
|
||||
|
||||
if (\in_array($sDefault, $aLang)) {
|
||||
return $sDefault;
|
||||
}
|
||||
if (\str_contains($sDefault, '-')) {
|
||||
$sDefault = \strtok($sDefault, '-');
|
||||
if (\in_array($sDefault, $aLang)) {
|
||||
return $sDefault;
|
||||
}
|
||||
}
|
||||
|
||||
if ($bAllowEmptyResult) {
|
||||
return '';
|
||||
if ($sResult || $bAllowEmptyResult) {
|
||||
return $sResult ?: '';
|
||||
}
|
||||
|
||||
$sResult = $this->Config()->Get($bAdmin ? 'admin_panel' : 'webmail', 'language', 'en');
|
||||
return \in_array($sResult, $aLang) ? $sResult : 'en';
|
||||
return \SnappyMail\L10n::validLanguage($sResult, $bAdmin) ? $sResult : 'en';
|
||||
}
|
||||
|
||||
public function detectClientLanguage(bool $bAdmin): string
|
||||
|
|
|
|||
|
|
@ -138,14 +138,7 @@ class Application extends \RainLoop\Config\AbstractConfig
|
|||
}
|
||||
}
|
||||
if ('language' === $sParamKey) {
|
||||
$aLang = \SnappyMail\L10n::getLanguages('admin_panel' === $sSectionKey);
|
||||
$sLanguage = \strtr($mParamValue, '_', '-');
|
||||
if (!\in_array($sLanguage, $aLang)) {
|
||||
if (\str_contains($sLanguage, '-')) {
|
||||
$sLanguage = \strtok($sLanguage, '-');
|
||||
}
|
||||
}
|
||||
$mParamValue = \in_array($sLanguage, $aLang) ? $sLanguage : 'en';
|
||||
$mParamValue = \SnappyMail\L10n::validLanguage($mParamValue, 'admin_panel' === $sSectionKey) ?: 'en';
|
||||
}
|
||||
parent::Set($sSectionKey, $sParamKey, $mParamValue);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,18 @@ abstract class L10n
|
|||
return $aCache[$bAdmin];
|
||||
}
|
||||
|
||||
public static function validLanguage(string $sLanguage, bool $bAdmin) : ?string
|
||||
{
|
||||
$aLang = static::getLanguages($bAdmin);
|
||||
$sLanguage = \strtr($sLanguage, '_', '-');
|
||||
if (!\in_array($sLanguage, $aLang)) {
|
||||
if (\str_contains($sLanguage, '-')) {
|
||||
$sLanguage = \strtok($sLanguage, '-');
|
||||
}
|
||||
}
|
||||
return \in_array($sLanguage, $aLang) ? $sLanguage : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* When $sLanguage is like 'sv-SE', it tries to load and merge (in order): en, sv and sv-SE
|
||||
* $sFile is either 'admin', 'static' or 'user'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue