Mercurial > SimpleWebPresenter
comparison accept-language.inc @ 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 |
comparison
equal
deleted
inserted
replaced
| 54:f938b292f046 | 55:88482c6636c4 |
|---|---|
| 2 /** | 2 /** |
| 3 * @file | 3 * @file |
| 4 * Functionality for determining language use | 4 * Functionality for determining language use |
| 5 */ | 5 */ |
| 6 class Language { | 6 class Language { |
| 7 function acceptedLanguages() { | 7 /** |
| 8 * Extracts the accepted languages from the GET query, sorted by | |
| 9 * preference(q-value). | |
| 10 * | |
| 11 * @return associative array of language codes with q value | |
| 12 */ | |
| 13 function accepted() { | |
| 8 $langs = array(); | 14 $langs = array(); |
| 9 | 15 |
| 10 /** | 16 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
| 11 * Extracts the accepted languages from the GET query, sorted by | 17 // break up string into pieces (languages and q factors) |
| 12 * preference(q-value). | 18 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); |
| 13 * | |
| 14 * @return associative array of language codes with q value | |
| 15 */ | |
| 16 function acceptedLanguages() { | |
| 17 $langs = array(); | |
| 18 | 19 |
| 19 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { | 20 if (count($lang_parse[1])) { |
| 20 // break up string into pieces (languages and q factors) | 21 // create a list like "en" => 0.8 |
| 21 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); | 22 $langs = array_combine($lang_parse[1], $lang_parse[4]); |
| 22 | 23 |
| 23 if (count($lang_parse[1])) { | 24 // set default to 1 for any without q factor |
| 24 // create a list like "en" => 0.8 | 25 foreach ($langs as $lang => $val) { |
| 25 $langs = array_combine($lang_parse[1], $lang_parse[4]); | 26 if ($val === '') $langs[$lang] = 1; |
| 27 } | |
| 26 | 28 |
| 27 // set default to 1 for any without q factor | 29 // sort list based on value |
| 28 foreach ($langs as $lang => $val) { | 30 arsort($langs, SORT_NUMERIC); |
| 29 if ($val === '') $langs[$lang] = 1; | 31 } |
| 30 } | 32 } |
| 33 return $langs; | |
| 34 } | |
| 31 | 35 |
| 32 // sort list based on value | 36 /** |
| 33 arsort($langs, SORT_NUMERIC); | 37 * From the list of desired languages, pick the best which we can serve. |
| 38 * | |
| 39 * @param $default what to choose if no match could be found. | |
| 40 */ | |
| 41 static function prefer($default) | |
| 42 { | |
| 43 $language = $default; | |
| 44 $langs = self::accepted(); | |
| 45 if ($langs) { | |
| 46 foreach ($langs as $l => $val) { | |
| 47 if (file_exists($l)) { | |
| 48 $language = $l; | |
| 49 break; | |
| 34 } | 50 } |
| 35 } | 51 } |
| 36 return $langs; | |
| 37 } | 52 } |
| 38 | 53 return $language; |
| 39 function preferLanguage($prefer) | |
| 40 { | |
| 41 $language = $prefer; | |
| 42 $langs = acceptedLanguages(); | |
| 43 if ($langs) { | |
| 44 foreach ($langs as $l => $val) { | |
| 45 if (file_exists($l)) { | |
| 46 $language = $l; | |
| 47 break; | |
| 48 } | |
| 49 } | |
| 50 } | |
| 51 return $language; | |
| 52 } | |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 ?> | 56 ?> |
