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