用户语言重定向 + root 上的默认语言:它不起作用
抱歉我的近似英语。
我使用此方法根据用户语言提供重定向,并将数据保存在 cookie 中: http://dallascao.com/en/use-cookies-to-remember/
但是,如果我决定选择域根作为默认语言,则重定向不起作用,如下所示:
<?php $lang=$_COOKIE["lang"];
switch ($lang) {
case 'en':
header('Location: http://hawalove.com/');
break;
case 'fr':
header('Location: http://www.hawalove.com/fr');
break;
#Get the default language of the browser if no cookies are found.
default:
$lang = getDefaultLanguage();
switch ($lang) {
case 'fr' :
header('Location: http://www.hawalove.com/fr');
break;
default:
header('Location: http://hawalove.com/');
break;
}
break;
}
?>
您可以帮助我实现此目的吗?我希望在 root (mydomain.com) 上有英语版本,在 mydomain.com/fr 上有法语版本。
谢谢。
Sorry for my approximative english.
I'm using this method to provide a redirection according user language, and keep the data in cookies:
http://dallascao.com/en/use-cookies-to-remember/
However, the redirection doesn't work if I decide to choose the domain root as the default language, like this way:
<?php $lang=$_COOKIE["lang"];
switch ($lang) {
case 'en':
header('Location: http://hawalove.com/');
break;
case 'fr':
header('Location: http://www.hawalove.com/fr');
break;
#Get the default language of the browser if no cookies are found.
default:
$lang = getDefaultLanguage();
switch ($lang) {
case 'fr' :
header('Location: http://www.hawalove.com/fr');
break;
default:
header('Location: http://hawalove.com/');
break;
}
break;
}
?>
May you help me to achieve this ? I'd like to have english version at root (mydomain.com) and french version at mydomain.com/fr.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜想“不起作用”意味着“英语使用者的无限重定向循环”。
您所需要的只是一种当所选语言为英语时根本不重定向的方法。添加一个关联数组,将支持的语言映射到重定向位置,然后执行以下操作:
$_COOKIE["lang"]
获取$lang
。$lang
不是受支持的语言(或者甚至未设置),则$lang = getDefaultLanguage()
。$lang
不是'en'
,则重定向,否则显示英文主页而不重定向。您可以将关联数组用于 (2) 和 (3)。基本策略很简单:如果语言最终是英语,则根本不重定向。一旦您也有了有效的语言,您可能会想要设置您的语言 cookie。
I'd guess that "doesn't work" means "infinite redirection loops for English speakers".
All you need is a way to not redirect at all when the chosen language is English. Add an associative array which maps the supported languages to the redirect location then do this:
$lang
from$_COOKIE["lang"]
.$lang
is not a supported language (or not even set) then$lang = getDefaultLanguage()
.$lang
is not'en'
, then redirect, otherwise display the English homepage without redirecting at all.You can use the associative array for (2) and (3). The basic strategy is simple: don't redirect at all if the language ends up being English. You'd probably want to set your language cookie once you have a valid language too.