我有一个用3种语言制作的网站,但是(可能)会扩展。 所以我在数据库上创建了一个表格,我在其中插入了网站的语言。
作为index.php我有一个脚本。如果检测到的URI是“/”,则脚本将重定向到/language/index.php。
我现在想要一个基于HTTP_ACCEPT_LANGUAGE的dinamically重定向脚本。 我的问题是,我制作的以下脚本仅适用于第一次foreach运行。
$abbr = $link->query("SELECT abbr AS langs FROM LANGS");
while($langs_db = mysqli_fetch_array($abbr)){
$site_langs[] = $langs_db['langs'];
}
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
foreach($site_langs as $lang){
switch($client_lang){
case $client_lang == $lang:
header("location: http://www.mysite.com/$lang/index.php");
break;
default:
header("location: http://www.mysite.com/$lang/index.php");
break;
}
}
我想的解决方案如下:
case $client_lang == $lang:
$find "location: http://www.mysite.com/$lang/index.php";
break;
default:
$not_find = "location: http://www.mysite.com/$lang/index.php";
break;
然后:
if($find != ''){
header($find);
}else{
header(not_find);
}
但我不确定这是个好主意......
谢谢!
答案 0 :(得分:0)
// Set the default language as a fall back
$default_lang='en';
// Have an array with all languages your site supports
$langs=array('en', 'es', 'pt', /* etc... */);
// Query the browser's first preferred language
$client_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// See if the language is supported otherwise redirect use the default one
$client_lang = (in_array($client_lang, $langs) ? $client_lang : $default_lang);
header("Location: /$client_lang/index.php");
或者,您可能需要考虑gettext: