I'm trying to build multilangual site.
I use this piece of code to detect users language. If you havent chosen a language, it will include your language file based on HTTP_ACCEPT_LANGUAGE.
I don't know where it gets it from though:
session_start();
if (!isset($_SESSION['lang'])) {
$_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'en') $_SESSION['lang'] = "en";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'sv') $_SESSION['lang'] = "sv";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'pl') $_SESSION['lang'] = "pl";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'fr') $_SESSION['lang'] = "fr";
include('languages/'.$_SESSION['lang'].'.php');
It works for me and includes the polish lang file. But is this code accurate? Or is there another way?
The browser generally sends a HTTP header, name Accept-Language, that indicates which languages the user is willing to get.
For instance, this header can be :
Accept-Language: en-us,en;q=0.5
There is notion of priority in it, btw ;-)
In PHP, you can get this in the $_SERVER super global :
var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);
will get me :
string 'en-us,en;q=0.5' (length=14)
Now, you have to parse that ;-)
If I edit my preferences in the browser's option to say "I want french, and if you can't serve me french, get me english from the US ; and if you can't get me that either, just get me english), the header will be :
Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3
And, from PHP :
string 'fr-fr,en-us;q=0.7,en;q=0.3' (length=26)
For more informations, you can take a look at [section 14.4 of the HTTP RFC][1].
And you probably can find lots of code example in PHP to parse that header ; for instance : Parse Accept-Language to detect a user's language
Have fun !
Here's the script I used for a bi-lingual site. It is to be used as index.php of mysite.com. Based on the user's browser's language preference, it would redirect to desired language version of the site or the default language site if the site in user's preferred langauge was not available.
<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
"en" => "http://en.mysite.com/",
"bn" => "http://bn.mysite.com/",
);
// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
$lang = 'en';
// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>
I know there already many good solutions, but have found my own way to solve this problem.
<?php
$prefLocales = array_reduce(
explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']),
function ($res, $el) {
list($l, $q) = array_merge(explode(';q=', $el), [1]);
$res[$l] = (float) $q;
return $res;
}, []);
arsort($prefLocales);
/*
This get you from headers like this
string 'en-US,en;q=0.8,uk;q=0.6,ru;q=0.4' (length=32)
array like this
array (size=4)
'en-US' => float 1
'en' => float 0.8
'uk' => float 0.6
'ru' => float 0.4
*/
Code will convert HTTP_ACCEPT_LANGUAGE string to array with locales as keys and weight as values, sorted from high value to low. So you can just get one by one with array_shift to get the best match with your site locales.
You can use: Locale::acceptFromHttp().
Tries to find locale that can satisfy the language list that is requested by the HTTP "Accept-Language" header.
Your code looks just fine. You might want to add a final else default choice if the visitor asks for a language you aren't providing.
Also, if the visitor himself selects a language you should save that choice in a persistent cookie and check its value, giving it precedence over HTTP_ACCEPT_LANGUAGE.
As far as I can tell Youtube does use HTTP_ACCEPT_LANGUAGE, but at the same time uses IP geolocation to suggest a change in language if the langauge of the visitor's country doesn't match that. Definitely annoying.
Just nitpicking: if you're gonna add languages to the list a switch() statement might be more readable.
Here's a function for selecting the best out of a group of supported languages. It extracts languages from Accept-Language, then sorts the given array of languages according to their priority.
function select_best_language($languages) {
if (!$_SERVER['HTTP_ACCEPT_LANGUAGE']) return $languages[0];
$default_q=100;
foreach (explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lqpair) {
$lq=explode(";q=",$lqpair);
if ($lq[1]) $lq[1]=floatval($lq[1]); else $lq[1]=$default_q--;
$larr[$lq[0]]=$lq[1];
}
usort($languages,function($a,$b) use ($larr) { return $larr[$b]<=>$larr[$a]; });
return $languages[0];
}
$lang = select_best_language(['en','fr','it']);
Try This
function getUserLanguage() {
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all(
'/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'],
$lang_parse
);
if (count($lang_parse[1])) {
// create a list like 'en' => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') {
$langs[$lang] = 1;
}
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
//extract most important (first)
reset($langs);
$lang = key($langs);
//if complex language simplify it
if (stristr($lang, '-')) {
list($lang) = explode('-', $lang);
}
return $lang;
}
This is also possible. It will use english as default if .php is not available.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
(#include_once 'languages/'.$lang.'.php') or (#include_once 'languages/en.php');
I solved this issue for PHP7.4+ with the code below. It strips out the first two letters of the locale code, takes into account cases like 'en-US' and produces a map like:
$map = [
'en' => 1,
'de' => 0.8,
'uk' => 0.3
];
The code is as follows:
$header = 'en-US,de-DE;q=0.8,uk;q=0.3';
$pattern = '((?P<code>[a-z-_A-Z]{2,5})([;q=]+?(?P<prio>0.\d+))?)';
preg_match_all($pattern, $header, $matches);
['code' => $codes, 'prio' => $values] = $matches;
$map = \array_combine(
\array_map(fn(string $language) => strtolower(substr($language, 0, 2)), \array_values($codes)),
\array_map(fn(string $value) => empty($value) ? 1 : (float)$value, \array_values($values))
);
Please note that the regex is probably suboptimal, improvement suggestions are welcome.
Named capture groups are always in the order of matching, so we can use array_combine safely.
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$parts=explode(';',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
$langs=explode(',',$parts[0]);
var_dump($langs);
}
Related
I need to display site visitor's continent. I am using geoip functionality to pull 2 letter code, which I can get to work very easily. However, just displaying 'AS' for Asia to the site visitor is clearly undesirable. So I'm trying to convert that. This is what I am trying to do. . .
<?php
$continent = geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
$africa = array('AF');
$antarctica = array('AN');
$asia = array('AS');
$europe = array('EU');
$northamerica = array('NA');
$oceania = array('OC');
$southamerica = array('SA');
if ($africa) {
echo 'Africa';
}
elseif ($antarctica) {
echo 'Antarctica';
}
elseif ($asia) {
echo 'Asia';
}
elseif ($europe) {
echo 'Europe';
}
elseif ($northamerica) {
echo 'North America';
}
elseif ($oceania) {
echo 'Australia and Oceania';
}
elseif ($southamerica) {
echo 'South America';
}
else {
echo '';
}
?>
It just displays 'Africa' no matter where the site visitor, so it's getting stuck there. I'm reading though PHP tutorials but can't figure out what I'm doing wrong. My if, else and elsifs look good as far as I can tell.
Well, you're kind of going about it wrong.
You're getting the code correctly with your first line, but then you are creating a bunch of different arrays, each containing one of the possible codes. Then you are checking for the existence of each array, and since you just created them, they do exist, so your IF block will stop with the first check and output Africa.
What you want to do is create one array that contains all the codes as keys, with the output name as values, and then just use the code you got from $_SERVER to return the matching value from that array. (I got the list of codes from the manual)
$continent = geoip_continent_code_by_name($_SERVER['REMOTE_ADDR']);
$continents = [
'AF' => 'Africa',
'AN' => 'Antarctica',
'AS' => 'Asia',
'EU' => 'Europe',
'NA' => 'North America',
'OC' => 'Oceania',
'SA' => 'South America',
];
echo $continents[$continent];
It's worth a little explanation for WHY your IF block functions the way it does. PHP is loosely typed, meaning that you don't have to explicitly set the type of variables. So when PHP encounters a variable it has to guess about how to use it. So when you say if($africa), php tries to make sense of $africa in the context of a boolean question, and since it DOES exist and is not 0 or false, it sees it as TRUE and executes the first block.
So i'm having a problem with google authenticator and my PHP.
So i'm using this library to generate QR codes: https://github.com/PHPGangsta/GoogleAuthenticator
So when I use my username to generate a code it works fine. I get something along the lines of:
otpauth://totp/username?secret=aCodeInBase32&issuer=Mysite
For my case it is:
otpauth://totp/NoahNok?secret=aCodeInBase32&issuer=Mysite
however when doing this for any other use I get an invalid token error on the Google Authenticator app. It doesnt matter what else I put I always seem to get this error, yet it works fine for my account.
E.g this one doesn't work: otpauth://totp/Test?secret=KRSX&issuer=MySite
Is there something obvious im doing wrong?
Code im using:
Some queries before to get data
$g = new PHPGangsta_GoogleAuthenticator();
include("Base32.php");
$secret = substr(Base32::encode($username),0,-4);
echo $g->getQRCodeGoogleUrl($username, $secret, "MySite");
Generates QR URL
public function getQRCodeGoogleUrl($name, $secret, $title = null, $params = array())
{
$width = !empty($params['width']) && (int) $params['width'] > 0 ? (int) $params['width'] : 200;
$height = !empty($params['height']) && (int) $params['height'] > 0 ? (int) $params['height'] : 200;
$level = !empty($params['level']) && array_search($params['level'], array('L', 'M', 'Q', 'H')) !== false ? $params['level'] : 'M';
$urlencoded = urlencode('otpauth://totp/'.$name.'?secret='.$secret.'');
if (isset($title)) {
$urlencoded .= urlencode('&issuer='.urlencode($title));
}
return 'https://chart.googleapis.com/chart?chs='.$width.'x'.$height.'&chld='.$level.'|0&cht=qr&chl='.$urlencoded.'';
}
Base32 is padded out to the nearest multiple of 8 characters, so it won't always have ==== at the end to strip off. From your examples we get:
NoahNok => JZXWC2CON5VQ====
and:
Test => KRSXG5A=
So if you remove the last 4 characters always you'll create an invalid Base32 sequence for situations like the latter. You could instead use rtrim like so:
$secret = rtrim(Base32::encode($username), '=')
to just remove all trailing equals (or just leave them in).
Edit
I was just thinking about this and while the above will fix the proximate issue, generating the secret this way is probably not a good idea. If you think about it, setting the secret equal to the username means that if someone finds the username they can generate a valid OTP and therefore be able to pass their 2FA.
Secret should be unique and generally unguessable for this purpose, and the library you are using has a createSecret method to do this for you.
I wanted to know, is there any way from PHP/javascript to get current client OS language. I tried to use $_SERVER["HTTP_ACCEPT_LANGUAGE"] but sometimes it get the wrong language.
For example in Google Chrome:
My OS: Windows 7
Language: English
Using $_SERVER["HTTP_ACCEPT_LANGUAGE"] I got this result:
HTTP_ACCEPT_LANGUAGE: zh,en-US;q=0.8,en;q=0.6
It said "zh" is my primary language.
Is there any other way to get client OS language? Because that's what I wanted, not the browser language setting. Thanks
try this function
function getUserLanguage() {
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like â??enâ?? => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === '') $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
//extract most important (first)
foreach ($langs as $lang => $val) { break; }
//if complex language simplify it
if (stristr($lang,"-")) {$tmp = explode("-",$lang); $lang = $tmp[0]; }
return $lang;
}
Send it via javascript on IE?
navigator.browserLanguage: browser language
navigator.systemLanguage: Windows system language
navigator.userLanguage: Windows user-specific language
Thanks to: Is there anyway to detect OS language using javascript?
That's the only way besides the one you've mentioned to get the language of the client OS, PHP is run by the server and nothing else.
Build a PHP sorting function.
HTTP_ACCEPT_LANGUAGE: zh,en-US;q=0.8,en;q=0.6
zh and en-US share the same q= value, meaning that you can sort on the highest language value and default to en-US if the quality is the same on two languages.
Just noticed that #Quentin mentioned this in the comment section a minute before my edit, well done sir!
Mockup:
$languages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$default = 'en-US';
/*
* magic split and structure the language into a array sorted by quality
*
* $languages_sorted_by_quality = array(0.8 => ('zh', 'en-US'));
*/
$top_languages = max($languages_sorted_by_quality);
if (isset($top_languages[$default])) {
$language = $default;
else
$language = $top_languages[0];
have your tried http_negotiate_language
I have an object which has a state property, for example state = 'state4' or state = 'state2'.
Now I also have an array of all available states that the state property can get, state1 to state8 (note: the states are not named stateN. They have eight different names, like payment or canceled. I just put stateN to describe the problem).
In addition to that, I have a logical expression like $expression = !state1||state4&&(!state2||state5) for example. This is the code for the above description:
$state = 'state4';
$expression = '!state1||state4&&(!state2||state5)';
Now I want to check if the logical expression is true or false. In the above case, it's true. In the following case it would be false:
$state = 'state1';
$expression = state4&&!state2||(!state1||state7);
How could this be solved in an elegant way?
//Initialize
$state = 'state4';
$expression = '!state1||state4&&(!state2||state5)';
//Adapt to your needs
$pattern='/state\d/';
//Replace
$e=str_replace($state,'true',$expression);
while (preg_match_all($pattern,$e,$matches)
$e=str_replace($matches[0],'false',$e);
//Eval
eval("\$result=$e;");
echo $result;
Edit:
Your update to the OQ necessitates some minor work:
//Initialize
$state = 'payed';
$expression = '!payed||cancelled&&(!whatever||shipped)';
//Adapt to your needs
$possiblestates=array(
'payed',
'cancelled',
'shipped',
'whatever'
);
//Replace
$e=str_replace($state,'true',$expression);
$e=str_replace($possiblestates,'false',$e);
//Eval
eval("\$result=$e;");
echo $result;
Edit 2
There has been concern about eval and PHP injection in the comments: The expression and the replacements are completly controlled by the application, no user input involved. As long as this holds, eval is safe.
I am using ExpressionLanguage, but there are few different solutions
ExpressionLanguage Symfony Component - https://symfony.com/doc/current/components/expression_language.html
cons - weird array syntax - array['key']. array.key works only for objects
cons - generate notice for array['key'] when key is not defined
pros - stable and well maintainer
https://github.com/mossadal/math-parser
https://github.com/optimistex/math-expression
Please remember that eval is NOT an option, under NO circumstances. We don't live an a static world. Any software always grows and evolves. What was once considered a safe input an one point may turn completely unsafe and uncontrolled.
I think you have a case which can be solved if you model each of your expressions as a rooted directed acyclic graph (DAG).
I assumed acyclic since your ultimate aim is to find the result of boolean algebra operations (if cycling occur in any of graph, then it'd be nonsense I think).
However, even if your graph structure—meaningfully—can be cyclic then your target search term will be cyclic graph, and it should still have a solution.
$expression = '!state1||state4&&(!state2||state5)';
And you have one root with two sub_DAGs in your example.
EXPRESSION as a Rooted DAG:
EXPRESSION
|
AND
___/ \___
OR OR
/ \ / \
! S_1 S_4 ! S_2 S5
Your adjacency list is:
expression_adj_list = [
expression => [ subExp_1, subExp_2 ] ,
subExp_1 => [ ! S_1, S_4 ],
subExp_2 => [ ! S_2, S5 ]
]
Now you can walk through this graph by BFS (breadth-first search algorithm) or DFS (depth-first search algorithm) or your custom, adjusted algorithm.
Of course you can just visit the adjacency list with keys and values as many times as you need if this suits and is easier for you.
You'll need a lookup table to teach your algorithm that. For example,
S2 && S5 = 1,
S1 or S4 = 0,
S3 && S7 = -1 (to throw an exception maybe)
After all, the algorithm below can solve your expression's result.
$adj_list = convert_expression_to_adj_list();
// can also be assigned by a function.
// root is the only node which has no incoming-edge in $adj_list.
$root = 'EXPRESSION';
q[] = $root; //queue to have expression & subexpressions
$results = [];
while ( ! empty(q)) {
$current = array_shift($q);
if ( ! in_array($current, $results)) {
if (isset($adj_list[$current])) { // if has children (sub/expression)
$children = $adj_list[$current];
// true if all children are states. false if any child is subexpression.
$bool = is_calculateable($children);
if ($bool) {
$results[$current] = calc($children);
}
else {
array_unshift($q, $current);
}
foreach ($children as $child) {
if (is_subexpresssion($child) && ! in_array($child, $results)) {
array_unshift($q, $child);
}
}
}
}
}
return $results[$root];
This approach has a great advantage also: if you save the results of the expressions in your database, if an expression is a child of the root expression then you won't need to recalculate it, just use the result from the database for the child subexpressions. In this way, you always have a two-level depth DAG (root and its children).
I've my site set up so I just need to add "?lang=en" or "?lang=es" to change languages English / Spanish.
When I enter the site with, for ex, "http://domain.com/something/something_else?lang=es", a cookie is set so I continue to navigate the site in that language.
I would like to redirect my users first by the "Accept-Language" value of their browser, but then allow them to continue to navigate the site in other language if they want to.
What would be the best way to do it? Would .htaccess work along with the cookie that's set when the language is chosen?
EDIT: Here's my updated code with Paul answer:
EDIT2: Oh, I just have "en" and "es" languages. I'm not sure on how this code wpuld choose only between this two or set the default... :/
if (isset($_GET["lang"]))
$this->setLanguage($_GET["lang"]);
elseif (isset($_COOKIE["language"]))
$this->setLanguage($_COOKIE["language"]);
elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
// Parse the Accept-Language according to:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
preg_match_all(
'/([a-z]{1,8})' . // First part of language e.g en
'(-[a-z]{1,8})*\s*' . // other parts of language e.g -us
// Optional quality factor
'(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'],
$langParse);
$langs = $langParse[1];
$quals = $langParse[4];
$numLanguages = count($langs);
$langArr = array();
for ($num = 0; $num < $numLanguages; $num++)
{
$newLang = strtoupper($langs[$num]);
$newQual = isset($quals[$num]) ?
(empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0;
// Choose whether to upgrade or set the quality factor for the
// primary language.
$langArr[$newLang] = (isset($langArr[$newLang])) ?
max($langArr[$newLang], $newQual) : $newQual;
}
// sort list based on value
arsort($langArr, SORT_NUMERIC);
$acceptedLanguages = array_keys($langArr);
$preferredLanguage = reset($acceptedLanguages);
$this->setLanguage($preferredLanguage);
}
else
$this->setLanguage("en");
I do this in PHP. Accept-Language is a complex thing. A browser can suggest more than one language that it would like to accept (each with a quality factor that shows which it prefers). For my site I have a default language to display (which is shown when none of the Accept-Languages is in my translation list). Otherwise if there is no language set (setLang) I choose it based on the most acceptable for the browser by parsing the Accept-Language. The function I use is below (it contains my session manager for setting cookies - but you could reimplement that with direct calls to $_SESSION[etc] = $foo;).
Edit: Unfortunately my website only has translations for the primary languages (EN, ES, FR) rather than (en_US, en_GB, es_MX, es_ES) so I choose the highest quality factor specified in these for the primary language.
public function setLanguage($setLang='')
{
if (!empty($setLang))
{
$this->setup['Session']->set($this->setup['Lang_Key'], $setLang);
}
else
{
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
// Parse the Accept-Language according to:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
preg_match_all(
'/([a-z]{1,8})' . // First part of language e.g en
'(-[a-z]{1,8})*\s*' . // other parts of language e.g -us
// Optional quality factor
'(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'],
$langParse);
$langs = $langParse[1];
$quals = $langParse[4];
$numLanguages = count($langs);
$langArr = array();
for ($num = 0; $num < $numLanguages; $num++)
{
$newLang = strtoupper($langs[$num]);
$newQual = isset($quals[$num]) ?
(empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0;
// Choose whether to upgrade or set the quality factor for the
// primary language.
$langArr[$newLang] = (isset($langArr[$newLang])) ?
max($langArr[$newLang], $newQual) : $newQual;
}
// sort list based on value
arsort($langArr, SORT_NUMERIC);
$acceptedLanguages = array_keys($langArr);
$preferredLanguage = reset($acceptedLanguages);
$this->setup['Session']->set(
$this->setup['Lang_Key'], $preferredLanguage);
}
else
{
$this->setup['Session']->set(
$this->setup['Lang_Key'], $this->setup['Default_Language']);
}
}
return $this->setup['Session']->get($this->setup['Lang_Key']);
}
I do this in PHP. Accept-Language is a complex thing. A browser can suggest more than one language that it would like to accept (each with a quality factor that shows which it prefers).
Unfortunately my website only has translations for the primary languages (EN, ES, FR) rather than (en_US, en_GB, es_MX, es_ES) so I choose the highest quality factor specified in these for the primary language.
Below is an untested edit which should remove most or all of the dependencies from my code. Sorry, things were confusing with my previous answer. I had a few calls to my function with some checking of languages are done elsewhere. The code below should set the session language variable, which you should use elsewhere for determining the correct translation.
It seems a lot less complicated than my previous answer and I will have to implement this in my own code before long. For people who need specific translations (EN_US, EN_GB) then the below code should be modified to take account of Match 2 in the preg_match_all.
$websiteLanguages = array('EN', 'ES');
session_start();
// The user wants a specific language regardless of their accept settings.
if (isset($_GET["lang"]))
{
$_SESSION["language"] = $_GET["lang"];
return;
}
// A language has already been decided upon.
if (isset($_SESSION["language"]))
{
return;
}
// No language has been chosen we should choose from the accept language.
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
// Parse the Accept-Language according to:
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
preg_match_all(
'/([a-z]{1,8})' . // M1 - First part of language e.g en
'(-[a-z]{1,8})*\s*' . // M2 -other parts of language e.g -us
// Optional quality factor M3 ;q=, M4 - Quality Factor
'(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i',
$_SERVER['HTTP_ACCEPT_LANGUAGE'],
$langParse);
$langs = $langParse[1]; // M1 - First part of language
$quals = $langParse[4]; // M4 - Quality Factor
$numLanguages = count($langs);
$langArr = array();
for ($num = 0; $num < $numLanguages; $num++)
{
$newLang = strtoupper($langs[$num]);
$newQual = isset($quals[$num]) ?
(empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0;
// Choose whether to upgrade or set the quality factor for the
// primary language.
$langArr[$newLang] = (isset($langArr[$newLang])) ?
max($langArr[$newLang], $newQual) : $newQual;
}
// sort list based on value
// langArr will now be an array like: array('EN' => 1, 'ES' => 0.5)
arsort($langArr, SORT_NUMERIC);
// The languages the client accepts in order of preference.
$acceptedLanguages = array_keys($langArr);
// Set the most preferred language that we have a translation for.
foreach ($acceptedLanguages as $preferredLanguage)
{
if (in_array($preferredLanguage, $websiteLanguages))
{
$_SESSION['Language'] = $preferredLanguage;
return;
}
}
}
// We will have returned by now if a language could be chosen, otherwise use
// our default language.
$_SESSION['Language'] = "EN";