translation.module

  1. 7.x drupal-7.x/modules/translation/translation.module
  2. 6.x drupal-6.x/modules/translation/translation.module

Manages content translations.

Translations are managed in sets of posts, which represent the same information in different languages. Only content types for which the administrator has explicitly enabled translations could have translations associated. Translations are managed in sets with exactly one source post per set. The source post is used to translate to different languages, so if the source post is significantly updated, the editor can decide to mark all translations outdated.

The node table stores the values used by this module:

  • tnid: Integer for the translation set ID, which equals the node ID of the source post.
  • translate: Integer flag, either indicating that the translation is up to date (0) or needs to be updated (1).

File

drupal-7.x/modules/translation/translation.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Manages content translations.
  5. *
  6. * Translations are managed in sets of posts, which represent the same
  7. * information in different languages. Only content types for which the
  8. * administrator has explicitly enabled translations could have translations
  9. * associated. Translations are managed in sets with exactly one source post
  10. * per set. The source post is used to translate to different languages, so if
  11. * the source post is significantly updated, the editor can decide to mark all
  12. * translations outdated.
  13. *
  14. * The node table stores the values used by this module:
  15. * - tnid: Integer for the translation set ID, which equals the node ID of the
  16. * source post.
  17. * - translate: Integer flag, either indicating that the translation is up to
  18. * date (0) or needs to be updated (1).
  19. */
  20. /**
  21. * Identifies a content type which has translation support enabled.
  22. */
  23. define('TRANSLATION_ENABLED', 2);
  24. /**
  25. * Implements hook_help().
  26. */
  27. function translation_help($path, $arg) {
  28. switch ($path) {
  29. case 'admin/help#translation':
  30. $output = '';
  31. $output .= '<h3>' . t('About') . '</h3>';
  32. $output .= '<p>' . t('The Content translation module allows content to be translated into different languages. Working with the <a href="@locale">Locale module</a> (which manages enabled languages and provides translation for the site interface), the Content translation module is key to creating and maintaining translated site content. For more information, see the online handbook entry for <a href="@translation">Content translation module</a>.', array('@locale' => url('admin/help/locale'), '@translation' => 'http://drupal.org/documentation/modules/translation/')) . '</p>';
  33. $output .= '<h3>' . t('Uses') . '</h3>';
  34. $output .= '<dl>';
  35. $output .= '<dt>' . t('Configuring content types for translation') . '</dt>';
  36. $output .= '<dd>' . t('To configure a particular content type for translation, visit the <a href="@content-types">Content types</a> page, and click the <em>edit</em> link for the content type. In the <em>Publishing options</em> section, select <em>Enabled, with translation</em> under <em>Multilingual support</em>.', array('@content-types' => url('admin/structure/types'))) . '</dd>';
  37. $output .= '<dt>' . t('Assigning a language to content') . '</dt>';
  38. $output .= '<dd>' . t('Use the <em>Language</em> drop down to select the appropriate language when creating or editing content.') . '</dd>';
  39. $output .= '<dt>' . t('Translating content') . '</dt>';
  40. $output .= '<dd>' . t('Users with the <em>translate content</em> permission can translate content, if the content type has been configured to allow translations. To translate content, select the <em>Translation</em> tab when viewing the content, select the language for which you wish to provide content, and then enter the content.') . '</dd>';
  41. $output .= '<dt>' . t('Maintaining translations') . '</dt>';
  42. $output .= '<dd>' . t('If editing content in one language requires that translated versions also be updated to reflect the change, use the <em>Flag translations as outdated</em> check box to mark the translations as outdated and in need of revision. Individual translations may also be marked for revision by selecting the <em>This translation needs to be updated</em> check box on the translation editing form.') . '</dd>';
  43. $output .= '</dl>';
  44. return $output;
  45. case 'node/%/translate':
  46. $output = '<p>' . t('Translations of a piece of content are managed with translation sets. Each translation set has one source post and any number of translations in any of the <a href="!languages">enabled languages</a>. All translations are tracked to be up to date or outdated based on whether the source post was modified significantly.', array('!languages' => url('admin/config/regional/language'))) . '</p>';
  47. return $output;
  48. }
  49. }
  50. /**
  51. * Implements hook_menu().
  52. */
  53. function translation_menu() {
  54. $items = array();
  55. $items['node/%node/translate'] = array(
  56. 'title' => 'Translate',
  57. 'page callback' => 'translation_node_overview',
  58. 'page arguments' => array(1),
  59. 'access callback' => '_translation_tab_access',
  60. 'access arguments' => array(1),
  61. 'type' => MENU_LOCAL_TASK,
  62. 'weight' => 2,
  63. 'file' => 'translation.pages.inc',
  64. );
  65. return $items;
  66. }
  67. /**
  68. * Access callback: Checks that the user has permission to 'translate content'.
  69. *
  70. * Only displays the translation tab for nodes that are not language-neutral
  71. * of types that have translation enabled.
  72. *
  73. * @param $node
  74. * A node object.
  75. *
  76. * @return
  77. * TRUE if the translation tab should be displayed, FALSE otherwise.
  78. *
  79. * @see translation_menu()
  80. */
  81. function _translation_tab_access($node) {
  82. if (entity_language('node', $node) != LANGUAGE_NONE && translation_supported_type($node->type) && node_access('view', $node)) {
  83. return user_access('translate content');
  84. }
  85. return FALSE;
  86. }
  87. /**
  88. * Implements hook_admin_paths().
  89. */
  90. function translation_admin_paths() {
  91. if (variable_get('node_admin_theme')) {
  92. $paths = array(
  93. 'node/*/translate' => TRUE,
  94. );
  95. return $paths;
  96. }
  97. }
  98. /**
  99. * Implements hook_permission().
  100. */
  101. function translation_permission() {
  102. return array(
  103. 'translate content' => array(
  104. 'title' => t('Translate content'),
  105. ),
  106. );
  107. }
  108. /**
  109. * Implements hook_form_FORM_ID_alter() for node_type_form().
  110. */
  111. function translation_form_node_type_form_alter(&$form, &$form_state) {
  112. // Add translation option to content type form.
  113. $form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
  114. // Description based on text from locale.module.
  115. $form['workflow']['language_content_type']['#description'] = t('Enable multilingual support for this content type. If enabled, a language selection field will be added to the editing form, allowing you to select from one of the <a href="!languages">enabled languages</a>. You can also turn on translation for this content type, which lets you have content translated to any of the installed languages. If disabled, new posts are saved with the default language. Existing content will not be affected by changing this option.', array('!languages' => url('admin/config/regional/language')));
  116. }
  117. /**
  118. * Implements hook_form_BASE_FORM_ID_alter() for node_form().
  119. *
  120. * Alters language fields on node edit forms when a translation is about to be
  121. * created.
  122. *
  123. * @see node_form()
  124. */
  125. function translation_form_node_form_alter(&$form, &$form_state) {
  126. if (translation_supported_type($form['#node']->type)) {
  127. $node = $form['#node'];
  128. $languages = language_list('enabled');
  129. $disabled_languages = isset($languages[0]) ? $languages[0] : FALSE;
  130. $translator_widget = $disabled_languages && user_access('translate content');
  131. $groups = array(t('Disabled'), t('Enabled'));
  132. // Allow translators to enter content in disabled languages. Translators
  133. // might need to distinguish between enabled and disabled languages, hence
  134. // we divide them in two option groups.
  135. if ($translator_widget) {
  136. $options = array($groups[1] => array(LANGUAGE_NONE => t('Language neutral')));
  137. $language_list = locale_language_list('name', TRUE);
  138. foreach (array(1, 0) as $status) {
  139. $group = $groups[$status];
  140. foreach ($languages[$status] as $langcode => $language) {
  141. $options[$group][$langcode] = $language_list[$langcode];
  142. }
  143. }
  144. $form['language']['#options'] = $options;
  145. }
  146. if (!empty($node->translation_source)) {
  147. // We are creating a translation. Add values and lock language field.
  148. $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
  149. $form['language']['#disabled'] = TRUE;
  150. }
  151. elseif (!empty($node->nid) && !empty($node->tnid)) {
  152. // Disable languages for existing translations, so it is not possible to switch this
  153. // node to some language which is already in the translation set. Also remove the
  154. // language neutral option.
  155. unset($form['language']['#options'][LANGUAGE_NONE]);
  156. foreach (translation_node_get_translations($node->tnid) as $langcode => $translation) {
  157. if ($translation->nid != $node->nid) {
  158. if ($translator_widget) {
  159. $group = $groups[(int)!isset($disabled_languages[$langcode])];
  160. unset($form['language']['#options'][$group][$langcode]);
  161. }
  162. else {
  163. unset($form['language']['#options'][$langcode]);
  164. }
  165. }
  166. }
  167. // Add translation values and workflow options.
  168. $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
  169. $form['translation'] = array(
  170. '#type' => 'fieldset',
  171. '#title' => t('Translation settings'),
  172. '#access' => user_access('translate content'),
  173. '#collapsible' => TRUE,
  174. '#collapsed' => !$node->translate,
  175. '#tree' => TRUE,
  176. '#weight' => 30,
  177. );
  178. if ($node->tnid == $node->nid) {
  179. // This is the source node of the translation
  180. $form['translation']['retranslate'] = array(
  181. '#type' => 'checkbox',
  182. '#title' => t('Flag translations as outdated'),
  183. '#default_value' => 0,
  184. '#description' => t('If you made a significant change, which means translations should be updated, you can flag all translations of this post as outdated. This will not change any other property of those posts, like whether they are published or not.'),
  185. );
  186. $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
  187. }
  188. else {
  189. $form['translation']['status'] = array(
  190. '#type' => 'checkbox',
  191. '#title' => t('This translation needs to be updated'),
  192. '#default_value' => $node->translate,
  193. '#description' => t('When this option is checked, this translation needs to be updated because the source post has changed. Uncheck when the translation is up to date again.'),
  194. );
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Implements hook_node_view().
  201. *
  202. * Displays translation links with language names if this node is part of a
  203. * translation set. If no language provider is enabled, "fall back" to simple
  204. * links built through the result of translation_node_get_translations().
  205. */
  206. function translation_node_view($node, $view_mode) {
  207. // If the site has no translations or is not multilingual we have no content
  208. // translation links to display.
  209. if (isset($node->tnid) && drupal_multilingual() && $translations = translation_node_get_translations($node->tnid)) {
  210. $languages = language_list('enabled');
  211. $languages = $languages[1];
  212. // There might be a language provider enabled defining custom language
  213. // switch links which need to be taken into account while generating the
  214. // content translation links. As custom language switch links are available
  215. // only for configurable language types and interface language is the only
  216. // configurable language type in core, we use it as default. Contributed
  217. // modules can change this behavior by setting the system variable below.
  218. $type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  219. $custom_links = language_negotiation_get_switch_links($type, "node/$node->nid");
  220. $links = array();
  221. foreach ($translations as $langcode => $translation) {
  222. // Do not show links to the same node, to unpublished translations or to
  223. // translations in disabled languages.
  224. if ($translation->status && isset($languages[$langcode]) && $langcode != entity_language('node', $node)) {
  225. $language = $languages[$langcode];
  226. $key = "translation_$langcode";
  227. if (isset($custom_links->links[$langcode])) {
  228. $links[$key] = $custom_links->links[$langcode];
  229. }
  230. else {
  231. $links[$key] = array(
  232. 'href' => "node/{$translation->nid}",
  233. 'title' => $language->native,
  234. 'language' => $language,
  235. );
  236. }
  237. // Custom switch links are more generic than content translation links,
  238. // hence we override existing attributes with the ones below.
  239. $links[$key] += array('attributes' => array());
  240. $attributes = array(
  241. 'title' => $translation->title,
  242. 'class' => array('translation-link'),
  243. );
  244. $links[$key]['attributes'] = $attributes + $links[$key]['attributes'];
  245. }
  246. }
  247. $node->content['links']['translation'] = array(
  248. '#theme' => 'links__node__translation',
  249. '#links' => $links,
  250. '#attributes' => array('class' => array('links', 'inline')),
  251. );
  252. }
  253. }
  254. /**
  255. * Implements hook_node_prepare().
  256. */
  257. function translation_node_prepare($node) {
  258. // Only act if we are dealing with a content type supporting translations.
  259. if (translation_supported_type($node->type) &&
  260. // And it's a new node.
  261. empty($node->nid) &&
  262. // And the user has permission to translate content.
  263. user_access('translate content') &&
  264. // And the $_GET variables are set properly.
  265. isset($_GET['translation']) &&
  266. isset($_GET['target']) &&
  267. is_numeric($_GET['translation'])) {
  268. $source_node = node_load($_GET['translation']);
  269. if (empty($source_node) || !node_access('view', $source_node)) {
  270. // Source node not found or no access to view. We should not check
  271. // for edit access, since the translator might not have permissions
  272. // to edit the source node but should still be able to translate.
  273. return;
  274. }
  275. $language_list = language_list();
  276. $langcode = $_GET['target'];
  277. if (!isset($language_list[$langcode]) || ($source_node->language == $langcode)) {
  278. // If not supported language, or same language as source node, break.
  279. return;
  280. }
  281. // Ensure we don't have an existing translation in this language.
  282. if (!empty($source_node->tnid)) {
  283. $translations = translation_node_get_translations($source_node->tnid);
  284. if (isset($translations[$langcode])) {
  285. drupal_set_message(t('A translation of %title in %language already exists, a new %type will be created instead of a translation.', array('%title' => $source_node->title, '%language' => $language_list[$langcode]->name, '%type' => $node->type)), 'error');
  286. return;
  287. }
  288. }
  289. // Populate fields based on source node.
  290. $node->language = $langcode;
  291. $node->translation_source = $source_node;
  292. $node->title = $source_node->title;
  293. // Add field translations and let other modules module add custom translated
  294. // fields.
  295. field_attach_prepare_translation('node', $node, $langcode, $source_node, $source_node->language);
  296. }
  297. }
  298. /**
  299. * Implements hook_node_insert().
  300. */
  301. function translation_node_insert($node) {
  302. // Only act if we are dealing with a content type supporting translations.
  303. if (translation_supported_type($node->type)) {
  304. if (!empty($node->translation_source)) {
  305. if ($node->translation_source->tnid) {
  306. // Add node to existing translation set.
  307. $tnid = $node->translation_source->tnid;
  308. }
  309. else {
  310. // Create new translation set, using nid from the source node.
  311. $tnid = $node->translation_source->nid;
  312. db_update('node')
  313. ->fields(array(
  314. 'tnid' => $tnid,
  315. 'translate' => 0,
  316. ))
  317. ->condition('nid', $node->translation_source->nid)
  318. ->execute();
  319. }
  320. db_update('node')
  321. ->fields(array(
  322. 'tnid' => $tnid,
  323. 'translate' => 0,
  324. ))
  325. ->condition('nid', $node->nid)
  326. ->execute();
  327. // Save tnid to avoid loss in case of resave.
  328. $node->tnid = $tnid;
  329. }
  330. }
  331. }
  332. /**
  333. * Implements hook_node_update().
  334. */
  335. function translation_node_update($node) {
  336. // Only act if we are dealing with a content type supporting translations.
  337. if (translation_supported_type($node->type)) {
  338. $langcode = entity_language('node', $node);
  339. if (isset($node->translation) && $node->translation && !empty($langcode) && $node->tnid) {
  340. // Update translation information.
  341. db_update('node')
  342. ->fields(array(
  343. 'tnid' => $node->tnid,
  344. 'translate' => $node->translation['status'],
  345. ))
  346. ->condition('nid', $node->nid)
  347. ->execute();
  348. if (!empty($node->translation['retranslate'])) {
  349. // This is the source node, asking to mark all translations outdated.
  350. db_update('node')
  351. ->fields(array('translate' => 1))
  352. ->condition('nid', $node->nid, '<>')
  353. ->condition('tnid', $node->tnid)
  354. ->execute();
  355. }
  356. }
  357. }
  358. }
  359. /**
  360. * Implements hook_node_validate().
  361. *
  362. * Ensures that duplicate translations can't be created for the same source.
  363. */
  364. function translation_node_validate($node, $form) {
  365. // Only act on translatable nodes with a tnid or translation_source.
  366. if (translation_supported_type($node->type) && (!empty($node->tnid) || !empty($form['#node']->translation_source->nid))) {
  367. $tnid = !empty($node->tnid) ? $node->tnid : $form['#node']->translation_source->nid;
  368. $translations = translation_node_get_translations($tnid);
  369. $langcode = entity_language('node', $node);
  370. if (isset($translations[$langcode]) && $translations[$langcode]->nid != $node->nid ) {
  371. form_set_error('language', t('There is already a translation in this language.'));
  372. }
  373. }
  374. }
  375. /**
  376. * Implements hook_node_delete().
  377. */
  378. function translation_node_delete($node) {
  379. // Only act if we are dealing with a content type supporting translations.
  380. if (translation_supported_type($node->type)) {
  381. translation_remove_from_set($node);
  382. }
  383. }
  384. /**
  385. * Removes a node from its translation set and updates accordingly.
  386. *
  387. * @param $node
  388. * A node object.
  389. */
  390. function translation_remove_from_set($node) {
  391. if (isset($node->tnid)) {
  392. $query = db_update('node')
  393. ->fields(array(
  394. 'tnid' => 0,
  395. 'translate' => 0,
  396. ));
  397. if (db_query('SELECT COUNT(*) FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchField() == 1) {
  398. // There is only one node left in the set: remove the set altogether.
  399. $query
  400. ->condition('tnid', $node->tnid)
  401. ->execute();
  402. }
  403. else {
  404. $query
  405. ->condition('nid', $node->nid)
  406. ->execute();
  407. // If the node being removed was the source of the translation set,
  408. // we pick a new source - preferably one that is up to date.
  409. if ($node->tnid == $node->nid) {
  410. $new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField();
  411. db_update('node')
  412. ->fields(array('tnid' => $new_tnid))
  413. ->condition('tnid', $node->tnid)
  414. ->execute();
  415. }
  416. }
  417. }
  418. }
  419. /**
  420. * Gets all nodes in a given translation set.
  421. *
  422. * @param $tnid
  423. * The translation source nid of the translation set, the identifier of the
  424. * node used to derive all translations in the set.
  425. *
  426. * @return
  427. * Array of partial node objects (nid, title, language) representing all
  428. * nodes in the translation set, in effect all translations of node $tnid,
  429. * including node $tnid itself. Because these are partial nodes, you need to
  430. * node_load() the full node, if you need more properties. The array is
  431. * indexed by language code.
  432. */
  433. function translation_node_get_translations($tnid) {
  434. if (is_numeric($tnid) && $tnid) {
  435. $translations = &drupal_static(__FUNCTION__, array());
  436. if (!isset($translations[$tnid])) {
  437. $translations[$tnid] = array();
  438. $result = db_select('node', 'n')
  439. ->fields('n', array('nid', 'type', 'uid', 'status', 'title', 'language'))
  440. ->condition('n.tnid', $tnid)
  441. ->addTag('node_access')
  442. ->execute();
  443. foreach ($result as $node) {
  444. $langcode = entity_language('node', $node);
  445. $translations[$tnid][$langcode] = $node;
  446. }
  447. }
  448. return $translations[$tnid];
  449. }
  450. }
  451. /**
  452. * Returns whether the given content type has support for translations.
  453. *
  454. * @return
  455. * TRUE if translation is supported, and FALSE if not.
  456. */
  457. function translation_supported_type($type) {
  458. return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
  459. }
  460. /**
  461. * Returns the paths of all translations of a node, based on its Drupal path.
  462. *
  463. * @param $path
  464. * A Drupal path, for example node/432.
  465. *
  466. * @return
  467. * An array of paths of translations of the node accessible to the current
  468. * user, keyed with language codes.
  469. */
  470. function translation_path_get_translations($path) {
  471. $paths = array();
  472. // Check for a node related path, and for its translations.
  473. if ((preg_match("!^node/(\d+)(/.+|)$!", $path, $matches)) && ($node = node_load((int) $matches[1])) && !empty($node->tnid)) {
  474. foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
  475. $paths[$language] = 'node/' . $translation_node->nid . $matches[2];
  476. }
  477. }
  478. return $paths;
  479. }
  480. /**
  481. * Implements hook_language_switch_links_alter().
  482. *
  483. * Replaces links with pointers to translated versions of the content.
  484. */
  485. function translation_language_switch_links_alter(array &$links, $type, $path) {
  486. $language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  487. if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
  488. $node = node_load((int) $matches[1]);
  489. if (empty($node->tnid)) {
  490. // If the node cannot be found nothing needs to be done. If it does not
  491. // have translations it might be a language neutral node, in which case we
  492. // must leave the language switch links unaltered. This is true also for
  493. // nodes not having translation support enabled.
  494. if (empty($node) || entity_language('node', $node) == LANGUAGE_NONE || !translation_supported_type($node->type)) {
  495. return;
  496. }
  497. $langcode = entity_language('node', $node);
  498. $translations = array($langcode => $node);
  499. }
  500. else {
  501. $translations = translation_node_get_translations($node->tnid);
  502. }
  503. foreach ($links as $langcode => $link) {
  504. if (isset($translations[$langcode]) && $translations[$langcode]->status) {
  505. // Translation in a different node.
  506. $links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
  507. }
  508. else {
  509. // No translation in this language, or no permission to view.
  510. unset($links[$langcode]['href']);
  511. $links[$langcode]['attributes']['class'][] = 'locale-untranslated';
  512. }
  513. }
  514. }
  515. }