|
1 <?php |
|
2 // $Id: language.inc,v 1.14.2.1 2009/01/02 23:37:48 dries Exp $ |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * Multiple language handling functionality. |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Choose a language for the page, based on language negotiation settings. |
|
11 */ |
|
12 function language_initialize() { |
|
13 global $user; |
|
14 |
|
15 // Configured presentation language mode. |
|
16 $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); |
|
17 // Get a list of enabled languages. |
|
18 $languages = language_list('enabled'); |
|
19 $languages = $languages[1]; |
|
20 |
|
21 switch ($mode) { |
|
22 case LANGUAGE_NEGOTIATION_NONE: |
|
23 return language_default(); |
|
24 |
|
25 case LANGUAGE_NEGOTIATION_DOMAIN: |
|
26 foreach ($languages as $language) { |
|
27 $parts = parse_url($language->domain); |
|
28 if (!empty($parts['host']) && ($_SERVER['HTTP_HOST'] == $parts['host'])) { |
|
29 return $language; |
|
30 } |
|
31 } |
|
32 return language_default(); |
|
33 |
|
34 case LANGUAGE_NEGOTIATION_PATH_DEFAULT: |
|
35 case LANGUAGE_NEGOTIATION_PATH: |
|
36 // $_GET['q'] might not be available at this time, because |
|
37 // path initialization runs after the language bootstrap phase. |
|
38 $args = isset($_GET['q']) ? explode('/', $_GET['q']) : array(); |
|
39 $prefix = array_shift($args); |
|
40 // Search prefix within enabled languages. |
|
41 foreach ($languages as $language) { |
|
42 if (!empty($language->prefix) && $language->prefix == $prefix) { |
|
43 // Rebuild $GET['q'] with the language removed. |
|
44 $_GET['q'] = implode('/', $args); |
|
45 return $language; |
|
46 } |
|
47 } |
|
48 if ($mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT) { |
|
49 // If we did not found the language by prefix, choose the default. |
|
50 return language_default(); |
|
51 } |
|
52 break; |
|
53 } |
|
54 |
|
55 // User language. |
|
56 if ($user->uid && isset($languages[$user->language])) { |
|
57 return $languages[$user->language]; |
|
58 } |
|
59 |
|
60 // Browser accept-language parsing. |
|
61 if ($language = language_from_browser()) { |
|
62 return $language; |
|
63 } |
|
64 |
|
65 // Fall back on the default if everything else fails. |
|
66 return language_default(); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Identify language from the Accept-language HTTP header we got. |
|
71 */ |
|
72 function language_from_browser() { |
|
73 // Specified by the user via the browser's Accept Language setting |
|
74 // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" |
|
75 $browser_langs = array(); |
|
76 |
|
77 if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
|
78 $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
|
79 for ($i = 0; $i < count($browser_accept); $i++) { |
|
80 // The language part is either a code or a code with a quality. |
|
81 // We cannot do anything with a * code, so it is skipped. |
|
82 // If the quality is missing, it is assumed to be 1 according to the RFC. |
|
83 if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($browser_accept[$i]), $found)) { |
|
84 $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] : 1.0); |
|
85 } |
|
86 } |
|
87 } |
|
88 |
|
89 // Order the codes by quality |
|
90 arsort($browser_langs); |
|
91 |
|
92 // Try to find the first preferred language we have |
|
93 $languages = language_list('enabled'); |
|
94 foreach ($browser_langs as $langcode => $q) { |
|
95 if (isset($languages['1'][$langcode])) { |
|
96 return $languages['1'][$langcode]; |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 /** |
|
102 * Rewrite URL's with language based prefix. Parameters are the same |
|
103 * as those of the url() function. |
|
104 */ |
|
105 function language_url_rewrite(&$path, &$options) { |
|
106 global $language; |
|
107 |
|
108 // Only modify relative (insite) URLs. |
|
109 if (!$options['external']) { |
|
110 |
|
111 // Language can be passed as an option, or we go for current language. |
|
112 if (!isset($options['language'])) { |
|
113 $options['language'] = $language; |
|
114 } |
|
115 |
|
116 switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) { |
|
117 case LANGUAGE_NEGOTIATION_NONE: |
|
118 // No language dependent path allowed in this mode. |
|
119 unset($options['language']); |
|
120 break; |
|
121 |
|
122 case LANGUAGE_NEGOTIATION_DOMAIN: |
|
123 if ($options['language']->domain) { |
|
124 // Ask for an absolute URL with our modified base_url. |
|
125 $options['absolute'] = TRUE; |
|
126 $options['base_url'] = $options['language']->domain; |
|
127 } |
|
128 break; |
|
129 |
|
130 case LANGUAGE_NEGOTIATION_PATH_DEFAULT: |
|
131 $default = language_default(); |
|
132 if ($options['language']->language == $default->language) { |
|
133 break; |
|
134 } |
|
135 // Intentionally no break here. |
|
136 |
|
137 case LANGUAGE_NEGOTIATION_PATH: |
|
138 if (!empty($options['language']->prefix)) { |
|
139 $options['prefix'] = $options['language']->prefix .'/'; |
|
140 } |
|
141 break; |
|
142 } |
|
143 } |
|
144 } |