translation.module

You are here

  1. 7 modules/translation/translation.module
  2. 6 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 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' is the translation set id, which equals the node id of the source post.
  • 'translate' is a flag, either indicating that the translation is up to date (0) or needs to be updated (1).

Functions

Namesort descending Description
translation_form_alter Implementation of hook_form_alter().
translation_help Implementation of hook_help().
translation_link Implementation of hook_link().
translation_menu Implementation of hook_menu().
translation_nodeapi Implementation of hook_nodeapi().
translation_node_get_translations Get all nodes in a translation set, represented by $tnid.
translation_path_get_translations Return paths of all translations of a node, based on its Drupal path.
translation_perm Implementation of hook_perm().
translation_remove_from_set Remove a node from its translation set (if any) and update the set accordingly.
translation_supported_type Returns whether the given content type has support for translations.
translation_translation_link_alter Implementation of hook_translation_link_alter().
_translation_tab_access Menu access callback.

Constants

Namesort descending Description
TRANSLATION_ENABLED Identifies a content type which has translation support enabled.

File

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 explicitly enabled translations could have translations
  9. * associated. Translations are managed in sets with exactly one source
  10. * post per set. The source post is used to translate to different
  11. * languages, so if the source post is significantly updated, the
  12. * editor can decide to mark all translations outdated.
  13. *
  14. * The node table stores the values used by this module:
  15. * - 'tnid' is the translation set id, which equals the node id
  16. * of the source post.
  17. * - 'translate' is a flag, either indicating that the translation
  18. * is up to 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. * Implementation of hook_help().
  26. */
  27. function translation_help($path, $arg) {
  28. switch ($path) {
  29. case 'admin/help#translation':
  30. $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.', array('@locale' => url('admin/help/locale'))) .'</p>';
  31. $output .= '<p>'. t('Configuring content translation and translation-enabled content types:') .'</p>';
  32. $output .= '<ul><li>'. t('Assign the <em>translate content</em> permission to the appropriate user roles at the <a href="@permissions">Permissions configuration page</a>.', array('@permissions' => url('admin/user/permissions'))) .'</li>';
  33. $output .= '<li>'. t('Add and enable desired languages at the <a href="@languages">Languages configuration page</a>.', array('@languages' => url('admin/settings/language'))) .'</li>';
  34. $output .= '<li>'. t('Determine which <a href="@content-types">content types</a> should support translation features. To enable translation support for a content type, edit the type and at the <em>Multilingual support</em> drop down, select <em>Enabled, with translation</em>. (<em>Multilingual support</em> is located within <em>Workflow settings</em>.) Be sure to save each content type after enabling multilingual support.', array('@content-types' => url('admin/content/types'))) .'</li></ul>';
  35. $output .= '<p>'. t('Working with translation-enabled content types:') .'</p>';
  36. $output .= '<ul><li>'. t('Use the <em>Language</em> drop down to select the appropriate language when creating or editing posts.') .'</li>';
  37. $output .= '<li>'. t('Provide new or edit current translations for existing posts via the <em>Translation</em> tab. Only visible while viewing a post as a user with the <em>translate content</em> permission, this tab allows translations to be added or edited using a specialized editing form that also displays the content being translated.') .'</li>';
  38. $output .= '<li>'. t('Update translations as needed, so that they accurately reflect changes in the content of the original post. The translation status flag provides a simple method for tracking outdated translations. After editing a post, for example, select the <em>Flag translations as outdated</em> check box to mark all of its translations as outdated and in need of revision. Individual translations may be marked for revision by selecting the <em>This translation needs to be updated</em> check box on the translation editing form.') .'</li>';
  39. $output .= '<li>'. t('The <a href="@content-node">Content management administration page</a> displays the language of each post, and also allows filtering by language or translation status.', array('@content-node' => url('admin/content/node'))) .'</li></ul>';
  40. $output .= '<p>'. t('Use the <a href="@blocks">language switcher block</a> provided by locale module to allow users to select a language. If available, both the site interface and site content are presented in the language selected.', array('@blocks' => url('admin/build/block'))) .'</p>';
  41. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@translation">Translation module</a>.', array('@translation' => 'http://drupal.org/handbook/modules/translation/')) .'</p>';
  42. return $output;
  43. case 'node/%/translate':
  44. $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/settings/language'))) .'</p>';
  45. return $output;
  46. }
  47. }
  48. /**
  49. * Implementation of hook_menu().
  50. */
  51. function translation_menu() {
  52. $items = array();
  53. $items['node/%node/translate'] = array(
  54. 'title' => 'Translate',
  55. 'page callback' => 'translation_node_overview',
  56. 'page arguments' => array(1),
  57. 'access callback' => '_translation_tab_access',
  58. 'access arguments' => array(1),
  59. 'type' => MENU_LOCAL_TASK,
  60. 'weight' => 2,
  61. 'file' => 'translation.pages.inc',
  62. );
  63. return $items;
  64. }
  65. /**
  66. * Menu access callback.
  67. *
  68. * Only display translation tab for node types, which have translation enabled
  69. * and where the current node is not language neutral (which should span
  70. * all languages).
  71. */
  72. function _translation_tab_access($node) {
  73. return !empty($node->language) && translation_supported_type($node->type) && node_access('view', $node) && user_access('translate content');
  74. }
  75. /**
  76. * Implementation of hook_perm().
  77. */
  78. function translation_perm() {
  79. return array('translate content');
  80. }
  81. /**
  82. * Implementation of hook_form_alter().
  83. *
  84. * - Add translation option to content type form.
  85. * - Alters language fields on node forms when a translation
  86. * is about to be created.
  87. */
  88. function translation_form_alter(&$form, $form_state, $form_id) {
  89. if ($form_id == 'node_type_form') {
  90. // Add translation option to content type form.
  91. $form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
  92. // Description based on text from locale.module.
  93. $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 enabled 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/settings/language')));
  94. }
  95. elseif (isset($form['#id']) && $form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
  96. $node = $form['#node'];
  97. if (!empty($node->translation_source)) {
  98. // We are creating a translation. Add values and lock language field.
  99. $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
  100. $form['language']['#disabled'] = TRUE;
  101. }
  102. elseif (!empty($node->nid) && !empty($node->tnid)) {
  103. // Disable languages for existing translations, so it is not possible to switch this
  104. // node to some language which is already in the translation set. Also remove the
  105. // language neutral option.
  106. unset($form['language']['#options']['']);
  107. foreach (translation_node_get_translations($node->tnid) as $translation) {
  108. if ($translation->nid != $node->nid) {
  109. unset($form['language']['#options'][$translation->language]);
  110. }
  111. }
  112. // Add translation values and workflow options.
  113. $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
  114. $form['translation'] = array(
  115. '#type' => 'fieldset',
  116. '#title' => t('Translation settings'),
  117. '#access' => user_access('translate content'),
  118. '#collapsible' => TRUE,
  119. '#collapsed' => !$node->translate,
  120. '#tree' => TRUE,
  121. '#weight' => 30,
  122. );
  123. if ($node->tnid == $node->nid) {
  124. // This is the source node of the translation
  125. $form['translation']['retranslate'] = array(
  126. '#type' => 'checkbox',
  127. '#title' => t('Flag translations as outdated'),
  128. '#default_value' => 0,
  129. '#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.'),
  130. );
  131. $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
  132. }
  133. else {
  134. $form['translation']['status'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('This translation needs to be updated'),
  137. '#default_value' => $node->translate,
  138. '#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.'),
  139. );
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Implementation of hook_link().
  146. *
  147. * Display translation links with native language names, if this node
  148. * is part of a translation set.
  149. */
  150. function translation_link($type, $node = NULL, $teaser = FALSE) {
  151. $links = array();
  152. if ($type == 'node' && ($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
  153. // Do not show link to the same node.
  154. unset($translations[$node->language]);
  155. $languages = language_list();
  156. foreach ($languages as $langcode => $language) {
  157. if (isset($translations[$langcode]) && $translations[$langcode]->status) {
  158. $links["node_translation_$langcode"] = array(
  159. 'title' => $language->native,
  160. 'href' => 'node/'. $translations[$langcode]->nid,
  161. 'language' => $language,
  162. 'attributes' => array('title' => $translations[$langcode]->title, 'class' => 'translation-link')
  163. );
  164. }
  165. }
  166. }
  167. return $links;
  168. }
  169. /**
  170. * Implementation of hook_nodeapi().
  171. *
  172. * Manages translation information for nodes.
  173. */
  174. function translation_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  175. // Only act if we are dealing with a content type supporting translations.
  176. if (!translation_supported_type($node->type)) {
  177. return;
  178. }
  179. switch ($op) {
  180. case 'prepare':
  181. if (empty($node->nid) && user_access('translate content') && isset($_GET['translation']) && isset($_GET['language']) && is_numeric($_GET['translation'])) {
  182. $translation_source = node_load($_GET['translation']);
  183. if (empty($translation_source) || !node_access('view', $translation_source)) {
  184. // Source node not found or no access to view. We should not check
  185. // for edit access, since the translator might not have permissions
  186. // to edit the source node but should still be able to translate.
  187. return;
  188. }
  189. $language_list = language_list();
  190. if (!isset($language_list[$_GET['language']]) || ($translation_source->language == $_GET['language'])) {
  191. // If not supported language, or same language as source node, break.
  192. return;
  193. }
  194. // Populate fields based on source node.
  195. $node->language = $_GET['language'];
  196. $node->translation_source = $translation_source;
  197. $node->title = $translation_source->title;
  198. // If user has no access to the filter used for the body, Drupal core
  199. // does not let the edit form to appear, so we should avoid exposing
  200. // the source text here too.
  201. $node->body = filter_access($translation_source->format) ? $translation_source->body : '';
  202. // Let every module add custom translated fields.
  203. node_invoke_nodeapi($node, 'prepare translation');
  204. }
  205. break;
  206. case 'insert':
  207. if (!empty($node->translation_source)) {
  208. if ($node->translation_source->tnid) {
  209. // Add node to existing translation set.
  210. $tnid = $node->translation_source->tnid;
  211. }
  212. else {
  213. // Create new translation set, using nid from the source node.
  214. $tnid = $node->translation_source->nid;
  215. db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->translation_source->nid);
  216. }
  217. db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->nid);
  218. $node->tnid = $tnid;
  219. }
  220. break;
  221. case 'update':
  222. if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {
  223. // Update translation information.
  224. db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translation['status'], $node->nid);
  225. if (!empty($node->translation['retranslate'])) {
  226. // This is the source node, asking to mark all translations outdated.
  227. db_query("UPDATE {node} SET translate = 1 WHERE tnid = %d AND nid != %d", $node->tnid, $node->nid);
  228. }
  229. }
  230. break;
  231. case 'delete':
  232. translation_remove_from_set($node);
  233. break;
  234. }
  235. }
  236. /**
  237. * Remove a node from its translation set (if any)
  238. * and update the set accordingly.
  239. */
  240. function translation_remove_from_set($node) {
  241. if (isset($node->tnid)) {
  242. if (db_result(db_query('SELECT COUNT(*) FROM {node} WHERE tnid = %d', $node->tnid)) == 1) {
  243. // There is only one node left in the set: remove the set altogether.
  244. db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE tnid = %d', $node->tnid);
  245. }
  246. else {
  247. db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE nid = %d', $node->nid);
  248. // If the node being removed was the source of the translation set,
  249. // we pick a new source - preferably one that is up to date.
  250. if ($node->tnid == $node->nid) {
  251. $new_tnid = db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d ORDER BY translate ASC, nid ASC', $node->tnid));
  252. db_query('UPDATE {node} SET tnid = %d WHERE tnid = %d', $new_tnid, $node->tnid);
  253. }
  254. }
  255. }
  256. }
  257. /**
  258. * Get all nodes in a translation set, represented by $tnid.
  259. *
  260. * @param $tnid
  261. * The translation source nid of the translation set, the identifier
  262. * of the node used to derive all translations in the set.
  263. * @return
  264. * Array of partial node objects (nid, title, language) representing
  265. * all nodes in the translation set, in effect all translations
  266. * of node $tnid, including node $tnid itself. Because these are
  267. * partial nodes, you need to node_load() the full node, if you
  268. * need more properties. The array is indexed by language code.
  269. */
  270. function translation_node_get_translations($tnid) {
  271. static $translations = array();
  272. if (is_numeric($tnid) && $tnid) {
  273. if (!isset($translations[$tnid])) {
  274. $translations[$tnid] = array();
  275. $result = db_query(db_rewrite_sql('SELECT n.nid, n.type, n.uid, n.status, n.title, n.language FROM {node} n WHERE n.tnid = %d'), $tnid);
  276. while ($node = db_fetch_object($result)) {
  277. $translations[$tnid][$node->language] = $node;
  278. }
  279. }
  280. return $translations[$tnid];
  281. }
  282. }
  283. /**
  284. * Returns whether the given content type has support for translations.
  285. *
  286. * @return
  287. * Boolean value.
  288. */
  289. function translation_supported_type($type) {
  290. return variable_get('language_content_type_'. $type, 0) == TRANSLATION_ENABLED;
  291. }
  292. /**
  293. * Return paths of all translations of a node, based on
  294. * its Drupal path.
  295. *
  296. * @param $path
  297. * A Drupal path, for example node/432.
  298. * @return
  299. * An array of paths of translations of the node accessible
  300. * to the current user keyed with language codes.
  301. */
  302. function translation_path_get_translations($path) {
  303. $paths = array();
  304. // Check for a node related path, and for its translations.
  305. if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
  306. foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
  307. $paths[$language] = 'node/'. $translation_node->nid . $matches[2];
  308. }
  309. }
  310. return $paths;
  311. }
  312. /**
  313. * Implementation of hook_translation_link_alter().
  314. *
  315. * Replaces links with pointers to translated versions of the content.
  316. */
  317. function translation_translation_link_alter(&$links, $path) {
  318. if ($paths = translation_path_get_translations($path)) {
  319. // Path can only start with "node/$nid" or "node/$nid/" here.
  320. $path = explode('/', $path);
  321. $node = node_load($path[1]);
  322. $translations = translation_node_get_translations($node->tnid);
  323. foreach ($links as $langcode => $link) {
  324. if (isset($paths[$langcode]) && $translations[$langcode]->status) {
  325. // Translation in a different node.
  326. $links[$langcode]['href'] = $paths[$langcode];
  327. }
  328. else {
  329. // No translation in this language, or no permission to view.
  330. unset($links[$langcode]);
  331. }
  332. }
  333. }
  334. }