Code coverage for /20080809/includes/language.inc

Line #Times calledCode
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
 */
1262
function language_initialize() {
1362
  global $user;
14
15
  // Configured presentation language mode.
1662
  $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
17
  // Get a list of enabled languages.
1862
  $languages = language_list('enabled');
1962
  $languages = $languages[1];
20
21
  switch ($mode) {
2262
    case LANGUAGE_NEGOTIATION_NONE:
2362
      return language_default();
24
250
    case LANGUAGE_NEGOTIATION_DOMAIN:
260
      foreach ($languages as $language) {
270
        $parts = parse_url($language->domain);
280
        if (!empty($parts['host']) && ($_SERVER['SERVER_NAME'] ==
$parts['host'])) {
290
          return $language;
300
        }
310
      }
320
      return language_default();
33
340
    case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
350
    case LANGUAGE_NEGOTIATION_PATH:
36
      // $_GET['q'] might not be available at this time, because
37
      // path initialization runs after the language bootstrap phase.
380
      $args = isset($_GET['q']) ? explode('/', $_GET['q']) : array();
390
      $prefix = array_shift($args);
40
      // Search prefix within enabled languages.
410
      foreach ($languages as $language) {
420
        if (!empty($language->prefix) && $language->prefix == $prefix) {
43
          // Rebuild $GET['q'] with the language removed.
440
          $_GET['q'] = implode('/', $args);
450
          return $language;
460
        }
470
      }
480
      if ($mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT) {
49
        // If we did not found the language by prefix, choose the default.
500
        return language_default();
510
      }
520
      break;
530
  }
54
55
  // User language.
560
  if ($user->uid && isset($languages[$user->language])) {
570
    return $languages[$user->language];
580
  }
59
60
  // Browser accept-language parsing.
610
  if ($language = language_from_browser()) {
620
    return $language;
630
  }
64
65
  // Fall back on the default if everything else fails.
660
  return language_default();
670
}
68
69
/**
70
 * Identify language from the Accept-language HTTP header we got.
71
 */
7262
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"
750
  $browser_langs = array();
76
770
  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
780
    $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
790
    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.
830
      if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!",
trim($browser_accept[$i]), $found)) {
840
        $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] :
1.0);
850
      }
860
    }
870
  }
88
89
  // Order the codes by quality
900
  arsort($browser_langs);
91
92
  // Try to find the first preferred language we have
930
  $languages = language_list('enabled');
940
  foreach ($browser_langs as $langcode => $q) {
950
    if (isset($languages['1'][$langcode])) {
960
      return $languages['1'][$langcode];
970
    }
980
  }
990
}
100
101
/**
102
 * Rewrite URL's with language based prefix. Parameters are the same
103
 * as those of the url() function.
104
 */
10562
function language_url_rewrite(&$path, &$options) {
10662
  global $language;
107
108
  // Only modify relative (insite) URLs.
10962
  if (!$options['external']) {
110
111
    // Language can be passed as an option, or we go for current language.
11262
    if (!isset($options['language'])) {
11362
      $options['language'] = $language;
11462
    }
115
11662
    switch (variable_get('language_negotiation',
LANGUAGE_NEGOTIATION_NONE)) {
11762
      case LANGUAGE_NEGOTIATION_NONE:
118
        // No language dependent path allowed in this mode.
11962
        unset($options['language']);
12062
        break;
121
1220
      case LANGUAGE_NEGOTIATION_DOMAIN:
1230
        if ($options['language']->domain) {
124
          // Ask for an absolute URL with our modified base_url.
1250
          $options['absolute'] = TRUE;
1260
          $options['base_url'] = $options['language']->domain;
1270
        }
1280
        break;
129
1300
      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
1310
        $default = language_default();
1320
        if ($options['language']->language == $default->language) {
1330
          break;
1340
        }
135
        // Intentionally no break here.
136
1370
      case LANGUAGE_NEGOTIATION_PATH:
1380
        if (!empty($options['language']->prefix)) {
1390
          $options['prefix'] = $options['language']->prefix . '/';
1400
        }
1410
        break;
1420
    }
14362
  }
14462
}
14562