comparison Language.inc.php @ 134:b6b4a58c7625

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