开发者

User language redirect + default language on root : it doesn't work

开发者 https://www.devze.com 2023-04-01 22:40 出处:网络
Sorry for my approximative english. I\'m using this method to provide a redirection according user language, and keep the data in cookies:

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 choo开发者_运维技巧se 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.


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:

  1. Grab $lang from $_COOKIE["lang"].
  2. If $lang is not a supported language (or not even set) then $lang = getDefaultLanguage().
  3. If $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.

0

精彩评论

暂无评论...
验证码 换一张
取 消