Reputation: 1606
I found that there are a different responses for this code for every language, depend on lot of factors.
$lang = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $lang;
for example English language : en_EN,en-EN,en_GB,en_US,en-GB,en-US.
the same for French and other languages.
what i'm looking for is where can i find those lang synonym so i can provide the right page language for every one of them.
for now i have a 3 language, so what i'm looking for is to find all possibilities for English language and French language and Arabic language something like this:
if (($lang=="en_EN") OR ($lang=="en-EN") OR ($lang=="en_GB") OR ($lang=="en_US") OR ($lang=="en-GB") OR ($lang=="en-US")) {
echo 'show English version';
}else if (($lang=="fr_FR") ...... ) { // i don't know the other like fr-FR .....
echo 'show French version';
}else if (($lang=="ar_AR") ...... ) { // i don't know the other like ar-AR .....
echo 'show Arabic version';
}else {
echo 'English as default';
}
Upvotes: 5
Views: 6740
Reputation: 17378
Based on your example, it seems that you're not concerned with the language localisation (if it's British English or American English for example). Rather you're only concerned that the language preference is English.
That being the case, I would suggest taking only the first two characters from the locale.
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
This will return you en
, fr
, ar
, etc, irrespective of the localisation. Use this to set the content language that is returned to the user, and if a language doesn't exist, server a default language, e.g. English.
Upvotes: 8
Reputation: 26
Try this to get an array of languages in ISO 639-1 (https://en.wikipedia.org/wiki/ISO_639-1):
$lang_parse="";
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])) {
$langs = array_combine($lang_parse[1], $lang_parse[4]);
foreach ($langs as $lang => $val) {
if($val === '') {
$rlangs[substr($lang, 0, 2)] = 1;
} else if (isset($rlangs[substr($lang, 0, 2)]) && $rlangs[substr($lang, 0, 2)] < $val) {
$rlangs[substr($lang, 0, 2)] = $val;
}
}
arsort($rlangs, SORT_NUMERIC);
}
In $rlangs var you will get an array with all browser accepted language. If accepted language is english you'll get:
array(1) {
["en"]=>int(1)
}
Upvotes: 0
Reputation: 9396
Try the following:
$lang = $lang[0].$lang[1];
Or
$lang = substr($lang, 0, 2);
Or
$lang = mb_strimwidth($lang, 0, 2);
All of these will return the first 2 chars
of the string, which would be one of en
, fr
, de
etc.
Upvotes: 1
Reputation: 1146
switch (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2))
{
case 'ar': break; # Arabic
case 'fr': break; # France
default: break; # English
}
You are able to improve this realization: $_SERVER['HTTP_ACCEPT_LANGUAGE']
may have ;
separated values, than means that user is able to read some langs per 1 time, so you can check all of them if you want to know what you may offer to him from your list.
Upvotes: 2
Reputation:
If you only care about the language, take the first two characters of the locale code, e.g. en
from en_US
. These characters represent the ISO 639-2 language code for the user's preferred language.
The remaining characters represent the preferred region, e.g. en_US
for American English vs. en_GB
for British English. (en_EN
is meaningless, as there is no country with the country code EN
.) If you don't care about the region, you can ignore this part.
Upvotes: 1