$HTTP_ACCEPT_LANGUAGE question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

$HTTP_ACCEPT_LANGUAGE question

Post 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
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post by potato »

or maybe better, are the iso language codes included in php?
Like the day-names...
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
  }
?>
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
  }
?>
User avatar
potato
Forum Contributor
Posts: 192
Joined: Tue Mar 16, 2004 8:30 am
Location: my lovely trailer, next to the big tree

Post 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);
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post 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
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Neither did I - all hail Google :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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";
}
Post Reply