Is there a way to detect the browser language and perform an action?
For example, if the user's browser is in English, my code performs a function such as redirecting a page or displaying a message
Is there a way to detect the browser language and perform an action?
For example, if the user's browser is in English, my code performs a function such as redirecting a page or displaying a message
One way is with locale_accept_from_http () , to get the "best" local configuration possible and according to the result, you make the redirection you want:
$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if ($lang = 'en_US') {
header('Location: http://www.sitio.com/en');
exit;
}
// ... otras redirecciones
When the browser sends language information in a header, called with HTTP_ACCEPT_LANGUAGE
, we will have a result similar to this:
es,en-us;q=0.3,de;q=0.1
These values indicate that the browser accepts Spanish es
, US English. en-us
and german de
.
Obviously, most browsers do not send as many possibilities, but you get the idea. Most of the code you can find to determine the default language simply looks up the first 2-letter language code's header and returns the first one it finds.
But watching the example, you will notice some additional information q=0.3
- what is that?
As part of the HTTP specification, these are values
q
, and should be a number between0
and1
(default is value1
if no number appears).The
q
values provide not only information to what a browser admits, but also what you prefer.
In the previous example, we see that es
has no value q
, so it equals 1.0
, while en-us
is a 0.3
and de
a 0.1
, so means that this client can handle Spanish, English or German, but prefers Spanish if it is available. If not, the server can send any of the other compatible options.
Now we will see the problem, if we ignore the value q
in HTTP_ACCEPT_LANGUAGE
, we will not have the preferences of the language of the client, if not only the one that supports.
If the heading HTTP_ACCEPT_LANGUAGE
contains several languages with different q
values, such as en, de; q = 0.9
, you will be served the main English language but you really know German, then we have to value the value q
to have a better result appropriate.
With the following code and calling the function getDefaultLanguage()
, we will obtain the value q
highest of the header HTTP_ACCEPT_LANGUAGE
.
#########################################################
# Copyright © 2008 Darrin Yeager #
# https://www.dyeager.org/ #
# Licensed under BSD license. #
# https://www.dyeager.org/downloads/license-bsd.txt #
#########################################################
function getDefaultLanguage() {
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
return parseDefaultLanguage($_SERVER["HTTP_ACCEPT_LANGUAGE"]);
}
else {
return parseDefaultLanguage(NULL);
}
}
function parseDefaultLanguage($http_accept, $deflang = "en") {
if (isset($http_accept) && strlen($http_accept) > 1) {
// dividir los posibles idiomas en un array
$x = explode(",",$http_accept);
foreach ($x as $val) {
// compruebe el valor q y cree un array asociativa. Si no existe el valor q, es por defecto 1
if (preg_match("/(.*);q=([0-1]{0,1}.\d{0,4})/i",$val,$matches)) {
$lang[$matches[1]] = (float)$matches[2];
}
else {
$lang[$val] = 1.0;
}
}
// retornamos el idioma por defecto el cual es el valor q más alto
$qval = 0.0;
foreach ($lang as $key => $value) {
if ($value > $qval) {
$qval = (float)$value;
$deflang = $key;
}
}
}
return strtolower($deflang);
}
Keeping in mind that you may not have translation for all languages , you could proceed in a way like this.
The default
of the block switch
will take you or include the page you want if you do not detect anything or detect a language for which you do not have a translation, such as Icelandic, Danish, Russian. ..
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "es":
//echo "PAGE ES";
include("index_es.php");//puede ser header(Location...) u otra cosa
break;
case "fr":
//echo "PAGE FR";
include("index_fr.php");//puede ser header(Location...) u otra cosa
break;
case "it":
//echo "PAGE IT";
include("index_it.php");
break;
case "en":
//echo "PAGE EN";
include("index_en.php");
break;
default:
//echo "PAGE EN - Configuración por defecto";
include("index_en.php");//incluye página en inglés, o en otro idioma, si se detecta otro tipo de lengua no indicada en los case
break;
}
?>
Also, if we consider this manual note :
It's good to mention that if user browser will not send
HTTP_ACCEPT_LANGUAGE
, the output from:Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
Will be null.
So remember to set up a fail over scenario!
the code above would use the default page if you can not find information about the language.
Source: Detect Browser Language in PHP