translation.module 16.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
// $Id$

/**
 * @file
 *   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).
 */

/**
 * Identifies a content type which has translation support enabled.
 */
26
define('TRANSLATION_ENABLED', 2);
27 28 29 30

/**
 * Implementation of hook_help().
 */
31 32
function translation_help($path, $arg) {
  switch ($path) {
33
    case 'admin/help#translation':
34 35 36 37 38 39 40 41 42 43 44 45
      $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>';
      $output .= '<p>' . t('Configuring content translation and translation-enabled content types:') . '</p>';
      $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>';
      $output .= '<li>' . t('Add and enable desired languages at the <a href="@languages">Languages configuration page</a>.', array('@languages' => url('admin/settings/language'))) . '</li>';
      $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/build/types'))) . '</li></ul>';
      $output .= '<p>' . t('Working with translation-enabled content types:') . '</p>';
      $output .= '<ul><li>' . t('Use the <em>Language</em> drop down to select the appropriate language when creating or editing posts.') . '</li>';
      $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>';
      $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>';
      $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>';
      $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>';
      $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 47
      return $output;
    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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      return $output;
  }
}

/**
 * Implementation of hook_menu().
 */
function translation_menu() {
  $items = array();
  $items['node/%node/translate'] = array(
    'title' => 'Translate',
    'page callback' => 'translation_node_overview',
    'page arguments' => array(1),
    'access callback' => '_translation_tab_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
  );
  return $items;
}

/**
 * Menu access callback.
 *
 * Only display translation tab for node types, which have translation enabled
 * and where the current node is not language neutral (which should span
 * all languages).
 */
function _translation_tab_access($node) {
  if (!empty($node->language) && translation_supported_type($node->type)) {
    return user_access('translate content');
  }
  return FALSE;
}

/**
 * Implementation of hook_perm().
 */
function translation_perm() {
88 89 90
  return array(
    'translate content' => t('Translate website content.'),
  );
91 92 93 94 95 96 97 98 99 100 101 102
}

/**
 * Implementation of hook_form_alter().
 *
 * - Add translation option to content type form.
 * - Alters language fields on node forms when a translation
 *   is about to be created.
 */
function translation_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'node_type_form') {
    // Add translation option to content type form.
103
    $form['workflow']['language_content_type']['#options'][TRANSLATION_ENABLED] = t('Enabled, with translation');
104
    // Description based on text from locale.module.
105
    $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')));
106
  }
107
  elseif (isset($form['#id']) && $form['#id'] == 'node-form' && translation_supported_type($form['#node']->type)) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    $node = $form['#node'];
    if (!empty($node->translation_source)) {
      // We are creating a translation. Add values and lock language field.
      $form['translation_source'] = array('#type' => 'value', '#value' => $node->translation_source);
      $form['language']['#disabled'] = TRUE;
    }
    elseif (!empty($node->nid) && !empty($node->tnid)) {
      // Disable languages for existing translations, so it is not possible to switch this
      // node to some language which is already in the translation set. Also remove the
      // language neutral option.
      unset($form['language']['#options']['']);
      foreach (translation_node_get_translations($node->tnid) as $translation) {
        if ($translation->nid != $node->nid) {
          unset($form['language']['#options'][$translation->language]);
        }
      }
      // Add translation values and workflow options.
125
      $form['tnid'] = array('#type' => 'value', '#value' => $node->tnid);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      $form['translation'] = array(
        '#type' => 'fieldset',
        '#title' => t('Translation settings'),
        '#access' => user_access('translate content'),
        '#collapsible' => TRUE,
        '#collapsed' => !$node->translate,
        '#tree' => TRUE,
        '#weight' => 30,
      );
      if ($node->tnid == $node->nid) {
        // This is the source node of the translation
        $form['translation']['retranslate'] = array(
          '#type' => 'checkbox',
          '#title' => t('Flag translations as outdated'),
          '#default_value' => 0,
          '#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.'),
        );
        $form['translation']['status'] = array('#type' => 'value', '#value' => 0);
      }
      else {
        $form['translation']['status'] = array(
          '#type' => 'checkbox',
          '#title' => t('This translation needs to be updated'),
          '#default_value' => $node->translate,
          '#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.'),
        );
      }
    }
  }
}

/**
 * Implementation of hook_link().
 *
 * Display translation links with native language names, if this node
 * is part of a translation set.
 */
function translation_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();
  if ($type == 'node' && ($node->tnid) && $translations = translation_node_get_translations($node->tnid)) {
    // Do not show link to the same node.
    unset($translations[$node->language]);
168
    $languages = language_list();
169 170 171 172 173 174 175 176 177
    foreach ($languages as $langcode => $language) {
      if (isset($translations[$langcode])) {
        $links["node_translation_$langcode"] = array(
          'title' => $language->native,
          'href' => 'node/' . $translations[$langcode]->nid,
          'language' => $language,
          'attributes' => array('title' => $translations[$langcode]->title, 'class' => 'translation-link')
        );
      }
178 179 180 181 182 183
    }
  }
  return $links;
}

/**
184
 * Implementation of hook_nodeapi_prepare().
185
 */
186
function translation_nodeapi_prepare(&$node, $teaser, $page) {
187
  // Only act if we are dealing with a content type supporting translations.
188 189 190 191 192 193 194 195 196 197 198 199 200
  if (translation_supported_type($node->type)) {
    if (empty($node->nid) && isset($_GET['translation']) && isset($_GET['language']) &&
        ($source_nid = $_GET['translation']) && ($language = $_GET['language']) &&
        (user_access('translate content'))) {
      // We are translating a node from a source node, so
      // load the node to be translated and populate fields.
      $node->language = $language;
      $node->translation_source = node_load($source_nid);
      $node->title = $node->translation_source->title;
      $node->body = $node->translation_source->body;
      // Let every module add custom translated fields.
      node_invoke_nodeapi($node, 'prepare translation');
    }
201
  }
202
}
203

