| 1 | | <?php |
| 2 | | // $Id: language.inc,v 1.16 2008/04/14 17:48:33 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 | 73 | function language_initialize() { |
| 13 | 73 | global $user; |
| 14 | | |
| 15 | | // Configured presentation language mode. |
| 16 | 73 | $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE); |
| 17 | | // Get a list of enabled languages. |
| 18 | 73 | $languages = language_list('enabled'); |
| 19 | 73 | $languages = $languages[1]; |
| 20 | | |
| 21 | | switch ($mode) { |
| 22 | 73 | case LANGUAGE_NEGOTIATION_NONE: |
| 23 | 63 | return language_default(); |
| 24 | | |
| 25 | 10 | case LANGUAGE_NEGOTIATION_DOMAIN: |
| 26 | 0 | foreach ($languages as $language) { |
| 27 | 0 | $parts = parse_url($language->domain); |
| 28 | 0 | if (!empty($parts['host']) && ($_SERVER['SERVER_NAME'] ==
$parts['host'])) { |
| 29 | 0 | return $language; |
| 30 | 0 | } |
| 31 | 0 | } |
| 32 | 0 | return language_default(); |
| 33 | | |
| 34 | 10 | case LANGUAGE_NEGOTIATION_PATH_DEFAULT: |
| 35 | 10 | 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 | 10 | $args = isset($_GET['q']) ? explode('/', $_GET['q']) : array(); |
| 39 | 10 | $prefix = array_shift($args); |
| 40 | | // Search prefix within enabled languages. |
| 41 | 10 | foreach ($languages as $language) { |
| 42 | 10 | if (!empty($language->prefix) && $language->prefix == $prefix) { |
| 43 | | // Rebuild $GET['q'] with the language removed. |
| 44 | 1 | $_GET['q'] = implode('/', $args); |
| 45 | 1 | return $language; |
| 46 | 0 | } |
| 47 | 10 | } |
| 48 | 9 | if ($mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT) { |
| 49 | | // If we did not found the language by prefix, choose the default. |
| 50 | 0 | return language_default(); |
| 51 | 0 | } |
| 52 | 9 | break; |
| 53 | 0 | } |
| 54 | | |
| 55 | | // User language. |
| 56 | 9 | if ($user->uid && isset($languages[$user->language])) { |
| 57 | 0 | return $languages[$user->language]; |
| 58 | 0 | } |
| 59 | | |
| 60 | | // Browser accept-language parsing. |
| 61 | 9 | if ($language = language_from_browser()) { |
| 62 | 1 | return $language; |
| 63 | 0 | } |
| 64 | | |
| 65 | | // Fall back on the default if everything else fails. |
| 66 | 8 | return language_default(); |
| 67 | 0 | } |
| 68 | | |
| 69 | | /** |
| 70 | | * Identify language from the Accept-language HTTP header we got. |
| 71 | | */ |
| 72 | 73 | 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 | 9 | $browser_langs = array(); |
| 76 | | |
| 77 | 9 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
| 78 | 1 | $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
| 79 | 1 | 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 | 1 | if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!",
trim($browser_accept[$i]), $found)) { |
| 84 | 1 | $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] :
1.0); |
| 85 | 1 | } |
| 86 | 1 | } |
| 87 | 1 | } |
| 88 | | |
| 89 | | // Order the codes by quality |
| 90 | 9 | arsort($browser_langs); |
| 91 | | |
| 92 | | // Try to find the first preferred language we have |
| 93 | 9 | $languages = language_list('enabled'); |
| 94 | 9 | foreach ($browser_langs as $langcode => $q) { |
| 95 | 1 | if (isset($languages['1'][$langcode])) { |
| 96 | 1 | return $languages['1'][$langcode]; |
| 97 | 0 | } |
| 98 | 1 | } |
| 99 | 8 | } |
| 100 | | |
| 101 | | /** |
| 102 | | * Rewrite URL's with language based prefix. Parameters are the same |
| 103 | | * as those of the url() function. |
| 104 | | */ |
| 105 | 73 | function language_url_rewrite(&$path, &$options) { |
| 106 | 73 | global $language; |
| 107 | | |
| 108 | | // Only modify relative (insite) URLs. |
| 109 | 73 | if (!$options['external']) { |
| 110 | | |
| 111 | | // Language can be passed as an option, or we go for current language. |
| 112 | 73 | if (!isset($options['language'])) { |
| 113 | 73 | $options['language'] = $language; |
| 114 | 73 | } |
| 115 | | |
| 116 | 73 | switch (variable_get('language_negotiation',
LANGUAGE_NEGOTIATION_NONE)) { |
| 117 | 73 | case LANGUAGE_NEGOTIATION_NONE: |
| 118 | | // No language dependent path allowed in this mode. |
| 119 | 63 | unset($options['language']); |
| 120 | 63 | break; |
| 121 | | |
| 122 | 10 | case LANGUAGE_NEGOTIATION_DOMAIN: |
| 123 | 0 | if ($options['language']->domain) { |
| 124 | | // Ask for an absolute URL with our modified base_url. |
| 125 | 0 | $options['absolute'] = TRUE; |
| 126 | 0 | $options['base_url'] = $options['language']->domain; |
| 127 | 0 | } |
| 128 | 0 | break; |
| 129 | | |
| 130 | 10 | case LANGUAGE_NEGOTIATION_PATH_DEFAULT: |
| 131 | 0 | $default = language_default(); |
| 132 | 0 | if ($options['language']->language == $default->language) { |
| 133 | 0 | break; |
| 134 | 0 | } |
| 135 | | // Intentionally no break here. |
| 136 | | |
| 137 | 10 | case LANGUAGE_NEGOTIATION_PATH: |
| 138 | 10 | if (!empty($options['language']->prefix)) { |
| 139 | 2 | $options['prefix'] = $options['language']->prefix . '/'; |
| 140 | 2 | } |
| 141 | 10 | break; |
| 142 | 0 | } |
| 143 | 73 | } |
| 144 | 73 | } |
| 145 | 73 | |