View previous topic :: View next topic |
Author |
Message |
HotelRoom2 -
Joined: 22 Aug 2006 Posts: 21
|
Posted: Mon Aug 28, 2006 11:21 pm Post subject: help with lang detect script |
|
|
I was looking for a way to detect a users os language, and i found this at http://techpatterns.com/forums/about120.html , i modified the look just a bit and i tested it on firefox and it said my lang was en (english) so i tried it in opera with the language as zh (chinese) and it still says i'm using english, but when i go to with chinese lang still on, google displays chinese search results.. anyone know whats wrong?
php 5 (latest), abyss x1 (latest), firefox 1.5.0.6, opera 8.5
Code: |
<?php
/*
// Author(s):: lixlpixel?? http://techpatterns.com/forums/about120.html
// Description: Language detect script.
// Example(s):
header('location: http://22nd.org/'.lixlpixel_detect_lang().'/index.php'); // redirect example
echo '<html><head></head><body>Welcome to 22nd.org.<br>We have detected your preferred language is: <b>'.lixlpixel_detect_lang().'</b> If this is correct, please proceed:</body></html>'; // For Demonstration
*/
// Define default language.
$GLOBALS['_DLANG']='en';
// Define all available languages.
// WARNING: uncomment all available languages
$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);
function lixlpixel_get_env_var($Var) {
if(empty($GLOBALS[$Var])){
$GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
$GLOBALS['_SERVER'][$Var]:
(!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
$GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
}
}
function lixlpixel_detect_lang() {
// Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
lixlpixel_get_env_var('HTTP_USER_AGENT');
$_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
$_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
// Try to detect Primary language if several languages are accepted.
foreach($GLOBALS['_LANG'] as $K) { //strstr()
if(strpos($_AL, $K)===0)
return $K;
}
// Try to detect any language if not yet detected.
foreach($GLOBALS['_LANG'] as $K){
if(strpos($_AL, $K)!==false)
return $K;
}
foreach($GLOBALS['_LANG'] as $K) {
if(preg_match("//[\[\( ]{$K}[;,_\-\)]//",$_UA))
return $K;
}
// Return default language if language is not yet detected.
return $GLOBALS['_DLANG'];
}
?>
<?php
//header('location: http://127.0.0.1/'.lixlpixel_detect_lang().'/index.php');
echo '
<html>
<head></head>
<body>
<br>We have detected your preferred language is: <b>'.lixlpixel_detect_lang().'</b>
If this is correct, please proceed:
</body>
</html>
';
?> |
|
|
Back to top |
|
|
pkSML -
Joined: 29 May 2006 Posts: 952 Location: Michigan, USA
|
Posted: Tue Aug 29, 2006 1:00 am Post subject: |
|
|
This script uses HTTP headers that your browser sends it - specifically, the HTTP_ACCEPT_LANGUAGE variable.
I'm guessing that Opera might be sending two accept languages.
In Opera, go here --> http://stephen.calvarybucyrus.org/redirect.php?page=headers.
Post the results found on that page. This will help in solving your problem. _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
|
HotelRoom2 -
Joined: 22 Aug 2006 Posts: 21
|
Posted: Tue Aug 29, 2006 1:16 am Post subject: |
|
|
Code: |
HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 8.50
HTTP_HOST: stephen.calvarybucyrus.org
HTTP_ACCEPT: application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1
HTTP_ACCEPT_LANGUAGE: it,ce;q=0.9,en;q=0.8
HTTP_ACCEPT_CHARSET: windows-1252, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1
HTTP_ACCEPT_ENCODING: deflate, gzip, x-gzip, identity, *;q=0
HTTP_CACHE_CONTROL: no-cache
HTTP_CONNECTION: Keep-Alive, TE
HTTP_TE: deflate, gzip, chunked, identity, trailers |
heres what i got.. it looks like it sent 3 languages, i removed them from prefered langauges in opera and i still got english from my script, but not in the link you posted |
|
Back to top |
|
|
pkSML -
Joined: 29 May 2006 Posts: 952 Location: Michigan, USA
|
Posted: Tue Aug 29, 2006 1:22 am Post subject: |
|
|
Yup, you're right. It sent three languages. It looks like Italian was the default language, then Chinese, then English.
The array GLOBALS['_LANG'] does not have Chinese in it.
Simply add the line into your array.
Then Chinese should be listed as your primary language.
Did this work?
P.S. If Italian comes up as your primary language, it is because it sent that language first in the header. _________________ Stephen
Need a LitlURL?
http://CodeBin.yi.org |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|