abcabc Posted March 11, 2006 Share Posted March 11, 2006 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 laterif (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;?> Quote Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/ Share on other sites More sharing options...
hitman6003 Posted March 11, 2006 Share Posted March 11, 2006 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 Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/#findComment-16300 Share on other sites More sharing options...
abcabc Posted March 11, 2006 Author Share Posted March 11, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/#findComment-16309 Share on other sites More sharing options...
hitman6003 Posted March 11, 2006 Share Posted March 11, 2006 [!--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.5So, I don't see any reason, off hand, why your code wouldn't work. Quote Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/#findComment-16314 Share on other sites More sharing options...
txmedic03 Posted March 11, 2006 Share Posted March 11, 2006 Well, if you comment out session_start() then there is no session to use $_SESSION. Quote Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/#findComment-16348 Share on other sites More sharing options...
abcabc Posted March 11, 2006 Author Share Posted March 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4656-determining-language-of-user/#findComment-16468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.