Code coverage for /20081101/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
 */
1273
function language_initialize() {
1373
  global $user;
14
15
  // Configured presentation language mode.
1673
  $mode = variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE);
17
  // Get a list of enabled languages.
1873
  $languages = language_list('enabled');
1973
  $languages = $languages[1];
20
21
  switch ($mode) {
2273
    case LANGUAGE_NEGOTIATION_NONE:
2363
      return language_default();
24
2510
    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
3410
    case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
3510
    case LANGUAGE_NEGOTIATION_PATH:
36
      // $_GET['q'] might not be available at this time, because
37
      // path initialization runs after the language bootstrap phase.
3810
      $args = isset($_GET['q']) ? explode('/', $_GET['q']) : array();
3910
      $prefix = array_shift($args);
40
      // Search prefix within enabled languages.
4110
      foreach ($languages as $language) {
4210
        if (!empty($language->prefix) && $language->prefix == $prefix) {
43
          // Rebuild $GET['q'] with the language removed.
441
          $_GET['q'] = implode('/', $args);
451
          return $language;
460
        }
4710
      }
489
      if ($mode == LANGUAGE_NEGOTIATION_PATH_DEFAULT) {
49
        // If we did not found the language by prefix, choose the default.
500
        return language_default();
510
      }
529
      break;
530
  }
54
55
  // User language.
569
  if ($user->uid && isset($languages[$user->language])) {
570
    return $languages[$user->language];
580
  }
59
60
  // Browser accept-language parsing.
619
  if ($language = language_from_browser()) {
621
    return $language;
630
  }
64
65
  // Fall back on the default if everything else fails.
668
  return language_default();
670
}
68
69
/**
70
 * Identify language from the Accept-language HTTP header we got.
71
 */
7273
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"
759
  $browser_langs = array();
76
779
  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
781
    $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']);
791
    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.
831
      if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!",
trim($browser_accept[$i]), $found)) {
841
        $browser_langs[$found[1]] = (isset($found[3]) ? (float) $found[3] :
1.0);
851
      }
861
    }
871
  }
88
89
  // Order the codes by quality
909
  arsort($browser_langs);
91
92
  // Try to find the first preferred language we have
939
  $languages = language_list('enabled');
949
  foreach ($browser_langs as $langcode => $q) {
951
    if (isset($languages['1'][$langcode])) {
961
      return $languages['1'][$langcode];
970
    }
981
  }
998
}
100
101
/**
102
 * Rewrite URL's with language based prefix. Parameters are the same
103
 * as those of the url() function.
104
 */
10573
function language_url_rewrite(&$path, &$options) {
10673
  global $language;
107
108
  // Only modify relative (insite) URLs.
10973
  if (!$options['external']) {
110
111
    // Language can be passed as an option, or we go for current language.
11273
    if (!isset($options['language'])) {
11373
      $options['language'] = $language;
11473
    }
115
11673
    switch (variable_get('language_negotiation',
LANGUAGE_NEGOTIATION_NONE)) {
11773
      case LANGUAGE_NEGOTIATION_NONE:
118
        // No language dependent path allowed in this mode.
11963
        unset($options['language']);
12063
        break;
121
12210
      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
13010
      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
13710
      case LANGUAGE_NEGOTIATION_PATH:
13810
        if (!empty($options['language']->prefix)) {
1392
          $options['prefix'] = $options['language']->prefix . '/';
1402
        }
14110
        break;
1420
    }
14373
  }
14473
}
14573