Translation helpers

nedjo - October 8, 2008 - 00:32

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).

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

Developer usage

hook_nodeapi('translation_change', $node)

Translation helpers invokes a new $op for 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:

<?php
function example_nodeapi() {
  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.

Sponsored in part by CivicActions.

Releases

Official releasesDateSizeLinksStatus
6.x-1.02009-Aug-187.21 KBRecommended for 6.xThis is currently the recommended release for 6.x.
Development snapshotsDateSizeLinksStatus
6.x-1.x-dev2009-Aug-187.22 KBDevelopment snapshotDevelopment snapshots are automatically regenerated and their contents can frequently change, so they are not recommended for production use.


 
 

Drupal is a registered trademark of Dries Buytaert.