comparison Language.inc @ 56:0f1e08cdfff2

Move accept-language to Language to reflect new class name.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Thu, 11 Oct 2012 01:07:17 +0200
parents accept-language.inc@88482c6636c4
children 74f7b64bdb78
comparison
equal deleted inserted replaced
55:88482c6636c4 56:0f1e08cdfff2
1 <?php
2 /**
3 * @file
4 * Functionality for determining language use
5 */
6 class Language {
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() {
14 $langs = array();
15
16 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
17 // break up string into pieces (languages and q factors)
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);
19
20 if (count($lang_parse[1])) {
21 // create a list like "en" => 0.8
22 $langs = array_combine($lang_parse[1], $lang_parse[4]);
23
24 // set default to 1 for any without q factor
25 foreach ($langs as $lang => $val) {
26 if ($val === '') $langs[$lang] = 1;
27 }
28
29 // sort list based on value
30 arsort($langs, SORT_NUMERIC);
31 }
32 }
33 return $langs;
34 }
35
36 /**
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;
50 }
51 }
52 }
53 return $language;
54 }
55 }
56 ?>