[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/includes/ -> language.inc (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7