Mercurial > SimpleWebPresenter
changeset 55:88482c6636c4
Yet another fix for failed merge.
Fix flag.php to use new Language.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 11 Oct 2012 01:04:23 +0200 |
| parents | f938b292f046 |
| children | 0f1e08cdfff2 |
| files | accept-language.inc flag.php |
| diffstat | 2 files changed, 41 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/accept-language.inc Thu Oct 11 00:57:16 2012 +0200 +++ b/accept-language.inc Thu Oct 11 01:04:23 2012 +0200 @@ -4,52 +4,53 @@ * Functionality for determining language use */ class Language { - function acceptedLanguages() { + /** + * Extracts the accepted languages from the GET query, sorted by + * preference(q-value). + * + * @return associative array of language codes with q value + */ + function accepted() { $langs = array(); - /** - * Extracts the accepted languages from the GET query, sorted by - * preference(q-value). - * - * @return associative array of language codes with q value - */ - function acceptedLanguages() { - $langs = array(); + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + // break up string into pieces (languages and q factors) + preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); + + if (count($lang_parse[1])) { + // create a list like "en" => 0.8 + $langs = array_combine($lang_parse[1], $lang_parse[4]); + + // set default to 1 for any without q factor + foreach ($langs as $lang => $val) { + if ($val === '') $langs[$lang] = 1; + } - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - // break up string into pieces (languages and q factors) - preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); - - if (count($lang_parse[1])) { - // create a list like "en" => 0.8 - $langs = array_combine($lang_parse[1], $lang_parse[4]); + // sort list based on value + arsort($langs, SORT_NUMERIC); + } + } + return $langs; + } - // set default to 1 for any without q factor - foreach ($langs as $lang => $val) { - if ($val === '') $langs[$lang] = 1; - } - - // sort list based on value - arsort($langs, SORT_NUMERIC); + /** + * From the list of desired languages, pick the best which we can serve. + * + * @param $default what to choose if no match could be found. + */ + static function prefer($default) + { + $language = $default; + $langs = self::accepted(); + if ($langs) { + foreach ($langs as $l => $val) { + if (file_exists($l)) { + $language = $l; + break; } } - return $langs; } - - function preferLanguage($prefer) - { - $language = $prefer; - $langs = acceptedLanguages(); - if ($langs) { - foreach ($langs as $l => $val) { - if (file_exists($l)) { - $language = $l; - break; - } - } - } - return $language; - } + return $language; } } ?> \ No newline at end of file
