cvs: phpweb /include langchooser.inc
| From: | Gabor Hojtsy | Date: | Sat, 08 Mar 2003 20:43:16 +0000 |
| Subject: | cvs: phpweb /include langchooser.inc | ||
| Groups: | php.mirrors | ||
| Request: | Send a blank email to php-mirrors+get-16212@lists.php.net to get a copy of this message | ||
goba Sat Mar 8 15:43:16 2003 EDT
Modified files:
/phpweb/include langchooser.inc
Log:
Update code to handle qualities (really only used by Safari as far as I know).
Refactor the code to be under a function, so temp variables are lost, and there
is no global namespace solution. Also remove ?> from the end, so there won't be
any whitespace outoput in any case...
Index: phpweb/include/langchooser.inc diff -u phpweb/include/langchooser.inc:1.11 phpweb/include/langchooser.inc:1.12 --- phpweb/include/langchooser.inc:1.11 Sun Mar 2 14:33:43 2003 +++ phpweb/include/langchooser.inc Sat Mar 8 15:43:16 2003 @@ -1,6 +1,6 @@ <?php // -*- C++ -*- -// $Id: langchooser.inc,v 1.11 2003/03/02 19:33:43 goba Exp $ +// $Id: langchooser.inc,v 1.12 2003/03/08 20:43:16 goba Exp $ /* @@ -24,96 +24,124 @@ */ -// Empty languages array, and preferred language -$LANGS = array(); $LANG = ''; - // Default STRIPPED_URI $_SERVER['STRIPPED_URI'] = $_SERVER['REQUEST_URI']; -// Explicit language is not specified -unset($EXPL_LANG); +// The code is encapsulated in a function, +// so the variable namespace is not polluted +list($LANG, $EXPL_LANG) = language_choose_code(); -// Specified for the request (GET/POST parameter) -if (isset($_REQUEST['lang'])) { - $EXPL_LANG = language_add($_REQUEST['lang']); -} +// Compatibility +if ($EXPL_LANG == '') { unset($EXPL_LANG); } -// Specified in a shortcut URL (eg. /en/echo or /pt_br/echo) -if (preg_match("!^/(\\w{2}(_\\w{2})?)/!", $_SERVER['REQUEST_URI'], $flang)) { - - // Put language into preference list - language_add($flang[1]); - - // Drop out langauge specification from URL, as this is already handled - $_SERVER['STRIPPED_URI'] = preg_replace( - "!^/$flang[1]/!", "/", $_SERVER['REQUEST_URI'] - ); +function language_choose_code() +{ + global $_REQUEST, $_SERVER; - // No need to set $EXPL_LANG here, as no page will be displayed, - // only a Location header is sent back to the user. -} + // Contains all the languages picked up by the + // process in priority order (without repeating codes) + $languages = array(); + + // Default values for languages + $explicitly_specified = ''; $selected = ''; + + // Specified for the request (GET/POST parameter) + if (isset($_REQUEST['lang'])) { + $explicitly_specified = language_add($_REQUEST['lang'], $languages); + } -// Specified in a manual URL (eg. manual/en/ or manual/pt_br/) -if (preg_match("!^/manual/(\\w{2}(_\\w{2})?)(/|$)!", $_SERVER['REQUEST_URI'], $flang)) { - $flang = language_add($flang[1]); - if (!isset($EXPL_LANG)) { - $EXPL_LANG = $flang; + // Specified in a shortcut URL (eg. /en/echo or /pt_br/echo) + if (preg_match("!^/(\\w{2}(_\\w{2})?)/!", $_SERVER['REQUEST_URI'], $flang)) { + + // Put language into preference list + language_add($flang[1], $languages); + + // Drop out langauge specification from URL, as this is already handled + $_SERVER['STRIPPED_URI'] = preg_replace( + "!^/$flang[1]/!", "/", $_SERVER['REQUEST_URI'] + ); + + // No need to set $explicitly_specified here, as no page will + // be displayed, only a Location header is sent back to the user. } -} -unset($flang); -// USER PREFERENCE HANDLING SHOULD BE ADDED HERE IN THE FUTURE + // Specified in a manual URL (eg. manual/en/ or manual/pt_br/) + if (preg_match("!^/manual/(\\w{2}(_\\w{2})?)(/|$)!", $_SERVER['REQUEST_URI'], $flang)) { + $flang = language_add($flang[1], $languages); + if (!isset($explicitly_specified)) { + $explicitly_specified = $flang; + } + } -// Specified by the user via the browser's Accept Language setting -// Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" -$browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); -foreach ($browser_accept as $langcode) { - - // Get language code part - list($langcode) = @explode(";", trim($langcode)); - - // Translation table for accept-language codes and phpdoc codes - switch($langcode) { - case "pt-br" : $langcode = 'pt_br'; break; - case "zh-cn" : $langcode = 'zh'; break; - case "zh-hk" : $langcode = 'hk'; break; - case "zh-tw" : $langcode = 'tw'; break; + // Specified by the user via the browser's Accept Language setting + // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" + $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); + $browser_langs = array(); + + // Go through all language preference specs + foreach ($browser_accept as $langpref) { + + // The language part is either a code or a code with a quality + // We cannot do anything with a * code, so it is skipped + // If the quality is missing, it is assumed to be 1 according to the RFC + if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($langpref), $found)) { + $quality = (isset($found[3]) ? (float) $found[3] : 1.0); + $browser_langs[] = array($found[1], $quality); + } + unset($found); } + + // Order the codes by quality + usort($browser_langs, "language_accept_order"); + + // For all languages found in the accept-language + foreach ($browser_langs as $langdata) { - // We do not support flavors of languages (except the ones above) - if (preg_match("!^(.+)-!", $langcode, $match)) { - $langcode = $match[1]; + // Translation table for accept-language codes and phpdoc codes + switch($langdata[0]) { + case "pt-br" : $langdata[0] = 'pt_br'; break; + case "zh-cn" : $langdata[0] = 'zh'; break; + case "zh-hk" : $langdata[0] = 'hk'; break; + case "zh-tw" : $langdata[0] = 'tw'; break; + } + + // We do not support flavors of languages (except the ones above) + // This is not in conformance to the RFC, but it here for user + // convinience reasons + if (preg_match("!^(.+)-!", $langdata[0], $match)) { + $langdata[0] = $match[1]; + } + + // Add language to priority order + language_add($langdata[0], $languages); } - - // Add language to priority order - language_add($langcode); -} -unset($browser_accept); unset($langcode); unset($match); -// Language preferred by this mirror site -language_add(default_language()); + // Language preferred by this mirror site + language_add(default_language(), $languages); -// Last default language is English -language_add("en"); + // Last default language is English + language_add("en", $languages); -// Try to find out what language is available on this mirror. -// As most of the language dependant operations involve manual -// page display (lookup, search, shortcuts), we will check for -// the index file of manuals. -foreach ($LANGS as $language) { - if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/manual/$language/index.php")) { - $LANG = $language; - break; + // Try to find out what language is available on this mirror. + // As most of the language dependant operations involve manual + // page display (lookup, search, shortcuts), we will check for + // the index file of manuals. + foreach ($languages as $language) { + if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/manual/$language/index.php")) { + $selected = $language; + break; + } } + + // Return with all found data + return array($selected, $explicitly_specified); } -// Do not pollute variable namespace -unset($LANGS); unset($language); // Add a language to the possible languages' list -function language_add($langcode) +function language_add($langcode, &$langs) { - global $LANGS, $LANGUAGES; + global $LANGUAGES; // Make language code lowercase $langcode = strtolower($langcode); @@ -124,10 +152,18 @@ // Append language code in priority order if it is not // there already and supported by the PHP site. Try to // lower number of file_exists() calls to the minumum... - if (!in_array($langcode, $LANGS) && isset($LANGUAGES[$langcode])) { $LANGS[] = $langcode; } + if (!in_array($langcode, $langs) && isset($LANGUAGES[$langcode])) { + $langs[] = $langcode; + } // Return with language code return $langcode; } -?> +// Order the array of compiled +// accept-language codes by quality value +function language_accept_order($a, $b) +{ + if ($a[1] == $b[1]) { return 0; } + return ($a[1] > $b[1]) ? -1 : 1; +}
Index: phpweb/include/langchooser.inc diff -u phpweb/include/langchooser.inc:1.11 phpweb/include/langchooser.inc:1.12 --- phpweb/include/langchooser.inc:1.11 Sun Mar 2 14:33:43 2003 +++ phpweb/include/langchooser.inc Sat Mar 8 15:43:16 2003 @@ -1,6 +1,6 @@ <?php // -*- C++ -*- -// $Id: langchooser.inc,v 1.11 2003/03/02 19:33:43 goba Exp $ +// $Id: langchooser.inc,v 1.12 2003/03/08 20:43:16 goba Exp $ /* @@ -24,96 +24,124 @@ */ -// Empty languages array, and preferred language -$LANGS = array(); $LANG = ''; - // Default STRIPPED_URI $_SERVER['STRIPPED_URI'] = $_SERVER['REQUEST_URI']; -// Explicit language is not specified -unset($EXPL_LANG); +// The code is encapsulated in a function, +// so the variable namespace is not polluted +list($LANG, $EXPL_LANG) = language_choose_code(); -// Specified for the request (GET/POST parameter) -if (isset($_REQUEST['lang'])) { - $EXPL_LANG = language_add($_REQUEST['lang']); -} +// Compatibility +if ($EXPL_LANG == '') { unset($EXPL_LANG); } -// Specified in a shortcut URL (eg. /en/echo or /pt_br/echo) -if (preg_match("!^/(\\w{2}(_\\w{2})?)/!", $_SERVER['REQUEST_URI'], $flang)) { - - // Put language into preference list - language_add($flang[1]); - - // Drop out langauge specification from URL, as this is already handled - $_SERVER['STRIPPED_URI'] = preg_replace( - "!^/$flang[1]/!", "/", $_SERVER['REQUEST_URI'] - ); +function language_choose_code() +{ + global $_REQUEST, $_SERVER; - // No need to set $EXPL_LANG here, as no page will be displayed, - // only a Location header is sent back to the user. -} + // Contains all the languages picked up by the + // process in priority order (without repeating codes) + $languages = array(); + + // Default values for languages + $explicitly_specified = ''; $selected = ''; + + // Specified for the request (GET/POST parameter) + if (isset($_REQUEST['lang'])) { + $explicitly_specified = language_add($_REQUEST['lang'], $languages); + } -// Specified in a manual URL (eg. manual/en/ or manual/pt_br/) -if (preg_match("!^/manual/(\\w{2}(_\\w{2})?)(/|$)!", $_SERVER['REQUEST_URI'], $flang)) { - $flang = language_add($flang[1]); - if (!isset($EXPL_LANG)) { - $EXPL_LANG = $flang; + // Specified in a shortcut URL (eg. /en/echo or /pt_br/echo) + if (preg_match("!^/(\\w{2}(_\\w{2})?)/!", $_SERVER['REQUEST_URI'], $flang)) { + + // Put language into preference list + language_add($flang[1], $languages); + + // Drop out langauge specification from URL, as this is already handled + $_SERVER['STRIPPED_URI'] = preg_replace( + "!^/$flang[1]/!", "/", $_SERVER['REQUEST_URI'] + ); + + // No need to set $explicitly_specified here, as no page will + // be displayed, only a Location header is sent back to the user. } -} -unset($flang); -// USER PREFERENCE HANDLING SHOULD BE ADDED HERE IN THE FUTURE + // Specified in a manual URL (eg. manual/en/ or manual/pt_br/) + if (preg_match("!^/manual/(\\w{2}(_\\w{2})?)(/|$)!", $_SERVER['REQUEST_URI'], $flang)) { + $flang = language_add($flang[1], $languages); + if (!isset($explicitly_specified)) { + $explicitly_specified = $flang; + } + } -// Specified by the user via the browser's Accept Language setting -// Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" -$browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); -foreach ($browser_accept as $langcode) { - - // Get language code part - list($langcode) = @explode(";", trim($langcode)); - - // Translation table for accept-language codes and phpdoc codes - switch($langcode) { - case "pt-br" : $langcode = 'pt_br'; break; - case "zh-cn" : $langcode = 'zh'; break; - case "zh-hk" : $langcode = 'hk'; break; - case "zh-tw" : $langcode = 'tw'; break; + // Specified by the user via the browser's Accept Language setting + // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" + $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); + $browser_langs = array(); + + // Go through all language preference specs + foreach ($browser_accept as $langpref) { + + // The language part is either a code or a code with a quality + // We cannot do anything with a * code, so it is skipped + // If the quality is missing, it is assumed to be 1 according to the RFC + if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($langpref), $found)) { + $quality = (isset($found[3]) ? (float) $found[3] : 1.0); + $browser_langs[] = array($found[1], $quality); + } + unset($found); } + + // Order the codes by quality + usort($browser_langs, "language_accept_order"); + + // For all languages found in the accept-language + foreach ($browser_langs as $langdata) { - // We do not support flavors of languages (except the ones above) - if (preg_match("!^(.+)-!", $langcode, $match)) { - $langcode = $match[1]; + // Translation table for accept-language codes and phpdoc codes + switch($langdata[0]) { + case "pt-br" : $langdata[0] = 'pt_br'; break; + case "zh-cn" : $langdata[0] = 'zh'; break; + case "zh-hk" : $langdata[0] = 'hk'; break; + case "zh-tw" : $langdata[0] = 'tw'; break; + } + + // We do not support flavors of languages (except the ones above) + // This is not in conformance to the RFC, but it here for user + // convinience reasons + if (preg_match("!^(.+)-!", $langdata[0], $match)) { + $langdata[0] = $match[1]; + } + + // Add language to priority order + language_add($langdata[0], $languages); } - - // Add language to priority order - language_add($langcode); -} -unset($browser_accept); unset($langcode); unset($match); -// Language preferred by this mirror site -language_add(default_language()); + // Language preferred by this mirror site + language_add(default_language(), $languages); -// Last default language is English -language_add("en"); + // Last default language is English + language_add("en", $languages); -// Try to find out what language is available on this mirror. -// As most of the language dependant operations involve manual -// page display (lookup, search, shortcuts), we will check for -// the index file of manuals. -foreach ($LANGS as $language) { - if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/manual/$language/index.php")) { - $LANG = $language; - break; + // Try to find out what language is available on this mirror. + // As most of the language dependant operations involve manual + // page display (lookup, search, shortcuts), we will check for + // the index file of manuals. + foreach ($languages as $language) { + if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/manual/$language/index.php")) { + $selected = $language; + break; + } } + + // Return with all found data + return array($selected, $explicitly_specified); } -// Do not pollute variable namespace -unset($LANGS); unset($language); // Add a language to the possible languages' list -function language_add($langcode) +function language_add($langcode, &$langs) { - global $LANGS, $LANGUAGES; + global $LANGUAGES; // Make language code lowercase $langcode = strtolower($langcode); @@ -124,10 +152,18 @@ // Append language code in priority order if it is not // there already and supported by the PHP site. Try to // lower number of file_exists() calls to the minumum... - if (!in_array($langcode, $LANGS) && isset($LANGUAGES[$langcode])) { $LANGS[] = $langcode; } + if (!in_array($langcode, $langs) && isset($LANGUAGES[$langcode])) { + $langs[] = $langcode; + } // Return with language code return $langcode; } -?> +// Order the array of compiled +// accept-language codes by quality value +function language_accept_order($a, $b) +{ + if ($a[1] == $b[1]) { return 0; } + return ($a[1] > $b[1]) ? -1 : 1; +}