Page 1 of 1

$HTTP_ACCEPT_LANGUAGE question

Posted: Mon Dec 11, 2006 7:15 am
by potato
hellow,

to detect the browser language, i know that i can use $HTTP_ACCEPT_LANGUAGE for detecting.

Code: Select all

if(strstr($HTTP_ACCEPT_LANGUAGE,"en")) { }
if(strstr($HTTP_ACCEPT_LANGUAGE,"en-au")) { }
but i dont want to give all the different language possibilities, like en, en-au, ...
but there has to be a solution where i cut everythinf after the -, so that for ex en-au would be en
i think it wouldn't give any problems after.

Anybody an idea?

greetz,
tom

Posted: Mon Dec 11, 2006 7:31 am
by potato
or maybe better, are the iso language codes included in php?
Like the day-names...

Posted: Mon Dec 11, 2006 8:10 am
by aaronhall

Code: Select all

if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) == "en") { }
Here's the W3 spec for accept language: http://www.w3.org/Protocols/rfc2616/rfc ... ml#sec14.4

You can find a list of language codes on google.

Posted: Mon Dec 11, 2006 9:29 am
by neel_basu
aaronhall wrote:

Code: Select all

if(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) == "en") { }
But If The Language Is Like xyz-ab It May Not Work
I Think Code Can Solve It
==================

Code: Select all

<?php
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang_brk = explode("-", $lang);
if($lang_brk[0] == "en")
  {
  //Your Code
  }
?>

Posted: Mon Dec 11, 2006 9:39 am
by neel_basu
And Here Is The Compleate Code
========================

Code: Select all

<?php
//Starting Filter
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if(strstr($lang,"-"))
  {
    $lang_brk = explode("-", $lang);
  }
else
  {
    $lang_brk = $lang;
  }
if(gettype($lang_brk) == "array")
  {
    $lang_get = $lang_brk[0];
  }
else
  {
    $lang_get = $lang_brk;
  }
?>
<?php
//Now $lang_get Contains Yhe Language Name You Can Work With It
if ($lang_get == "en")
  {
    //en Page
  }
if ($lang_get == "bn")
  {
    //bn Page
  }
?>

Posted: Mon Dec 11, 2006 10:22 am
by potato
thankyou everybody.
This is the actual code i got, maybe its usefull for somebody in the future.

Code: Select all

$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
if(strstr($lang,"-"))
  {
    $lang_brk = explode("-", $lang);
  }
else
  {
    $lang_brk = $lang;
  }
if(gettype($lang_brk) == "array")
  {
    $lang_get = $lang_brk[0];
  }
else
  {
    $lang_get = $lang_brk;
  }


$lang_get = substr($lang, 0, 2);

Posted: Mon Dec 11, 2006 10:32 am
by neel_basu
If You Use

Code: Select all

$lang_get = substr($lang, 0, 2);
In That Place It Will Overwrite The Previous $lang_get
And It Would Not Work If The Lang Is Like This xyz-abc

Posted: Mon Dec 11, 2006 11:08 am
by aaronhall
According to RFC 4646, all language subtags are two-letters long. Anyway, there really shouldn't be a need to use substr as long as your exploding with a dash.

Posted: Mon Dec 11, 2006 11:39 am
by neel_basu
aaronhall wrote:According to RFC 4646, all language subtags are two-letters long
Thanks Its A New Information For Me
I Really didn't Know It

Posted: Mon Dec 11, 2006 11:48 am
by aaronhall
Neither did I - all hail Google :)

Posted: Tue Dec 12, 2006 1:08 am
by volka
aaronhall wrote:According to RFC 4646, all language subtags are two-letters long. Anyway, there really shouldn't be a need to use substr as long as your exploding with a dash.
:?:
http://www.w3.org/International/articles/language-tags/Overview.en.php wrote:Most language tags consist of a two- or three-letter language subtag. Sometimes this is followed by a two-letter or three-digit region subtag.
[...]
Examples include:
[...]
mas Masai language
[...]
Now all valid subtags are listed in a single IANA registry, which adopts only one value from the ISO lists per language. If a two-letter ISO code is available, this will be the one in the registry. Otherwise the registry will contain one three-letter code.
strtok can simplify the script to extract the language subtag

Code: Select all

$testdata = array('en', 'en-UK', 'zh-Hans-TW', 'mas');

foreach($testdata as $lang) {
	$primary = strtok($lang, '-');
	echo $lang, ': ', $primary, "<br />\n";
}