i18n.module 19 KB

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