/[drupal]/drupal/modules/translation/translation.module
ViewVC logotype

Contents of /drupal/modules/translation/translation.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.63 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (3 weeks, 6 days ago) by dries
Branch: MAIN
Changes since 1.62: +9 -9 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: translation.module,v 1.62 2009/10/11 03:07:21 webchick 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 * Implement 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/config/people/permissions'))) . '</li>';
37 $output .= '<li>' . t('Add and enable desired languages at the <a href="@languages">Languages configuration page</a>.', array('@languages' => url('admin/config/regional/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>Publishing options</em>.) Be sure to save each content type after enabling multilingual support.', array('@content-types' => url('admin/structure/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'))) . '</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/structure/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 * Implement 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(stdClass $node) {
79 if (!empty($node->language) && translation_supported_type($node->type)) {
80 return user_access('translate content');
81 }
82 return FALSE;
83 }
84
85 /**
86 * Implement hook_permission().
87 */
88 function translation_permission() {
89 return array(
90 'translate content' => array(
91 'title' => t('Translate content'),
92 'description' => t('Translate website content.'),
93 ),
94 );
95 }
96
97 /**
98 * Implement hook_form_FORM_ID_alter().
99 */
100 function translation_form_node_type_form_alter(&$form, &$form_state) {
101 // Add translation option to content type form.
102 $form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
103 // Description based on text from locale.module.
104 $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')));
105 }
106
107 /**
108 * Implement hook_form_alter().
109 *
110 * - Add translation option to content type form.
111 * - Alters language fields on node forms when a translation
112 * is about to be created.
113 */
114 function translation_form_alter(&$form, &$form_state, $form_id) {
115 if (isset($form['#id']) && $form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
116 $node = $form['#node'];
117 if (!empty($node->translation_source)) {
118 // We are creating a translation. Add values and lock language field.
119 $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
120 $form['language']['#disabled'] = TRUE;
121 }
122 elseif (!empty($node->nid) && !empty($node->tnid)) {
123 // Disable languages for existing translations, so it is not possible to switch this
124 // node to some language which is already in the translation set. Also remove the
125 // language neutral option.
126 unset($form['language']['#options']['']);
127 foreach (translation_node_get_translations($node->tnid) as $translation) {
128 if ($translation->nid != $node->nid) {
129 unset($form['language']['#options'][$translation->language]);
130 }
131 }
132 // Add translation values and workflow options.
133 $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
134 $form['translation'] = array(
135 '#type' => 'fieldset',
136 '#title' => t('Translation settings'),
137 '#access' => user_access('translate content'),
138 '#collapsible' => TRUE,
139 '#collapsed' => !$node->translate,
140 '#tree' => TRUE,
141 '#weight' => 30,
142 );
143 if ($node->tnid == $node->nid) {
144 // This is the source node of the translation
145 $form['translation']['retranslate'] = array(
146 '#type' => 'checkbox',
147 '#title' => t('Flag translations as outdated'),
148 '#default_value' => 0,
149 '#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.'),
150 );
151 $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
152 }
153 else {
154 $form['translation']['status'] = array(
155 '#type' => 'checkbox',
156 '#title' => t('This translation needs to be updated'),
157 '#default_value' => $node->translate,
158 '#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.'),
159 );
160 }
161 }
162 }
163 }
164
165 /**
166 * Implement hook_node_view().
167 *
168 * Display translation links with native language names, if this node
169 * is part of a translation set.
170 */
171 function translation_node_view(stdClass $node, $build_mode) {
172 if (isset($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
173 $path = 'node/' . $node->nid;
174 $links = language_negotiation_get_switch_links(LANGUAGE_TYPE_CONTENT, $path);
175 if (is_object($links)) {
176 $links = $links->links;
177 // Do not show link to the same node.
178 unset($links[$node->language]);
179 $node->content['links']['translation'] = array(
180 '#theme' => 'links',
181 '#links' => $links,
182 '#attributes' => array('class' => array('links', 'inline')),
183 );
184 }
185 }
186 }
187
188 /**
189 * Implement hook_node_prepare().
190 */
191 function translation_node_prepare(stdClass $node) {
192 // Only act if we are dealing with a content type supporting translations.
193 if (translation_supported_type($node->type)) {
194 if (empty($node->nid) && isset($_GET['translation']) && isset($_GET['language']) &&
195 ($source_nid = $_GET['translation']) && ($language = $_GET['language']) &&
196 (user_access('translate content'))) {
197 // We are translating a node from a source node, so
198 // load the node to be translated and populate fields.
199 $source_node = node_load($source_nid);
200 // Ensure we don't have an existing translation in this language.
201 if (!empty($source_node->tnid)) {
202 $translations = translation_node_get_translations($source_node->tnid);
203 if (isset($translations[$language])) {
204 $languages = language_list();
205 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[FIELD_LANGUAGE_NONE][0]['value'], '%language' => $languages[$language]->name, '%type' => $node->type)), 'error');
206 return;
207 }
208 }
209 $node->language = $language;
210 $node->translation_source = $source_node;
211 $node->title = $node->translation_source->title;
212 // Let every module add custom translated fields.
213 module_invoke_all('node_prepare_translation', $node);
214 }
215 }
216 }
217
218 /**
219 * Implement hook_node_insert().
220 */
221 function translation_node_insert(stdClass $node) {
222 // Only act if we are dealing with a content type supporting translations.
223 if (translation_supported_type($node->type)) {
224 if (!empty($node->translation_source)) {
225 if ($node->translation_source->tnid) {
226 // Add node to existing translation set.
227 $tnid = $node->translation_source->tnid;
228 }
229 else {
230 // Create new translation set, using nid from the source node.
231 $tnid = $node->translation_source->nid;
232 db_update('node')
233 ->fields(array(
234 'tnid' => $tnid,
235 'translate' => 0,
236 ))
237 ->condition('nid', $node->translation_source->nid)
238 ->execute();
239 }
240 db_update('node')
241 ->fields(array(
242 'tnid' => $tnid,
243 'translate' => 0,
244 ))
245 ->condition('nid', $node->nid)
246 ->execute();
247 }
248 }
249 }
250
251 /**
252 * Implement hook_node_update().
253 */
254 function translation_node_update(stdClass $node) {
255 // Only act if we are dealing with a content type supporting translations.
256 if (translation_supported_type($node->type)) {
257 if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {
258 // Update translation information.
259 db_update('node')
260 ->fields(array(
261 'tnid' => $node->tnid,
262 'translate' => $node->translation['status'],
263 ))
264 ->condition('nid', $node->nid)
265 ->execute();
266 if (!empty($node->translation['retranslate'])) {
267 // This is the source node, asking to mark all translations outdated.
268 db_update('node')
269 ->fields(array('translate' => 1))
270 ->condition('nid', $node->nid, '<>')
271 ->condition('tnid', $node->tnid)
272 ->execute();
273 }
274 }
275 }
276 }
277
278 /**
279 * Implement hook_node_validate().
280 *
281 * Ensure that duplicate translations can not be created for the same source.
282 */
283 function translation_node_validate(stdClass $node, $form) {
284 // Only act on translatable nodes with a tnid or translation_source.
285 if (translation_supported_type($node->type) && (!empty($node->tnid) || !empty($form['#node']->translation_source->nid))) {
286 $tnid = !empty($node->tnid) ? $node->tnid : $form['#node']->translation_source->nid;
287 $translations = translation_node_get_translations($tnid);
288 if (isset($translations[$node->language]) && $translations[$node->language]->nid != $node->nid ) {
289 form_set_error('language', t('There is already a translation in this language.'));
290 }
291 }
292 }
293
294 /**
295 * Implement hook_node_delete().
296 */
297 function translation_node_delete(stdClass $node) {
298 // Only act if we are dealing with a content type supporting translations.
299 if (translation_supported_type($node->type)) {
300 translation_remove_from_set($node);
301 }
302 }
303
304 /**
305 * Remove a node from its translation set (if any)
306 * and update the set accordingly.
307 */
308 function translation_remove_from_set(stdClass $node) {
309 if (isset($node->tnid)) {
310 $query = db_update('node')
311 ->fields(array(
312 'tnid' => 0,
313 'translate' => 0,
314 ));
315 if (db_query('SELECT COUNT(*) FROM {node} WHERE tnid = :tnid', array(':tnid' => $node->tnid))->fetchField() == 1) {
316 // There is only one node left in the set: remove the set altogether.
317 $query
318 ->condition('tnid', $node->tnid)
319 ->execute();
320 }
321 else {
322 $query
323 ->condition('nid', $node->nid)
324 ->execute();
325
326 // If the node being removed was the source of the translation set,
327 // we pick a new source - preferably one that is up to date.
328 if ($node->tnid == $node->nid) {
329 $new_tnid = db_query('SELECT nid FROM {node} WHERE tnid = :tnid ORDER BY translate ASC, nid ASC', array(':tnid' => $node->tnid))->fetchField();
330 db_update('node')
331 ->fields(array('tnid' => $new_tnid))
332 ->condition('tnid', $node->tnid)
333 ->execute();
334 }
335 }
336 }
337 }
338
339 /**
340 * Get all nodes in a translation set, represented by $tnid.
341 *
342 * @param $tnid
343 * The translation source nid of the translation set, the identifier
344 * of the node used to derive all translations in the set.
345 * @return
346 * Array of partial node objects (nid, title, language) representing
347 * all nodes in the translation set, in effect all translations
348 * of node $tnid, including node $tnid itself. Because these are
349 * partial nodes, you need to node_load() the full node, if you
350 * need more properties. The array is indexed by language code.
351 */
352 function translation_node_get_translations($tnid) {
353 if (is_numeric($tnid) && $tnid) {
354 $translations = &drupal_static(__FUNCTION__, array());
355
356 if (!isset($translations[$tnid])) {
357 $translations[$tnid] = array();
358 $result = db_select('node', 'n')
359 ->fields('n', array('nid', 'title', 'language'))
360 ->condition('n.tnid', $tnid)
361 ->addTag('node_access')
362 ->execute();
363
364 foreach ($result as $node) {
365 $translations[$tnid][$node->language] = $node;
366 }
367 }
368 return $translations[$tnid];
369 }
370 }
371
372 /**
373 * Returns whether the given content type has support for translations.
374 *
375 * @return
376 * Boolean value.
377 */
378 function translation_supported_type($type) {
379 return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
380 }
381
382 /**
383 * Return paths of all translations of a node, based on
384 * its Drupal path.
385 *
386 * @param $path
387 * A Drupal path, for example node/432.
388 * @return
389 * An array of paths of translations of the node accessible
390 * to the current user keyed with language codes.
391 */
392 function translation_path_get_translations($path) {
393 $paths = array();
394 // Check for a node related path, and for its translations.
395 if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
396 foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
397 $paths[$language] = 'node/' . $translation_node->nid . $matches[2];
398 }
399 }
400 return $paths;
401 }
402
403 /**
404 * Implement hook_language_switch_link_alter().
405 *
406 * Replaces links with pointers to translated versions of the content.
407 */
408 function translation_language_switch_links_alter(array &$links, $type, $path) {
409 if ($type == LANGUAGE_TYPE_CONTENT && $paths = translation_path_get_translations($path)) {
410 foreach ($links as $langcode => $link) {
411 if (isset($paths[$langcode])) {
412 // Translation in a different node.
413 $links[$langcode]['href'] = $paths[$langcode];
414 }
415 else {
416 // No translation in this language, or no permission to view.
417 unset($links[$langcode]);
418 }
419 }
420 }
421 }

  ViewVC Help
Powered by ViewVC 1.1.2