Jump to content


Photo

determining language of user


  • Please log in to reply
5 replies to this topic

#1 abcabc

abcabc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 March 2006 - 12:55 AM

I would like to know if this code works. I am trying to determine the language the user is using, but I don't know of a way to check this. For example, if a user is using French, will the variable $lang receive the value of 'fr'? Thanks in advance.

<?php

// session_start();
error_reporting(E_ALL);// can be removed later
if (isset($_GET['lang'])) {
$lang = $_GET['lang'];
$_SESSION['Language'] = $lang;
} else {
if (isset($_SESSION['Language'])) {
$lang = $_SESSION['Language'];
}
}
if (!isset($lang)) {
switch (substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2)) {
case 'fr': $lang = 'fr'; break;
case 'de': $lang = 'de'; break;
case 'ko': $lang = 'ko'; break;
case 'es': $lang = 'es'; break;
case 'it': $lang = 'it'; break;
case 'ja': $lang = 'ja'; break;
case 'pt': $lang = 'pt'; break;
case 'zh-CN': $lang = 'zh-CN'; break;
default: $lang = 'en';
}
}
echo $lang;
?>

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 11 March 2006 - 01:37 AM

According to the manual, $_SERVER['HTTP_ACCEPT_LANGUAGE'] is not one of the predefined variables available.

Refer to [a href=\"http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.server\" target=\"_blank\"]http://us2.php.net/manual/en/reserved.vari...ariables.server[/a] to see which variables are available by default.

#3 abcabc

abcabc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 March 2006 - 02:13 AM

[!--quoteo(post=353801:date=Mar 10 2006, 08:37 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 10 2006, 08:37 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
According to the manual, $_SERVER['HTTP_ACCEPT_LANGUAGE'] is not one of the predefined variables available.

Refer to [a href=\"http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.server\" target=\"_blank\"]http://us2.php.net/manual/en/reserved.vari...ariables.server[/a] to see which variables are available by default.
[/quote]

I did refer to that manual beforehand, and used it to put together this code. Also, look at
[a href=\"http://www.developershome.com/wap/detection/detection.asp?page=readHeader\" target=\"_blank\"]http://www.developershome.com/wap/detectio...page=readHeader[/a]
and scroll down to header "Retrieving HTTP Headers with PHP" and you will see it listed as OK.



#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 11 March 2006 - 02:35 AM

[!--quoteo(post=353810:date=Mar 10 2006, 09:13 PM:name=abcabc)--][div class=\'quotetop\']QUOTE(abcabc @ Mar 10 2006, 09:13 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I did refer to that manual beforehand, and used it to put together this code. Also, look at
[a href=\"http://www.developershome.com/wap/detection/detection.asp?page=readHeader\" target=\"_blank\"]http://www.developershome.com/wap/detectio...page=readHeader[/a]
and scroll down to header "Retrieving HTTP Headers with PHP" and you will see it listed as OK.
[/quote]

Fair enough...I never actually looked at all the elements in the server array before. On my home server, the value of that particular element is:

[HTTP_ACCEPT_LANGUAGE] => en-us,en;q=0.5

So, I don't see any reason, off hand, why your code wouldn't work.

#5 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 11 March 2006 - 05:20 AM

Well, if you comment out session_start() then there is no session to use $_SESSION.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#6 abcabc

abcabc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 11 March 2006 - 07:09 PM

Let me ask the question differently:

Is there someone who has English as a secondary language installed on their machine, and with one of the other listed languages as their primary.

If yes, can you copy and paste the code into a .php file, run it, and let me know if your default language is indicated or is "en" indicated.

thanks.


[!--quoteo(post=353849:date=Mar 11 2006, 12:20 AM:name=txmedic03)--][div class=\'quotetop\']QUOTE(txmedic03 @ Mar 11 2006, 12:20 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, if you comment out session_start() then there is no session to use $_SESSION.
[/quote]

Yes, you are correct, session-start should be uncommented. thanks.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users