204 205 206 207 208 209 210 211 212 213
/**
 * Implementation of hook_nodeapi_insert().
 */
function translation_nodeapi_insert(&$node, $teaser, $page) {
  // Only act if we are dealing with a content type supporting translations.
  if (translation_supported_type($node->type)) {
    if (!empty($node->translation_source)) {
      if ($node->translation_source->tnid) {
        // Add node to existing translation set.
        $tnid = $node->translation_source->tnid;
214
      }
215 216 217 218
      else {
        // Create new translation set, using nid from the source node.
        $tnid = $node->translation_source->nid;
        db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->translation_source->nid);
219
      }
220 221 222 223
      db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $tnid, 0, $node->nid);
    }
  }
}
224

225 226 227 228 229 230 231 232 233 234 235 236
/**
 * Implementation of hook_nodeapi_update().
 */
function translation_nodeapi_update(&$node, $teaser, $page) {
  // Only act if we are dealing with a content type supporting translations.
  if (translation_supported_type($node->type)) {
    if (isset($node->translation) && $node->translation && !empty($node->language) && $node->tnid) {
      // Update translation information.
      db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $node->tnid, $node->translation['status'], $node->nid);
      if (!empty($node->translation['retranslate'])) {
        // This is the source node, asking to mark all translations outdated.
        db_query("UPDATE {node} SET translate = 1 WHERE tnid = %d AND nid <> %d", $node->tnid, $node->nid);
237
      }
238
    }
239 240 241
  }
}

242 243 244 245 246 247 248 249 250 251
/**
 * Implementation of hook_nodeapi_delete().
 */
function translation_nodeapi_delete(&$node, $teaser, $page) {
  // Only act if we are dealing with a content type supporting translations.
  if (translation_supported_type($node->type)) {
    translation_remove_from_set($node);
  }
}
 
252 253 254 255 256 257
/**
 * Remove a node from its translation set (if any)
 * and update the set accordingly.
 */
function translation_remove_from_set($node) {
  if (isset($node->tnid)) {
258 259
    if (db_result(db_query('SELECT COUNT(*) FROM {node} WHERE tnid = %d', $node->tnid)) == 1) {
      // There is only one node left in the set: remove the set altogether.
260 261 262 263 264 265 266 267 268 269 270 271
      db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE tnid = %d', $node->tnid);
    }
    else {
      db_query('UPDATE {node} SET tnid = 0, translate = 0 WHERE nid = %d', $node->nid);

      // If the node being removed was the source of the translation set,
      // we pick a new source - preferably one that is up to date.
      if ($node->tnid == $node->nid) {
        $new_tnid = db_result(db_query('SELECT nid FROM {node} WHERE tnid = %d ORDER BY translate ASC, nid ASC', $node->tnid));
        db_query('UPDATE {node} SET tnid = %d WHERE tnid = %d', $new_tnid, $node->tnid);
      }
    }
272 273 274
  }
}

275 276 277 278 279 280 281
/**
 * Get all nodes in a translation set, represented by $tnid.
 *
 * @param $tnid
 *   The translation source nid of the translation set, the identifier
 *   of the node used to derive all translations in the set.
 * @return
282 283 284
 *   Array of partial node objects (nid, title, language) representing
 *   all nodes in the translation set, in effect all translations
 *   of node $tnid, including node $tnid itself. Because these are
285
 *   partial nodes, you need to node_load() the full node, if you
286
 *   need more properties. The array is indexed by language code.
287 288 289 290 291 292 293
 */
function translation_node_get_translations($tnid) {
  static $translations = array();

  if (is_numeric($tnid) && $tnid) {
    if (!isset($translations[$tnid])) {
      $translations[$tnid] = array();
294
      $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.language FROM {node} n WHERE n.tnid = %d'), $tnid);
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
      while ($node = db_fetch_object($result)) {
        $translations[$tnid][$node->language] = $node;
      }
    }
    return $translations[$tnid];
  }
}

/**
 * Returns whether the given content type has support for translations.
 *
 * @return
 *   Boolean value.
 */
function translation_supported_type($type) {
310
  return variable_get('language_content_type_' . $type, 0) == TRANSLATION_ENABLED;
311
}
312 313 314

/**
 * Return paths of all translations of a node, based on
315
 * its Drupal path.
316 317 318 319
 *
 * @param $path
 *   A Drupal path, for example node/432.
 * @return
320
 *   An array of paths of translations of the node accessible
321 322 323 324 325 326 327
 *   to the current user keyed with language codes.
 */
function translation_path_get_translations($path) {
  $paths = array();
  // Check for a node related path, and for its translations.
  if ((preg_match("!^node/([0-9]+)(/.+|)$!", $path, $matches)) && ($node = node_load((int)$matches[1])) && !empty($node->tnid)) {
    foreach (translation_node_get_translations($node->tnid) as $language => $translation_node) {
328
      $paths[$language] = 'node/' . $translation_node->nid . $matches[2];
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    }
  }
  return $paths;
}

/**
 * Implementation of hook_alter_translation_link().
 *
 * Replaces links with pointers to translated versions of the content.
 */
function translation_translation_link_alter(&$links, $path) {
  if ($paths = translation_path_get_translations($path)) {
    foreach ($links as $langcode => $link) {
      if (isset($paths[$langcode])) {
        // Translation in a different node.
        $links[$langcode]['href'] = $paths[$langcode];
      }
      else {
        // No translation in this language, or no permission to view.
        unset($links[$langcode]);
      }
    }
  }
}
353