5

I would like to know what's the best method to redirect a site depending on the browsers language settings.

I want to redirect the following browser locales to /es:

es-ar, es-co, es-cu, es-do, es-gt, es-ht, es-mx, es-ni, es-pe, es-pr, es-sv and of course es and es-es

i know it could be done via .htaccess but the following did not work at all:

 RewriteCond %{HTTP:Accept-Language} (es|es-ec|es-mx|es-co) [NC]
 RewriteRule .* http://www.%{HTTP_HOST}/es/ [R,L]

but I think the best would be to do this in the index.php template:

in the /index.php I would need some sort of redirection upon language detection but my php skills are very limited, so maybe someone can show me the right path:

something like:

if ($_SERVER['HTTP_ACCEPT_LANGUAGE'] = "es|es-es|...")
{
    // header( 'Location: http://${_SERVER['HTTP_HOST']}/es/');
    //please help!   
};

any help appreciated!

cheers
stefan

2

This should do it:

$accept_language = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$lang = $accept_language[0];
echo $lang;
switch ($lang)
{
    case 'en-US':
    case 'en':
        echo 'Success!';
        break;
    default:
        echo 'Count on En';
}
|improve this answer|||||
  • thanks for providing this, but I think I found a more elegant solution. – outline4 Feb 18 '13 at 16:42
0

After looking on the interwebs I found a solution that suits my needs for the moment. I guess a real solution would involve cookies and some more checks.

I put this in the index.php file.

$lc = ""; // Initialize the language code variable
// Check to see that the global language server variable isset()
// If it is set, we cut the first two characters from that string
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
    $lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
// Now we simply evaluate that variable to detect specific languages
if($lc == "es"){
    header("location: /es");
    exit();
} 

it's great, because it always checks for ALL of the locales (es-es, es...)! but it gets executed on every page load... this is certainly no good...

cheers stefan

btw: If somebody knows a better solution, please don't hesitate to post it... it's still not a 100% satisfactory for me.

|improve this answer|||||
  • That's only taking the 1st two characters. It will work for "en", but not "en-US". The solution I posted splits on ',', getting the 1st segment, and then looks at any value in the 1st segment. The switch/case syntax lets you set specific cases or "fall through" to the default. – Mark J. Reeves Feb 18 '13 at 20:30
  • Also note that you can "stack" cases in the example I provided, combining multiple values into 1 route. – Mark J. Reeves Feb 19 '13 at 16:47
-1

No cookies needed.

What I do is to redirect the homepage to the relevent language section of my site based on HTTP_ACCEPT_LANGUAGE only if the visitor is coming from a link external to my site or by typing the domain directly in the browser. I determine this by using HTTP_HOST and HTTP_REFERER.

So if he arrives externally on the homepage (english) with an italian browser he gets redirected to the italian section of my site. If he then clicks on the "english" link on that page he get's to see the "english" homepage without being redirected.

Here's the ASP code I use. It should be quite readable and easy to port into other programming languages.

strHTTP_HOST = request.servervariables("HTTP_HOST")
strHTTP_REFERER = request.servervariables("HTTP_REFERER")
strHTTP_ACCEPT_LANGUAGE = left(request.servervariables("HTTP_ACCEPT_LANGUAGE")&"",2)

If (GetDomainName(strHTTP_REFERER) <> GetDomainName(strHTTP_HOST)) And (strHTTP_ACCEPT_LANGUAGE="it") Then
  response.redirect "index_it.asp"
End If


Function GetDomainName(Domain)

  z = Domain
  If z <> "" then
    If InStr(z, "//") <> 0 Then ' take of http:// or http:// etc.
      z = Right(z, Len(z) - InStr(z, "//") - 1)
    End If
    If InStr(z, "/") <> 0 Then ' strip off paths
      z = Left(z, InStr(z, "/") - 1)
    End If
    If InStr(z, ".") <> InStrRev(z, ".") <> 0 Then          ' get 2nd leveldomain
      z = Right(z, Len(z) - InStrRev(z, ".", Len(z) - 4)) ' basically gets the location of the second point
    End If                          ' assumes that domain is at keast 2 chars !
  End If
    GetDomainName = z

End Function
|improve this answer|||||
  • ASP code has zero relevance to ExpressionEngine, which runs under PHP. – Derek Hogue Mar 19 '15 at 13:46
  • @Derek Hogue Hi .. the poster in the solution to his own question wrote "it gets executed on every page load... this is certainly no good .... If somebody knows a better solution please post it". Maybe my solution was better as a comment on his solution. My solution solves his problem. I don't know PHP so I thought that posting the commented ASP code would be more helpful than a solely descriptive reply. I had searched the internet widely before finding the solution myself and wanted to share. One lives and learns and the newbie bows down to the higher god. – Delta2 Apr 29 '15 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.