-1
我知道如何檢測PHP中的語言瀏覽器,以及如何檢測用於切換到另一個語言的語言href錨點。添加瀏覽器語言檢測後,語言href錨點(hl)停止工作
我的舊代碼不具有locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
:
if (isset($_GET['hl']))
{
$langOption = $_GET['hl'];
}
else
{
$langOption = '';
}
switch ($langOption):
case 'ca':
$language = 'cat';
break;
case 'el':
$language = 'el';
break;
case 'en':
$language = 'en';
break;
case 'en_GB':
$language = 'en_GB';
break;
case 'es':
$language = 'es';
break;
case 'fr':
$language = 'fr';
break;
case 'ka':
$language = 'ka';
break;
case 'nl':
$language = 'nl';
break;
case 'pt_PT':
$language = 'pt_PT';
break;
case 'pt_BR':
$language = 'pt_BR';
break;
case 'ro':
$language = 'ro';
break;
default:
$language = 'pt_BR';
break;
endswitch;
require_once("idiomas/{$language}.php");
這些舊代碼非常努力語言HREF錨。像:
<a class="waves-effect" href="?hl=en_GB" name="en">English</a>
<a class="waves-effect" href="?hl=pt_BR" name="pt_BR">Português Brasileiro</a>
我改變爲代碼來壓縮,節約和縮短代碼。下面是新的代碼:
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$lang = $_GET['hl'];
}
else
{
$lang = '';
}
switch ($lang):
case $language:
$language = $language;
break;
default:
$language = $language;
break;
endswitch;
require_once("idiomas/{$language}.php");
只有瀏覽器語言的檢測工作非常出色,但語言HREF錨(HL)不工作,因爲如果你用英文還是切換到葡萄牙,頁面。
類似像:
switch ($lang):
case "en":
$language = "en";
break;
case "en_GB":
$language = "en_GB";
break;
case "pt_BR":
$language = "pt_BR";
break;
case "pt_PT":
$language = "pt_PT";
break;
endswitch;
但我不希望重複這些舊代碼。我想保持凝聚並縮短相同的新代碼。