language.inc 13.3 KB
Newer Older
1 2 3 4 5 6 7 8
<?php

/**
 * @file
 * Multiple language handling functionality.
 */

/**
9 10
 * No language negotiation. The default language is used.
 */
11
const LANGUAGE_NEGOTIATION_DEFAULT = 'language-default';
12 13

/**
14 15 16 17 18 19 20 21 22
 * Chooses a language for the given type based on language negotiation settings.
 *
 * @param $type
 *   The language type key.
 *
 * @return
 *   The negotiated language object.
 */
function language_types_initialize($type) {
23 24
  // Execute the language negotiation methods in the order they were set up and
  // return the first valid language found.
25 26
  $negotiation = variable_get("language_negotiation_$type", array());

27
  foreach ($negotiation as $method_id => $method) {
28 29 30 31
    // Skip negotiation methods not appropriate for this type.
    if (isset($method['types']) && !in_array($type, $method['types'])) {
      continue;
    }
32
    $language = language_negotiation_method_invoke($method_id, $method);
33
    if ($language) {
34 35
      // Remember the method ID used to detect the language.
      $language->method_id = $method_id;
36 37 38 39 40 41
      return $language;
    }
  }

  // If no other language was found use the default one.
  $language = language_default();
42
  $language->method_id = LANGUAGE_NEGOTIATION_DEFAULT;
43 44 45 46 47
  return $language;
}

/**
 * Returns information about all defined language types.
48 49
 *
 * @return
50 51 52 53
 *   An associative array of language type information arrays keyed by type
 *   names. Based on information from hook_language_types_info().
 *
 * @see hook_language_types_info().
54 55 56 57 58 59 60 61 62 63 64 65 66 67
 */
function language_types_info() {
  $language_types = &drupal_static(__FUNCTION__);

  if (!isset($language_types)) {
    $language_types = module_invoke_all('language_types_info');
    // Let other modules alter the list of language types.
    drupal_alter('language_types_info', $language_types);
  }

  return $language_types;
}

/**
68
 * Returns only the configurable language types.
69 70
 *
 * A language type maybe configurable or fixed. A fixed language type is a type
71
 * whose negotiation values are unchangeable and defined while defining the
72 73
 * language type itself.
 *
74
 * @param $stored
75 76 77 78 79
 *   (optional) By default, retrieves values from the 'language_types' variable
 *   to avoid unnecessary hook invocations. If set to FALSE, retrieves values
 *   from the actual language type definitions. This allows reaction to
 *   alterations performed on the definitions by modules installed after the
 *   'language_types' variable is set.
80
 *
81 82 83
 * @return
 *   An array of language type names.
 */
84
function language_types_get_configurable($stored = TRUE) {
85 86
  $configurable = &drupal_static(__FUNCTION__);

87
  if ($stored && !isset($configurable)) {
88
    $types = variable_get('language_types', language_types_get_default());
89 90 91
    $configurable = array_keys(array_filter($types));
  }

92 93 94 95 96 97 98 99 100 101
  if (!$stored) {
    $result = array();
    foreach (language_types_info() as $type => $info) {
      if (!isset($info['fixed'])) {
        $result[] = $type;
      }
    }
    return $result;
  }

102 103 104 105
  return $configurable;
}

/**
106
 * Disables the given language types.
107 108 109 110 111
 *
 * @param $types
 *   An array of language types.
 */
function language_types_disable($types) {
112
  $enabled_types = variable_get('language_types', language_types_get_default());
113 114 115 116 117 118 119 120

  foreach ($types as $type) {
    unset($enabled_types[$type]);
  }

  variable_set('language_types', $enabled_types);
}

121 122 123 124 125 126 127 128 129 130 131 132 133
/**
 * Updates the language type configuration.
 */
function language_types_set() {
  // Ensure that we are getting the defined language negotiation information. An
  // invocation of module_enable() or module_disable() could outdate the cached
  // information.
  drupal_static_reset('language_types_info');
  drupal_static_reset('language_negotiation_info');

  // Determine which language types are configurable and which not by checking
  // whether the 'fixed' key is defined. Non-configurable (fixed) language types
  // have their language negotiation settings stored there.
134
  $language_types = array();
135
  $negotiation_info = language_negotiation_info();
136 137 138
  foreach (language_types_info() as $type => $info) {
    if (isset($info['fixed'])) {
      $language_types[$type] = FALSE;
139 140 141 142
      $method_weights = array();
      foreach ($info['fixed'] as $weight => $method_id) {
        if (isset($negotiation_info[$method_id])) {
          $method_weights[$method_id] = $weight;
143 144
        }
      }
145
      language_negotiation_set($type, $method_weights);
146 147 148 149 150 151
    }
    else {
      $language_types[$type] = TRUE;
    }
  }

152
  // Save enabled language types.
153 154
  variable_set('language_types', $language_types);

155 156 157
  // Ensure that subsequent calls of language_types_get_configurable() return
  // the updated language type information.
  drupal_static_reset('language_types_get_configurable');
158 159
}

