comparison modules/translation/translation.module @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 589fb7c02327
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: translation.module,v 1.23 2008/01/28 11:38:58 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 if (!empty($node->language) && translation_supported_type($node->type)) {
80 return user_access('translate content');
81 }
82 return FALSE;
83 }
84
85 /**
86 * Implementation of hook_perm().
87 */
88 function translation_perm() {
89 return array('translate content');
90 }
91
92 /**
93 * Implementation of hook_form_alter().
94 *
95 * - Add translation option to content type form.
96 * - Alters language fields on node forms when a translation
97 * is about to be created.
98 */
99 function translation_form_alter(&$form, $form_state, $form_id) {
100 if ($form_id == 'node_type_form') {
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 elseif (isset($form['#id']) && $form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
107 $node = $form['#node'];
108 if (!empty($node->translation_source)) {
109 // We are creating a translation. Add values and lock language field.
110 $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
111 $form['language']['#disabled'] = TRUE;
112 }
113 elseif (!empty($node->nid) && !empty($node->tnid)) {
114 // Disable languages for existing translations, so it is not possible to switch this
115 // node to some language which is already in the translation set. Also remove the
116 // language neutral option.
117 unset($form['language']['#options']['']);
118 foreach (translation_node_get_translations($node->tnid) as $translation) {
119 if ($translation->nid != $node->nid) {
120 unset($form['language']['#options'][$translation->language]);
121 }
122 }
123 // Add translation values and workflow options.
124 $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
125 $form['translation'] = array(
126 '#type' => 'fieldset',
127 '#title' => t('Translation settings'),
128 '#access' => user_access('translate content'),
129 '#collapsible' => TRUE,
130 '#collapsed' => !$node->translate,
131 '#tree' => TRUE,
132 '#weight' => 30,
133 );
134 if ($node->tnid == $node->nid) {
135 // This is the source node of the translation
136 $form['translation']['retranslate'] = array(
137 '#type' => 'checkbox',
138 '#title' => t('Flag translations as outdated'),
139 '#default_value' => 0,
140 '#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.'),
141 );
142 $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
143 }
144 else {
145 $form['translation']['status'] = array(
146 '#type' => 'checkbox',
147 '#title' => t('This translation needs to be updated'),
148 '#default_value' => $node->translate,
149 '#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.'),
150 );
151 }
152 }
153 }
154 }
155
156 /**
157 * Implementation of hook_link().
158 *
159 * Display translation links with native language names, if this node
160 * is part of a translation set.
161 */
162 function translation_link($type, $node = NULL, $teaser = FALSE) {
163 $links = array();
164 if ($type == 'node' && ($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
165 // Do not show link to the same node.
166 unset($translations[$node->language]);
167 $languages = language_list();
168 foreach ($translations as $language => $translation) {
169 $links["node_translation_$language"] = array(
170 'title' => $languages[$language]->native,
171 'href' => "node/$translation->nid",
172 'language' => $languages[$language],
173 'attributes' => array('title' => $translation->title, 'class' => 'translation-link')
174 );
175 }
176 }
177 return $links;
178 }
179
180 /**
181 * Implementation of hook_nodeapi().
182 *
183 * Manages translation information for nodes.
184 */
185 function translation_nodeapi(&$node, $op, $teaser, $page) {
186 // Only act if we are dealing with a content type supporting translations.
187 if (!translation_supported_type($node->type)) {
188 return;
189 }
190
191 switch ($op) {
192 case 'prepare':
193 if (empty($node->nid) && isset($_GET['translation']) && isset($_GET['language']) &&
194 ($source_nid = $_GET['translation']) && ($language = $_GET['language']) &&
195 (user_access('translate content'))) {
196 // We are translating a node from a source node, so
197 // load the node to be translated and populate fields.
198 $node->language = $language;
199 $node->translation_source = node_load($source_nid);
200 $node->title = $node->translation_source->title;
201 $node->body = $node->translation_source->body;
202 // Let every module add custom translated fields.
203 node_invoke_nodeapi($node, 'prepare translation');
204 }
205 break;
206
207 case 'insert':
208 if (!empty($node->translation_source)) {
209 if ($node->translation_source->tnid) {
210 // Add node to existing translation set.
211 $tnid = $node->translation_source->tnid;
212 }
213 else {
214 // Create new translation set, using nid from the source node.
215 $tnid = $node->translation_source->nid;
216 db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->translation_source->nid);
217 }
218 db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->nid);
219 }
220 break;
221
222 case 'update':
223 if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {
224 // Update translation information.
225 db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translation['status'], $node->nid);
226 if (!empty($node->translation['retranslate'])) {
227 // This is the source node, asking to mark all translations outdated.
228 db_query("UPDATE {node} SET translate = 1 WHERE tnid = %d AND nid != %d", $node->tnid, $node->nid);
229 }
230 }
231 break;
232
233 case 'delete':
234 translation_remove_from_set($node);
235 break;
236 }
237 }
238
239 /**
240 * Remove a node from its translation set (if any)
241 * and update the set accordingly.
242 */
243 function translation_remove_from_set($node) {
244 if (isset($node->tnid)) {
245 if (db_result(db_query('SELECT COUNT(*) FROM {node} WHERE tnid = %d', $node->tnid)) <= 2) {
246 // There would only be one node left in the set: remove the set altogether.
247 db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE tnid = %d', $node->tnid);
248 }
249 else {
250 db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE nid = %d', $node->nid);
251
252 // If the node being removed was the source of the translation set,
253 // we pick a new source - preferably one that is up to date.
254 if ($node->tnid == $node->nid) {
255 $new_tnid = db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d ORDER BY translate ASC, nid ASC', $node->tnid));
256 db_query('UPDATE {node} SET tnid = %d WHERE tnid = %d', $new_tnid, $node->tnid);
257 }
258 }
259 }
260 }
261
262 /**
263 * Get all nodes in a translation set, represented by $tnid.
264 *
265 * @param $tnid
266 * The translation source nid of the translation set, the identifier
267 * of the node used to derive all translations in the set.
268 * @return
269 * Array of partial node objects (nid, title, language) representing
270 * all nodes in the translation set, in effect all translations
271 * of node $tnid, including node $tnid itself. Because these are
272 * partial nodes, you need to node_load() the full node, if you
273 * need more properties. The array is indexed by language code.
274 */
275 function translation_node_get_translations($tnid) {
276 static $translations = array();
277
278 if (is_numeric($tnid) && $tnid) {
279 if (!isset($translations[$tnid])) {
280 $translations[$tnid] = array();
281 $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.language FROM {node} n WHERE n.tnid = %d'), $tnid);
282 while ($node = db_fetch_object($result)) {
283 $translations[$tnid][$node->language] = $node;
284 }
285 }
286 return $translations[$tnid];
287 }
288 }
289
290 /**
291 * Returns whether the given content type has support for translations.
292 *
293 * @return
294 * Boolean value.
295 */
296 function translation_supported_type($type) {
297 return variable_get('language_content_type_'. $type, 0) == TRANSLATION_ENABLED;
298 }
299
300 /**
301 * Return paths of all translations of a node, based on
302 * its Drupal path.
303 *
304 * @param $path
305 * A Drupal path, for example node/432.
306 * @return
307 * An array of paths of translations of the node accessible
308 * to the current user keyed with language codes.
309 */
310 function translation_path_get_translations($path) {
311 $paths = array();
312 // Check for a node related path, and for its translations.
313 if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
314 foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
315 $paths[$language] = 'node/'. $translation_node->nid . $matches[2];
316 }
317 }
318 return $paths;
319 }
320
321 /**
322 * Implementation of hook_alter_translation_link().
323 *
324 * Replaces links with pointers to translated versions of the content.
325 */
326 function translation_translation_link_alter(&$links, $path) {
327 if ($paths = translation_path_get_translations($path)) {
328 foreach ($links as $langcode => $link) {
329 if (isset($paths[$langcode])) {
330 // Translation in a different node.
331 $links[$langcode]['href'] = $paths[$langcode];
332 }
333 else {
334 // No translation in this language, or no permission to view.
335 unset($links[$langcode]);
336 }
337 }
338 }
339 }
340