Translation helpers enables other modules to respond to changes in the "source translation" of a set of translated content. This functionality is useful for modules that track data by the "source translation" (node.tnid value).

This module doesn't do anything on its own. You should install it only if it's suggested or required by another module you're using.

The module also provides other methods for modules to use with translated content.

Drupal 8

A Drupal 8 version won't be needed. Translation Helpers addresses use cases related to the model of translation used in the Drupal 7 core Content Translation module, in which each language has its own node. Drupal 8 has switched to fields-based translation, in which the awkward workarounds of D7 will be a thing of the past.

Developer usage

hook_node_translation_change(node)

Translation helpers invokes a hook_nodeapi_translation_change().

Implement this hook to respond to a change in a translation set. This is useful for any module tracking data by source translation (node.tnid).

The $node object passed to this hook has a $node->translation_change property, consisting of an array. Keys are:

  • 'old_tnid': the now-deleted tnid (translation set ID).
  • 'new_tnid': the new translation set ID. If the translation set has been removed (because there was only a single remaining translation), this will be 0.
  • 'remaining_nid': in the case that the translation set was removed, the nid of the remining former member of the translation set.

When a translation is deleted, one of two things can happen:

  1. If there is only one remaining member of the translation set, the set is removed. In this case, the $node->translation_change['new_tnid'] value will be 0.
  2. If the deleted node was the source translation and there are at least two remaining translations, a new translation source is selected. In this case, the $node->translation_change['new_tnid'] will be the nid of new translation source.

Here's a sample hook implementation in Drupal 7:

<?php
function example_node_translation_change($node) {
  if (isset(
$node->translation_change)) {
   
// If there is only one node remaining, track by nid rather than tnid. Otherwise, use
    // the new tnid.
   
$new = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
   
db_update('example')
      ->
condition('id', $node->translation_change['old_tnid'])
      ->
fields(array(
       
'id' => $new,
      ))
      ->
execute();
  }
}
?>

Here's a sample hook implementation in Drupal 6:

<?php
function example_nodeapi($op, &$node) {
  switch (
$op) {
    case
'translation_change':
      if (isset(
$node->translation_change)) {
       
// If there is only one node remaining, track by nid rather than tnid. Otherwise, use
        // the new tnid.
       
$new = $node->translation_change['new_tnid'] == 0 ? $node->translation_change['remaining_nid'] : $node->translation_change['new_tnid'];
       
db_query('UPDATE {example} SET id = %d WHERE id = %d', $new, $node->translation_change['old_tnid']);
      }
      break;
  }
}
?>

See #318328: Hook to respond to change of source translation for a patch to add this hook to the core translation module.

translation_helpers_get_translation($node, $language)

Call this function to get the translation of a node in a given language. Returns FALSE if no such translation found.

translation_helpers_get_source($node)

Call this function to get the source translation of a node. Returns FALSE if no such translation found.

Initial development sponsored in part by CivicActions.

Project Information

Downloads