160
/**
161
 * Returns the ID of the language type's first language negotiation method.
162 163
 *
 * @param $type
164
 *   The language type.
165
 */
166
function language_negotiation_method_get_first($type) {
167
  $negotiation = variable_get("language_negotiation_$type", array());
168
  return empty($negotiation) ? LANGUAGE_NEGOTIATION_DEFAULT : key($negotiation);
169 170 171
}

/**
172
 * Checks if a language negotiation method is enabled for a language type.
173
 *
174 175 176 177 178
 * @param $method_id
 *   The language negotiation method ID.
 * @param $type
 *   (optional) The language type. If none is passed, all the configurable
 *   language types will be inspected.
179 180
 *
 * @return
181 182
 *   TRUE if the method is enabled for at least one of the given language
 *   types, or FALSE otherwise.
183
 */
184 185 186 187 188 189
function language_negotiation_method_enabled($method_id, $type = NULL) {
  $language_types = !empty($type) ? array($type) : language_types_get_configurable();

  foreach ($language_types as $type) {
    $negotiation = variable_get("language_negotiation_$type", array());
    if (isset($negotiation[$method_id])) {
190 191 192 193 194 195 196 197
      return TRUE;
    }
  }

  return FALSE;
}

/**
198
 * Returns the language switch links for the given language type.
199 200
 *
 * @param $type
201
 *   The language type.
202 203 204 205 206 207 208 209 210 211
 * @param $path
 *   The internal path the switch links will be relative to.
 *
 * @return
 *   A keyed array of links ready to be themed.
 */
function language_negotiation_get_switch_links($type, $path) {
  $links = FALSE;
  $negotiation = variable_get("language_negotiation_$type", array());

212 213 214 215
  foreach ($negotiation as $method_id => $method) {
    if (isset($method['callbacks']['language_switch'])) {
      if (isset($method['file'])) {
        require_once DRUPAL_ROOT . '/' . $method['file'];
216
      }
217

218
      $callback = $method['callbacks']['language_switch'];
219 220 221 222 223
      $result = $callback($type, $path);

      if (!empty($result)) {
        // Allow modules to provide translations for specific links.
        drupal_alter('language_switch_links', $result, $type, $path);
224
        $links = (object) array('links' => $result, 'method_id' => $method_id);
225
        break;
226
      }
227 228 229 230 231 232
    }
  }

  return $links;
}

233
/**
234
 * Removes any language negotiation methods that are no longer defined.
235 236 237 238 239 240 241 242
 */
function language_negotiation_purge() {
  // Ensure that we are getting the defined language negotiation information. An
  // invocation of module_enable() or module_disable() could outdate the cached
  // information.
  drupal_static_reset('language_negotiation_info');
  drupal_static_reset('language_types_info');

243
  $negotiation_info = language_negotiation_info();
244 245
  foreach (language_types_info() as $type => $type_info) {
    $weight = 0;
246 247 248 249
    $method_weights = array();
    foreach (variable_get("language_negotiation_$type", array()) as $method_id => $method) {
      if (isset($negotiation_info[$method_id])) {
        $method_weights[$method_id] = $weight++;
250 251
      }
    }
252
    language_negotiation_set($type, $method_weights);
253 254
  }
}
255 256

/**
257
 * Saves a list of language negotiation methods for a language type.
258 259
 *
 * @param $type
260 261 262
 *   The language type.
 * @param $method_weights
 *   An array of language negotiation method weights keyed by method id.
263
 */
264
function language_negotiation_set($type, $method_weights) {
265
  // Save only the necessary fields.
266
  $method_fields = array('callbacks', 'file', 'cache');
267 268

  $negotiation = array();
269
  $negotiation_info = language_negotiation_info();
270
  $default_types = language_types_get_configurable(FALSE);
271

272 273 274 275 276 277 278 279 280 281 282
  // Order the language negotiation method list by weight.
  asort($method_weights);

  foreach ($method_weights as $method_id => $weight) {
    if (isset($negotiation_info[$method_id])) {
      $method = $negotiation_info[$method_id];
      // If the language negotiation method does not express any preference
      // about types, make it available for any configurable type.
      $types = array_flip(isset($method['types']) ? $method['types'] : $default_types);
      // Check if the language negotiation method is defined and has the right
      // type.
283
      if (isset($types[$type])) {
284 285 286 287
        $method_data = array();
        foreach ($method_fields as $field) {
          if (isset($method[$field])) {
            $method_data[$field] = $method[$field];
288 289
          }
        }
290
        $negotiation[$method_id] = $method_data;
291 292
      }
    }
293
  }
294

295 296 297 298
  variable_set("language_negotiation_$type", $negotiation);
}

