annotate modules/translation/translation.module @ 20:e3d20ebd63d1 tip

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