Jump to content

determining language of user


abcabc

Recommended Posts

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;
?>
Link to comment
https://forums.phpfreaks.com/topic/4656-determining-language-of-user/
Share on other sites

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.
[!--quoteo(post=353801:date=Mar 10 2006, 08:37 PM:name=hitman6003)--][div class=\'quotetop\']QUOTE(hitman6003 @ Mar 10 2006, 08:37 PM) [snapback]353801[/snapback][/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.

[!--quoteo(post=353810:date=Mar 10 2006, 09:13 PM:name=abcabc)--][div class=\'quotetop\']QUOTE(abcabc @ Mar 10 2006, 09:13 PM) [snapback]353810[/snapback][/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.
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) [snapback]353849[/snapback][/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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.