/**
299
 * Returns all defined language negotiation methods.
300 301
 *
 * @return
302
 *   An array of language negotiation methods.
303 304
 */
function language_negotiation_info() {
305
  $negotiation_info = &drupal_static(__FUNCTION__);
306

307 308 309
  if (!isset($negotiation_info)) {
    // Collect all the module-defined language negotiation methods.
    $negotiation_info = module_invoke_all('language_negotiation_info');
310

311 312
    // Add the default language negotiation method.
    $negotiation_info[LANGUAGE_NEGOTIATION_DEFAULT] = array(
313 314
      'callbacks' => array('language' => 'language_from_default'),
      'weight' => 10,
315
      'name' => t('Default language'),
316
      'description' => t('Use the default site language (@language_name).', array('@language_name' => language_default()->name)),
317
      'config' => 'admin/config/regional/language',
318 319
    );

320 321
     // Let other modules alter the list of language negotiation methods.
     drupal_alter('language_negotiation_info', $negotiation_info);
322
  }
323

324
  return $negotiation_info;
325 326 327
}

/**
328
 * Invokes a language negotiation method and caches the results.
329
 *
330 331 332 333 334
 * @param $method_id
 *   The language negotiation method ID.
 * @param $method
 *   (optional) The language negotiation method to be invoked. If not passed it
 *   will be explicitly loaded through language_negotiation_info().
335 336
 *
 * @return
337
 *   The language negotiation method's return value.
338
 */
339
function language_negotiation_method_invoke($method_id, $method = NULL) {
340
  $results = &drupal_static(__FUNCTION__);
341

342
  if (!isset($results[$method_id])) {
343 344
    global $user;

345
    $languages = language_list();
346

347 348 349
    if (!isset($method)) {
      $negotiation_info = language_negotiation_info();
      $method = $negotiation_info[$method_id];
350 351
    }

352 353
    if (isset($method['file'])) {
      require_once DRUPAL_ROOT . '/' . $method['file'];
354
    }
355

356 357 358 359
    // If the language negotiation method has no cache preference or this is
    // satisfied we can execute the callback.
    $cache = !isset($method['cache']) || $user->uid || $method['cache'] == variable_get('cache', 0);
    $callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE;
360
    $langcode = $cache && function_exists($callback) ? $callback($languages) : FALSE;
361
    $results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
362
  }
363

364
  // Since objects are resources we need to return a clone to prevent the
365 366 367 368
  // language negotiation method cache to be unintentionally altered. The same
  // language negotiation methods might be used with different language types
  // based on configuration.
  return !empty($results[$method_id]) ? clone($results[$method_id]) : $results[$method_id];
369
}
370 371

/**
372
 * Returns the default language code.
373 374 375
 *
 * @return
 *   The default language code.
376
 */
377
function language_from_default() {
378
  return language_default()->langcode;
379
}
380

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/**
 * Split the given path into prefix and actual path.
 *
 * Parse the given path and return the language object identified by the
 * prefix and the actual path.
 *
 * @param $path
 *   The path to split.
 * @param $languages
 *   An array of valid languages.
 *
 * @return
 *   An array composed of:
 *    - A language object corresponding to the identified prefix on success,
 *      FALSE otherwise.
 *    - The path without the prefix on success, the given path otherwise.
 */
function language_url_split_prefix($path, $languages) {
399
  $args = empty($path) ? array() : explode('/', $path);
400 401 402
  $prefix = array_shift($args);

  // Search prefix within enabled languages.
403
  $prefixes = language_negotiation_url_prefixes();
404
  foreach ($languages as $language) {
405
    if (isset($prefixes[$language->langcode]) && $prefixes[$language->langcode] == $prefix) {
406 407 408 409 410 411 412
      // Rebuild $path with the language removed.
      return array($language, implode('/', $args));
    }
  }

  return array(FALSE, $path);
}
413 414 415 416 417 418 419 420 421 422 423 424 425 426

/**
 * Return the possible fallback languages ordered by language weight.
 *
 * @param
 *   The language type.
 *
 * @return
 *   An array of language codes.
 */
function language_fallback_get_candidates($type = LANGUAGE_TYPE_CONTENT) {
  $fallback_candidates = &drupal_static(__FUNCTION__);

  if (!isset($fallback_candidates)) {
427
    // Get languages ordered by weight, add LANGUAGE_NOT_SPECIFIED at the end.
428
    $fallback_candidates = array_keys(language_list());
429
    $fallback_candidates[] = LANGUAGE_NOT_SPECIFIED;
430 431

    // Let other modules hook in and add/change candidates.
432
    drupal_alter('language_fallback_candidates', $fallback_candidates);
433 434 435 436
  }

  return $fallback_candidates;
}