Projet

Général

Profil

Paste
Télécharger (19,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n.module @ ba09eb79

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Internationalization (i18n) module.
6
 *
7
 * This module extends multilingual support being the base module for the i18n package.
8
 * - Multilingual variables
9
 * - Extended languages for nodes
10
 * - Extended language API
11
 *
12
 * @author Jose A. Reyero, 2004
13
 */
14
15
// All multilingual options disabled
16
define('I18N_LANGUAGE_DISABLED', 0);
17
// Language list will include all enabled languages
18
define('I18N_LANGUAGE_ENABLED', 1);
19
// Language list will include also disabled languages
20
define('I18N_LANGUAGE_EXTENDED', 4);
21
// Disabled languages will be hidden when possible
22
define('I18N_LANGUAGE_HIDDEN', 8);
23
// All defined languages will be allowed but hidden when possible
24
define('I18N_LANGUAGE_EXTENDED_NOT_DISPLAYED', I18N_LANGUAGE_EXTENDED | I18N_LANGUAGE_HIDDEN);
25
26
// No multilingual options
27
define('I18N_MODE_NONE', 0);
28
// Localizable object. Run through the localization system
29
define('I18N_MODE_LOCALIZE', 1);
30
// Predefined language for this object and all related ones.
31
define('I18N_MODE_LANGUAGE', 2);
32
// Multilingual objects, translatable but not localizable.
33
define('I18N_MODE_TRANSLATE', 4);
34
// Objects are translatable (if they have language or localizable if not)
35
define('I18N_MODE_MULTIPLE', I18N_MODE_LOCALIZE | I18N_MODE_TRANSLATE);
36
37
/**
38
 * Global variable for i18n context language.
39
 */
40
define('I18N_LANGUAGE_TYPE_CONTEXT', 'i18n_language_context');
41
42
/**
43
 * Implements hook_boot().
44
 */
45
function i18n_boot() {
46
  // Just make sure the module is loaded for boot and the API is available.
47
}
48
49
/**
50
 * Implements hook_hook_info().
51
 */
52
function i18n_hook_info() {
53
  $hooks['i18n_object_info'] = array(
54
    'group' => 'i18n',
55
  );
56
  return $hooks;
57
}
58
59
/**
60
 * WARNING: Obsolete function, use other i18n_language_* instead.
61
 *
62
 * Get global language object, make sure it is initialized
63
 *
64
 * @param $language
65
 *   Language code or language object to convert to valid language object
66
 */
67
function i18n_language($language = NULL) {
68
  if ($language && ($language_object = i18n_language_object($language))) {
69
    return $language_object;
70
  }
71
  else {
72
    return i18n_language_interface();
73
  }
74
}
75
76
/**
77
 * Get language object from language code or object.
78
 *
79
 * @param $language
80
 *   Language code or language object to convert to valid language object.
81
 * @return
82
 *   Language object if this is an object or a valid language code.
83
 */
84
function i18n_language_object($language) {
85
  if (is_object($language)) {
86
    return $language;
87
  }
88
  else {
89
    $list = language_list();
90
    return isset($list[$language]) ? $list[$language] : NULL;
91
  }
92
}
93
94
/**
95
 * Get interface language, make sure it is initialized.
96
 */
97
function i18n_language_interface() {
98
  if (empty($GLOBALS[LANGUAGE_TYPE_INTERFACE])) {
99
    // We don't have language yet, initialize the language system and retry
100
    drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
101
  }
102
  return $GLOBALS[LANGUAGE_TYPE_INTERFACE];
103
}
104
105
/**
106
 * Get content language, make sure it is initialized.
107
 */
108
function i18n_language_content() {
109
  if (empty($GLOBALS[LANGUAGE_TYPE_CONTENT])) {
110
    // We don't have language yet, initialize the language system and retry
111
    drupal_bootstrap(DRUPAL_BOOTSTRAP_LANGUAGE);
112
  }
113
  return $GLOBALS[LANGUAGE_TYPE_CONTENT];
114
}
115
116
/**
117
 * Get / set language from current context / content.
118
 *
119
 * Depending on the page content we may need to use a different language for some operations.
120
 *
121
 * This should be the language of the specific page content. I.e. node language for node pages
122
 * or term language for taxonomy term page.
123
 *
124
 * @param $language
125
 *   Optional language object to set for context.
126
 * @return
127
 *   Context language object, which defaults to content language.
128
 *
129
 * @see hook_i18n_context_language().
130
 */
131
function i18n_language_context($language = NULL) {
132
  if ($language) {
133
    $GLOBALS[I18N_LANGUAGE_TYPE_CONTEXT] = $language;
134
  }
135
  elseif (!isset($GLOBALS[I18N_LANGUAGE_TYPE_CONTEXT])) {
136
    // It will default to content language.
137
    $GLOBALS[I18N_LANGUAGE_TYPE_CONTEXT] = i18n_language_content();
138
    // Get language from the first module that provides it.
139
    foreach (module_implements('i18n_context_language') as $module) {
140
      if ($language = module_invoke($module, 'i18n_context_language')) {
141
        $GLOBALS[I18N_LANGUAGE_TYPE_CONTEXT] = $language;
142
        break;
143
      }
144
    }
145
  }
146
  return $GLOBALS[I18N_LANGUAGE_TYPE_CONTEXT];
147
}
148
149
/**
150
 * Menu object loader, language
151
 */
152
function i18n_language_load($langcode) {
153
  $list = language_list();
154
  return isset($list[$langcode]) ? $list[$langcode] : FALSE;
155
}
156
157
/**
158
 * Get language selector form element
159
 */
160
function i18n_element_language_select($default = LANGUAGE_NONE) {
161
  if (is_object($default) || is_array($default)) {
162
    $default = i18n_object_langcode($default, LANGUAGE_NONE);
163
  }
164
  return array(
165
    '#type' => 'select',
166
    '#title' => t('Language'),
167
    '#default_value' => $default,
168
    '#options' => array(LANGUAGE_NONE => t('Language neutral')) + i18n_language_list(),
169
  );
170
}
171
172
/**
173
 * Get language field for hook_field_extra_fields()
174
 */
175
function i18n_language_field_extra() {
176
  return array(
177
    'form' => array(
178
      'language' => array(
179
        'label' => t('Language'),
180
        'description' => t('Language selection'),
181
        'weight' => 0,
182
      ),
183
    ),
184
    'display' => array(
185
      'language' => array(
186
        'label' => t('Language'),
187
        'description' => t('Language'),
188
        'weight' => 0,
189
      ),
190
    ),
191
  );
192
}
193
194
/**
195
 * Get full language list
196
 *
197
 * @todo See about creating a permission for seeing disabled languages
198
 */
199
function i18n_language_list($field = 'name', $mode = NULL) {
200
  $mode = isset($mode) ? $mode : variable_get('i18n_language_list', I18N_LANGUAGE_ENABLED);
201
  return locale_language_list($field, I18N_LANGUAGE_EXTENDED & $mode);
202
}
203
204
/**
205
 * Get language name for any defined (enabled or not) language
206
 *
207
 * @see locale_language_list()
208
 */
209
function i18n_language_name($lang) {
210
  $list = &drupal_static(__FUNCTION__);
211
  if (!isset($list)) {
212
    $list = locale_language_list('name', TRUE);
213
  }
214
  if (!$lang || $lang === LANGUAGE_NONE) {
215
    return t('Undefined');
216
  }
217
  elseif (isset($list[$lang])) {
218
    return check_plain($list[$lang]);
219
  }
220
  else {
221
    return t('Unknown');
222
  }
223
}
224
225
/**
226
 * Get valid language code for current page or check whether the code is a defined language
227
 */
228
function i18n_langcode($langcode = NULL) {
229
  return $langcode && $langcode !== LANGUAGE_NONE ? $langcode : i18n_language()->language;
230
}
231
232
/**
233
 * Implements hook_help().
234
 */
235
function i18n_help($path = 'admin/help#i18n', $arg) {
236
  switch ($path) {
237
    case 'admin/help#i18n' :
238
      $output = '<p>' . t('This module improves support for multilingual content in Drupal sites:') . '</p>';
239
      $output .= '<ul>';
240
      $output .= '<li>' . t('Shows content depending on page language.') . '</li>';
241
      $output .= '<li>' . t('Handles multilingual variables.') . '</li>';
242
      $output .= '<li>' . t('Extended language option for chosen content types. For these content types transations will be allowed for all defined languages, not only for enabled ones.') . '</li>';
243
      $output .= '<li>' . t('Provides a block for language selection and two theme functions: <em>i18n_flags</em> and <em>i18n_links</em>.') . '</li>';
244
      $output .= '</ul>';
245
      $output .= '<p>' . t('This is the base module for several others adding different features:') . '</p>';
246
      $output .= '<ul>';
247
      $output .= '<li>' . t('Multilingual menu items.') . '</li>';
248
      $output .= '<li>' . t('Multilingual taxonomy adds a language field for taxonomy vocabularies and terms.') . '</li>';
249
      $output .= '</ul>';
250
      $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@i18n">Internationalization module</a>.', array('@i18n' => 'http://drupal.org/node/133977')) . '</p>';
251
      return $output;
252
253
    case 'admin/config/language/i18n':
254
      $output = '<ul>';
255
      $output .= '<li>' . t('To enable multilingual support for specific content types go to <a href="@configure_content_types">configure content types</a>.', array('@configure_content_types' => url('admin/structure/types'))) . '</li>';
256
      $output .= '</ul>';
257
      return $output;
258
  }
259
}
260
261
/**
262
 * Implements hook_menu().
263
 */
264
function i18n_menu() {
265
  $items['admin/config/regional/i18n'] = array(
266
    'title' => 'Multilingual settings',
267
    'description' => 'Configure extended options for multilingual content and translations.',
268
    'page callback' => 'drupal_get_form',
269
    'page arguments' => array('variable_module_form', 'i18n'),
270
    'access arguments' => array('administer site configuration'),
271
    'weight' => 10,
272
  );
273
  $items['admin/config/regional/i18n/configure'] = array(
274
    'title' => 'Multilingual system',
275
    'description' => 'Configure extended options for multilingual content and translations.',
276
    'type' => MENU_DEFAULT_LOCAL_TASK,
277
  );
278
  module_load_include('pages.inc', 'i18n');
279
  $items += i18n_page_menu_items();
280
  return $items;
281
}
282
283
/**
284
 * Simple i18n API
285
 */
286
287
/**
288
 * Switch select Mode on off if enabled
289
 *
290
 * Usage for disabling content selection for a while then return to previous state
291
 *
292
 *   // Disable selection, but store previous mode
293
 *   $previous = i18n_select(FALSE);
294
 *
295
 *   // Other code to be run without content selection here
296
 *   ..........................
297
 *
298
 *   // Return to previous mode
299
 *   i18n_select($previous);
300
 *
301
 * @param $value
302
 *   Optional, enable/disable selection: TRUE/FALSE
303
 * @return boolean
304
 *   Previous selection mode (TRUE/FALSE)
305
 */
306
function i18n_select($value = NULL) {
307
  static $mode = FALSE;
308
309
  if (isset($value)) {
310
    $previous = $mode;
311
    $mode = $value;
312
    return $previous;
313
  }
314
  else {
315
    return $mode;
316
  }
317
}
318
319
/**
320
 * Get language properties.
321
 *
322
 * @param $code
323
 *   Language code.
324
 * @param $property
325
 *   It may be 'name', 'native', 'ltr'...
326
 */
327
function i18n_language_property($code, $property) {
328
  $languages = language_list();
329
  return isset($languages[$code]->$property) ? $languages[$code]->$property : NULL;
330
}
331
332
/**
333
 * Implements hook_preprocess_html().
334
 */
335
function i18n_preprocess_html(&$variables) {
336
  global $language;
337
  $variables['classes_array'][] = 'i18n-' . $language->language;
338
}
339
340
/**
341
 * Translate or update user defined string. Entry point for i18n_string API if enabled.
342
 *
343
 * This function is from i18n_string sub module and is subject to be moved back.
344
 *
345
 * @param $name
346
 *   Textgroup and context glued with ':'.
347
 * @param $default
348
 *   String in default language. Default language may or may not be English.
349
 * @param $options
350
 *   An associative array of additional options, with the following keys:
351
 *   - 'langcode' (defaults to the current language) The language code to translate to a language other than what is used to display the page.
352
 *   - 'filter' Filtering callback to apply to the translated string only
353
 *   - 'format' Input format to apply to the translated string only
354
 *   - 'callback' Callback to apply to the result (both to translated or untranslated string
355
 *   - 'update' (defaults to FALSE) Whether to update source table
356
 *   - 'translate' (defaults to TRUE) Whether to return a translation
357
 *
358
 * @return $string
359
 *   Translated string, $string if not found
360
 */
361
function i18n_string($name, $string, $options = array()) {
362
  $options += array('translate' => TRUE, 'update' => FALSE);
363
  if ($options['update']) {
364
    $result = function_exists('i18n_string_update') ? i18n_string_update($name, $string, $options) : FALSE;
365
  }
366
  if ($options['translate']) {
367
    $result = function_exists('i18n_string_translate') ? i18n_string_translate($name, $string, $options) : $string;
368
  }
369
  return $result;
370
}
371
372
/**
373
 * Get object wrapper.
374
 *
375
 * Create an object wrapper or retrieve it from the static cache if
376
 * a wrapper for the same object was created before.
377
 *
378
 * @see i18n_object_info()
379
 *
380
 * @param $type
381
 *   The object type.
382
 */
383
function i18n_object($type, $object) {
384
  $key = i18n_object_key($type, $object);
385
  return i18n_get_object($type, $key, $object);
386
}
387
388
/**
389
 * Get object wrapper by object key.
390
 *
391
 * @param $type
392
 *   The object type to load e.g. node_type, menu, taxonomy_term.
393
 * @param $key
394
 *   The object key, can be an scalar or an array.
395
 * @param $object
396
 *   Optional Drupal object or array. It will be autoloaded using the key if not present.
397
 *
398
 * @return
399
 *   A fully-populated object wrapper.
400
 */
401
function i18n_get_object($type, $key, $object = NULL) {
402
  $cache = &drupal_static(__FUNCTION__);
403
  $index = is_array($key) ? implode(':', $key) : $key;
404
  if (!isset($cache[$type][$index])) {
405
    $class = i18n_object_info($type, 'class', 'i18n_object_wrapper');
406 e4c061ad Assos Assos
    $object_wrapper = new $class($type, $key, $object);
407
    // Do not cache object with empty index.
408
    if (!empty($index)) {
409
      $cache[$type][$index] = $object_wrapper;
410
    }
411
  }
412
  else {
413
    $object_wrapper = $cache[$type][$index];
414 85ad3d82 Assos Assos
  }
415 e4c061ad Assos Assos
416
  return $object_wrapper;
417 85ad3d82 Assos Assos
}
418
419
/**
420
 * Get object language code
421
 *
422
 * @param $object
423
 *   Object or array having language field or plain language field
424
 * @param $default
425
 *   What value to return if the object doesn't have a valid language
426
 */
427
function i18n_object_langcode($object, $default = FALSE, $field = 'language') {
428
  $value = i18n_object_field($object, $field, $default);
429
  return $value && $value != LANGUAGE_NONE ? $value : $default;
430
}
431
432
/**
433
 * Get translation information for objects
434
 */
435
function i18n_object_info($type = NULL, $property = NULL, $default = NULL) {
436
  $info = &drupal_static(__FUNCTION__);
437
  if (!$info) {
438
    $info = module_invoke_all('i18n_object_info');
439
    drupal_alter('i18n_object_info', $info);
440
  }
441
  if ($property) {
442
    return isset($info[$type][$property]) ? $info[$type][$property] : $default;
443
  }
444
  elseif ($type) {
445
    return isset($info[$type]) ? $info[$type] : array();
446
  }
447
  else {
448
    return $info;
449
  }
450
}
451
452
/**
453
 * Get field value from object/array
454
 */
455
function i18n_object_field($object, $field, $default = NULL) {
456
  if (is_array($field)) {
457
    // We can handle a list of fields too. This is useful for multiple keys (like blocks)
458
    foreach ($field as $key => $name) {
459
      $values[$key] = i18n_object_field($object, $name);
460
    }
461
    return $values;
462
  }
463
  elseif (strpos($field, '.')) {
464
    // Access nested properties with the form 'name1.name2..', will map to $object->name1->name2...
465
    $names = explode('.', $field);
466
    $current = array_shift($names);
467
    if ($nested = i18n_object_field($object, $current)) {
468
      return i18n_object_field($nested, implode('.', $names), $default);
469
    }
470
    else {
471
      return $default;
472
    }
473
  }
474
  elseif (is_object($object)) {
475
    return isset($object->$field) ? $object->$field : $default;
476
  }
477
  elseif (is_array($object)) {
478
    return isset($object[$field]) ? $object[$field] : $default;
479
  }
480
  else {
481
    return $default;
482
  }
483
}
484
485
/**
486
 * Get key value from object/array
487
 */
488
function i18n_object_key($type, $object, $default = NULL) {
489
  if ($field = i18n_object_info($type, 'key')) {
490
    return i18n_object_field($object, $field, $default);
491
  }
492
  else {
493
    return $default;
494
  }
495
}
496
497
/**
498
 * Menu access callback for mixed translation tab
499
 */
500
function i18n_object_translate_access($type, $object) {
501
  return i18n_object($type, $object)->get_translate_access();
502
}
503
504
/**
505
 * Get translations for path.
506
 *
507
 * @param $path
508
 *   Path to get translations for or '<front>' for front page.
509 6331c987 Assos Assos
 * @param $check_access
510
 *   Whether to check access to paths, defaults to TRUE
511 85ad3d82 Assos Assos
 */
512 6331c987 Assos Assos
function i18n_get_path_translations($path, $check_access = TRUE) {
513 85ad3d82 Assos Assos
  $translations = &drupal_static(__FUNCTION__);
514 6331c987 Assos Assos
515 85ad3d82 Assos Assos
  if (!isset($translations[$path])) {
516
    $translations[$path] = array();
517
    foreach (module_implements('i18n_translate_path') as $module) {
518
      $translated = call_user_func($module . '_i18n_translate_path', $path);
519
      // Add into the array, if two modules returning a translation first takes precedence.
520
      if ($translated) {
521
        $translations[$path] += $translated;
522
      }
523
    }
524 6331c987 Assos Assos
    // Add access information if not there.
525
    foreach ($translations[$path] as $langcode => &$info) {
526
      if (!isset($info['access'])) {
527
        $item = menu_get_item($info['href']);
528
        // If no menu item, it may be an external URL, we allow access.
529
        $info['access'] = $item ? !empty($item['access']) : TRUE;
530
      }
531
    }
532 85ad3d82 Assos Assos
    // Chance for altering the results.
533
    drupal_alter('i18n_translate_path', $translations[$path], $path);
534
  }
535 6331c987 Assos Assos
536
  if ($check_access) {
537
    return array_filter($translations[$path], '_i18n_get_path_translations_access');
538
  }
539
  else {
540
    return $translations[$path];
541
  }
542
}
543
544
/**
545
 * Helper function to check access to path translation.
546
 */
547
function _i18n_get_path_translations_access($path) {
548
  return $path['access'];
549 85ad3d82 Assos Assos
}
550
551
/**
552
 * Implements hook_language_switch_links_alter().
553
 *
554
 * Replaces links with pointers to translated versions of the content.
555
 */
556
function i18n_language_switch_links_alter(array &$links, $type, $path) {
557
  // For the front page we have nothing to add to Drupal core links.
558
  if ($path != '<front>' && ($translations = i18n_get_path_translations($path))) {
559
    foreach ($translations as $langcode => $translation) {
560
      if (isset($links[$langcode])) {
561
        $links[$langcode]['href'] = $translation['href'];
562
        if (!empty($translation['title'])) {
563
          $links[$langcode]['attributes']['title'] = $translation['title'];
564
        }
565
      }
566
    }
567
  }
568
}
569
570
/**
571
 * Build translation link
572
 */
573
function i18n_translation_link($path, $langcode, $link = array()) {
574
  $language = i18n_language_object($langcode);
575
  $link += array(
576
    'href' => $path,
577
    'title' => $language->native,
578
    'language' => $language,
579
    'i18n_translation' => TRUE,
580
  );
581
  $link['attributes']['class'] = array('language-link');
582
  // @todo Fix languageicons weight, but until that
583
  if (function_exists('languageicons_link_add')) {
584
    languageicons_link_add($link);
585
  }
586
  return $link;
587
}
588
589
/**
590
 * Implements hook_form_FORM_ID_alter().
591
 */
592
function i18n_form_block_admin_display_form_alter(&$form, &$form_state) {
593
  $form['#submit'][] = 'i18n_form_block_admin_display_form_submit';
594
}
595
596
/**
597
 * Display a help message when enabling the language switcher block.
598
 */
599
function i18n_form_block_admin_display_form_submit($form, &$form_state) {
600
  foreach ($form_state['values']['blocks'] as $key => $block) {
601
    $previous = $form['blocks'][$key]['region']['#default_value'];
602
    if (empty($previous) && $block['region'] != -1 && $block['module'] == 'locale') {
603
      $message = t('The language switcher will appear only after configuring <a href="!url">language detection</a>. You need to enable at least one method that alters URLs like <em>URL</em> or <em>Session</em>.', array('!url' => url('admin/config/regional/language/configure')));
604
      drupal_set_message($message, 'warning', FALSE);
605
      break;
606
    }
607
  }
608
}
609
610
/**
611
 * Normal path should be checked with menu item's language to avoid
612
 * troubles when a node and it's translation has the same url alias.
613
 */
614
function i18n_prepare_normal_path($link_path, $language) {
615
  $normal_path = drupal_get_normal_path($link_path, $language);
616
  if ($link_path != $normal_path) {
617
    drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $link_path, '%normal_path' => $normal_path)));
618
  }
619
  return $normal_path;
620
}
621
622
/**
623
 * Checks if an entity translation is enabled for the given entity type.
624
 * @param $entity_type
625
 */
626
function i18n_entity_translation_enabled($entity_type) {
627
  $cache = &drupal_static(__FUNCTION__);
628
  if (!isset($cache[$entity_type])) {
629
    // Check if the entity_translation module exists and if so if the given
630
    // entity type is handled.
631
    $cache[$entity_type] = module_exists('entity_translation') && entity_translation_enabled($entity_type);
632
  }
633
  return $cache[$entity_type];
634
}
635
636
/**
637
 * Implements hook_modules_enabled().
638
 */
639
function i18n_modules_enabled($modules) {
640
  drupal_static_reset('i18n_object_info');
641
}