[IMC-US-tech] Language autodetection patch for dada
Brian Szymanski
bks10 at cornell.edu
Thu Jan 22 20:08:18 CST 2004
Howdy y'all.
The attached patch to header.inc (against 0.98.2) does language
autodetection with dada.
Notes:
1 - It could use some cleanup - I'm not a php guru and my principle was
"make it work, make it pretty later"... and it's not later yet :-) - (does
making a variable name start with g_ automatically make it global in php?
If so, g_llang and g_llangq but not g_lang should be renamed sans g_)
2 - I haven't verified if the code does all the things dada wants it to
do. For example, I'm not sure if it saves the results of its autodetection
in to a cookie properly (but I suspect it does since it sets g_lang with
the result of the autodetection every time there is no cookie to check for
a language preference). Also, it might not mangle browser passed
language/locale pairs such as "en-us" into dadaified strings such as
"en_US" (unless dada already has code to make this conversion).
3 - Since I lack a browser with a customizable accept-language string,
it's somewhat untested, but according to some debug prints seems to work
with Mozilla/1.5/en-us,en;q=0.7,es;q=0.3 and IE/6.0/en-us.
Peace,
Brian Szymanski
ski at indymedia.org
bks10 at cornell.edu
-------------- next part --------------
--- docs/upgrade_98_2/header.inc Mon Jan 12 00:01:57 2004
+++ header.inc Thu Jan 22 20:51:35 2004
@@ -170,55 +170,116 @@
// Language localization
///////////////////////////////////////////////////////////////
+// first see if there is a http client accept string
+if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
+ // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
+ $g_lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
+ //split by semicolons
+ $g_llang = explode(",", $g_lang);
+ //if any elements in g_llang have *, just ignoring them is safe
+ for($i=0; $i<count($g_llang); $i++) {
+ if(substr_count($g_llang[$i], "*")>0) {
+ //remove this element
+ for($j=$i; $j<count($g_llang)-1; $j++) {
+ $g_llang[$j] = $g_llang[$j+1];
+ }
+ }
+ }
+ $log = "\n";
+ //TODO: sort g_llang and g_llangq into descending order
+ for($i=0; $i<count($g_llang); $i++) {
+ $log = $log.$g_llang[$i]."<br>";
+ $t=explode(";", $g_llang[$i]);
+ if(count($t)==1) {
+ //default q==1
+ $g_llangq[$i] = 1;
+ } else if(count($t)==2) {
+ //split by =, just use what's on the right
+ $tt = explode("=", $t[1]);
+ $g_llangq[$i] = $tt[1];
+ //remove the post-semicolon stuff from g_llang
+ $g_llang[$i] = $t[0];
+ } else {
+ error();
+ }
+ }
+}
+// slide all in g_llang , g_llangq over one
+for($i=count($g_llang); $i>=0; $i--) {
+ $g_llang[$i] = $g_llang[$i-1];
+ $g_llangq[$i] = $g_llangq[$i-1];
+}
+$g_llang[0] = "";
+$g_llangq[0] = "1";
+// cookie variable trumps http client accept string
if (isset($_SESSION['lang'])) {
- $g_lang = $_SESSION['lang'];
+ $g_llang[0] = $_SESSION['lang'];
}
// _POST trumps COOKIE_VARS
if (isset($_POST['lang'])) {
- $g_lang = $_POST['lang'];
+ $g_llang[0] = $_POST['lang'];
}
// if it's still not set, try User obj
-if (!isset($g_lang)) {
+if ($g_llang[0]=="") {
if (isAuthorized('User')) {
$user = $authorized_object->authobj;
if (!empty($user->language)) {
- $g_lang = $user->language;
- } else {
- $g_lang = $g_default_lang;
+ $g_llang[0] = $user->language;
+// don't do this anymore
+// } else {
+// $g_llang[0] = $g_default_lang;
}
- } else {
- $g_lang = $g_default_lang;
+// don't do this anymore
+// } else {
+// $g_llang[0] = $g_default_lang;
}
}
-
-// Set language to desired language
-$desired_lang = $g_languages[$g_lang];
-// try the default locale directory for this language
-$checklang = setlocale(LC_ALL, $desired_lang['directory']);
-if ($checklang === FALSE) {
- dt_log('Failure setting language to '.$g_lang.' using desired directory '.$desired_lang['directory'],3);
- $alt_directories = explode(',',$desired_lang['alt']);
- // if PHP > 4.3.0, try an array of directory names
- if (version_compare(phpversion(),'4.3.0','ge')) {
- $checklang = setlocale(LC_ALL,$alt_directories);
- if ($checklang === FALSE) {
- dt_log("Failed to set locale using array of ".$desired_lang['alt'],1);
- } else {
- dt_log("New locale set to $checklang",1);
- }
- } else {
- // otherwise loop through them one at a time
- foreach($alt_directories as $altdir) {
- if ($checklang = setlocale(LC_ALL,$altdir) !== FALSE) {
- dt_log("New locale set to $checklang",1);
- break;
+//use default lang as last preference
+$g_llang[count($g_llang)] = $g_default_lang;
+$g_llangq[count($g_llang)] = 0.000001;
+
+for($i=0; $i < count($g_llang); $i++) {
+ $g_lang = $g_llang[$i];
+ //unknown language
+ if($g_lang == "") {
+ continue;
+ }
+ //user explicitly doesn't want this language
+ if($g_llangq[$i]<=0) {
+ continue;
+ }
+// $log = $log."trying: ".$g_llang[$i]."<br>";
+ // Set language to desired language
+ $desired_lang = $g_languages[$g_lang];
+ // try the default locale directory for this language
+ $checklang = setlocale(LC_ALL, $desired_lang['directory']);
+ if ($checklang === FALSE) {
+// $log = $log.'Failure setting language to '.$g_lang.' using desired directory '.$desired_lang['directory']."<br>";
+ dt_log('Failure setting language to '.$g_lang.' using desired directory '.$desired_lang['directory'],3);
+ $alt_directories = explode(',',$desired_lang['alt']);
+ // if PHP > 4.3.0, try an array of directory names
+ if (version_compare(phpversion(),'4.3.0','ge')) {
+ $checklang = setlocale(LC_ALL,$alt_directories);
+ if ($checklang === FALSE) {
+ dt_log("Failed to set locale using array of ".$desired_lang['alt'],1);
} else {
- dt_log('Failure setting language to '.$g_lang.' using alternate '.$altdir,3);
+ dt_log("New locale set to $checklang",1);
+ }
+ } else {
+ // otherwise loop through them one at a time
+ foreach($alt_directories as $altdir) {
+ if ($checklang = setlocale(LC_ALL,$altdir) !== FALSE) {
+ dt_log("New locale set to $checklang",1);
+ break;
+ } else {
+ dt_log('Failure setting language to '.$g_lang.' using alternate '.$altdir,3);
+ }
}
}
+ } else {
+ dt_log("New locale set to $checklang",1);
+ break;
}
-} else {
- dt_log("New locale set to $checklang",1);
}
if ($checklang) {
putenv("LANG=$checklang");
@@ -364,4 +425,4 @@
if (isAuthorized('Any')) {
$use_cache = false;
}
-?>
\ No newline at end of file
+?>
More information about the IMC-US-tech
mailing list