translation.module 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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', $tnid)
  318. ->execute();
  319. // Flush the (untranslated) source node from the load cache.
  320. entity_get_controller('node')->resetCache(array($tnid));
  321. }
  322. db_update('node')
  323. ->fields(array(
  324. 'tnid' => $tnid,
  325. 'translate' => 0,
  326. ))
  327. ->condition('nid', $node->nid)
  328. ->execute();
  329. // Save tnid to avoid loss in case of resave.
  330. $node->tnid = $tnid;
  331. }
  332. }
  333. }
  334. /**
  335. * Implements hook_node_update().
  336. */
  337. function translation_node_update($node) {
  338. // Only act if we are dealing with a content type supporting translations.
  339. if (translation_supported_type($node->type)) {
  340. $langcode = entity_language('node', $node);
  341. if (isset($node->translation) && $node->translation && !empty($langcode) && $node->tnid) {
  342. // Update translation information.
  343. db_update('node')
  344. ->fields(array(
  345. 'tnid' => $node->tnid,
  346. 'translate' => $node->translation['status'],
  347. ))
  348. ->condition('nid', $node->nid)
  349. ->execute();
  350. if (!empty($node->translation['retranslate'])) {
  351. // This is the source node, asking to mark all translations outdated.
  352. $translations = db_select('node', 'n')
  353. ->fields('n', array('nid'))
  354. ->condition('nid', $node->nid, '<>')
  355. ->condition('tnid', $node->tnid)
  356. ->execute()
  357. ->fetchCol();
  358. db_update('node')
  359. ->fields(array('translate' => 1))
  360. ->condition('nid', $translations, 'IN')
  361. ->execute();
  362. // Flush the modified translation nodes from the load cache.
  363. entity_get_controller('node')->resetCache($translations);
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * Implements hook_node_validate().
  370. *
  371. * Ensures that duplicate translations can't be created for the same source.
  372. */
  373. function translation_node_validate($node, $form) {
  374. // Only act on translatable nodes with a tnid or translation_source.
  375. if (translation_supported_type($node->type) && (!empty($node->tnid) || !empty($form['#node']->translation_source->nid))) {
  376. $tnid = !empty($node->tnid) ? $node->tnid : $form['#node']->translation_source->nid;
  377. $translations = translation_node_get_translations($tnid);
  378. $langcode = entity_language('node', $node);
  379. if (isset($translations[$langcode]) && $translations[$langcode]->nid != $node->nid ) {
  380. form_set_error('language', t('There is already a translation in this language.'));
  381. }
  382. }
  383. }
  384. /**
  385. * Implements hook_node_delete().
  386. */
  387. function translation_node_delete($node) {
  388. // Only act if we are dealing with a content type supporting translations.
  389. if (translation_supported_type($node->type)) {
  390. translation_remove_from_set($node);
  391. }
  392. }
  393. /**
  394. * Removes a node from its translation set and updates accordingly.
  395. *
  396. * @param $node
  397. * A node object.
  398. */
  399. function translation_remove_from_set($node) {
  400. if (isset($node->tnid)) {
  401. $query = db_update('node')
  402. ->fields(array(
  403. 'tnid' => 0,
  404. 'translate' => 0,
  405. ));
  406. // Determine which nodes to apply the update to.
  407. $set_nids = db_query('SELECT nid FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchCol();
  408. if (count($set_nids) == 1) {
  409. // There is only one node left in the set: remove the set altogether.
  410. $query
  411. ->condition('tnid', $node->tnid)
  412. ->execute();
  413. $flush_set = TRUE;
  414. }
  415. else {
  416. $query
  417. ->condition('nid', $node->nid)
  418. ->execute();
  419. // If the node being removed was the source of the translation set,
  420. // we pick a new source - preferably one that is up to date.
  421. if ($node->tnid == $node->nid) {
  422. $new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField();
  423. db_update('node')
  424. ->fields(array('tnid' => $new_tnid))
  425. ->condition('tnid', $node->tnid)
  426. ->execute();
  427. $flush_set = TRUE;
  428. }
  429. }
  430. // Flush the modified nodes from the load cache.
  431. $nids = !empty($flush_set) ? $set_nids : array($node->nid);
  432. entity_get_controller('node')->resetCache($nids);
  433. }
  434. }
  435. /**
  436. * Gets all nodes in a given translation set.
  437. *
  438. * @param $tnid
  439. * The translation source nid of the translation set, the identifier of the
  440. * node used to derive all translations in the set.
  441. *
  442. * @return
  443. * Array of partial node objects (nid, title, language) representing all
  444. * nodes in the translation set, in effect all translations of node $tnid,
  445. * including node $tnid itself. Because these are partial nodes, you need to
  446. * node_load() the full node, if you need more properties. The array is
  447. * indexed by language code.
  448. */
  449. function translation_node_get_translations($tnid) {
  450. if (is_numeric($tnid) && $tnid) {
  451. $translations = &drupal_static(__FUNCTION__, array());
  452. if (!isset($translations[$tnid])) {
  453. $translations[$tnid] = array();
  454. $result = db_select('node', 'n')
  455. ->fields('n', array('nid', 'type', 'uid', 'status', 'title', 'language'))
  456. ->condition('n.tnid', $tnid)
  457. ->addTag('node_access')
  458. ->execute();
  459. foreach ($result as $node) {
  460. $langcode = entity_language('node', $node);
  461. $translations[$tnid][$langcode] = $node;
  462. }
  463. }
  464. return $translations[$tnid];
  465. }
  466. }
  467. /**
  468. * Returns whether the given content type has support for translations.
  469. *
  470. * @return
  471. * TRUE if translation is supported, and FALSE if not.
  472. */
  473. function translation_supported_type($type) {
  474. return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
  475. }
  476. /**
  477. * Returns the paths of all translations of a node, based on its Drupal path.
  478. *
  479. * @param $path
  480. * A Drupal path, for example node/432.
  481. *
  482. * @return
  483. * An array of paths of translations of the node accessible to the current
  484. * user, keyed with language codes.
  485. */
  486. function translation_path_get_translations($path) {
  487. $paths = array();
  488. // Check for a node related path, and for its translations.
  489. if ((preg_match("!^node/(\d+)(/.+|)$!", $path, $matches)) && ($node = node_load((int) $matches[1])) && !empty($node->tnid)) {
  490. foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
  491. $paths[$language] = 'node/' . $translation_node->nid . $matches[2];
  492. }
  493. }
  494. return $paths;
  495. }
  496. /**
  497. * Implements hook_language_switch_links_alter().
  498. *
  499. * Replaces links with pointers to translated versions of the content.
  500. */
  501. function translation_language_switch_links_alter(array &$links, $type, $path) {
  502. $language_type = variable_get('translation_language_type', LANGUAGE_TYPE_INTERFACE);
  503. if ($type == $language_type && preg_match("!^node/(\d+)(/.+|)!", $path, $matches)) {
  504. $node = node_load((int) $matches[1]);
  505. if (empty($node->tnid)) {
  506. // If the node cannot be found nothing needs to be done. If it does not
  507. // have translations it might be a language neutral node, in which case we
  508. // must leave the language switch links unaltered. This is true also for
  509. // nodes not having translation support enabled.
  510. if (empty($node) || entity_language('node', $node) == LANGUAGE_NONE || !translation_supported_type($node->type)) {
  511. return;
  512. }
  513. $langcode = entity_language('node', $node);
  514. $translations = array($langcode => $node);
  515. }
  516. else {
  517. $translations = translation_node_get_translations($node->tnid);
  518. }
  519. foreach ($links as $langcode => $link) {
  520. if (isset($translations[$langcode]) && $translations[$langcode]->status) {
  521. // Translation in a different node.
  522. $links[$langcode]['href'] = 'node/' . $translations[$langcode]->nid . $matches[2];
  523. }
  524. else {
  525. // No translation in this language, or no permission to view.
  526. unset($links[$langcode]['href']);
  527. $links[$langcode]['attributes']['class'][] = 'locale-untranslated';
  528. }
  529. }
  530. }
  531. }