August 22, 2009

Drupal – create links to translated nodes

Filed under: Tips — Tags: , , , — Webopius @ 1:37 pm

When you are using Drupal’s internationalisation module, you will often reach the point in your page and node templates where you need to create a dynamic link to the correct content item depending on the site’s current language. The title of the link may also change to represent the language choice.

For example, /node/10 might be the English language version of a page, while /node/231 might be the French translation of the same content. In your template, you’ll need to dynamically generate a link to the right node AND the correct title text of the link depending on whether your user is viewing the original site (e.g. www.yoursite.com) or a translated version (www/yoursite.com/fr).

Here’s a quick solution, in your theme’s template.php file, create the following function:

/* Given a node id, find the current language and return a link to the node's translation (if any) */

function _translation_nodelink($nid) {
   global $language;
   // Look for a translated version of this node in the current site selected language
   $result = db_query("SELECT n2.nid, n2.title FROM {node} n1, {node} n2 WHERE n1.tnid = n2.tnid and n1.nid = %d and n2.language = '%s'", $nid, $language->language);
   $node = db_fetch_object($result);
   if ($node) {
      print l($node->title,"node/".$node->nid);
   }
   else
   {
      // No translation available, return the node's own original language title
      $node = node_load($nid);
      print l($node->title,"node/".$nid);
   }
}

Now, in your theme templates, wherever you need to generate the correct link to a node’s translated content depending on the language being used by the site, you simply include the following:

<?php print _translation_nodelink($nodeid)?>

Where $nodeid should be the node id of the ‘base language’ node (e.g. the English version).

What will happen is if the user is viewing the site in another language, this function will detect the language being used and generate a link to the translated node (if one is available), otherwise, it will generate a link to the original non-translated node. The link title will also be translated if a title translation is available.


Add to Technorati Favorites

8 Comments »

  1. What about the core translation_node_get_translations function?

    http://api.drupal.org/api/function/translation_node_get_translations/6

    Comment by Fletch — April 22, 2010 @ 3:07 pm

  2. great article. I got exactly the thing what i was looking for

    Comment by sohail arif — June 11, 2010 @ 11:07 am

  3. I have updated the query from:
    $result = db_query(“SELECT n2.nid, n2.title FROM {node} n1, {node} n2 WHERE n1.tnid = n2.tnid and n1.nid = %d and n2.language = ‘%s’”, $nid, $language->language);

    to:

    $result = db_query(“SELECT n2.nid, n2.title FROM {node} n1, {node} n2 WHERE n1.tnid = n2.tnid and n1.nid != 0 and n1.nid = %d and n2.language = ‘%s’”, $nid, $language->language);

    To provide accurate results if the node has no translations.

    Comment by Gabriel Ungureanu — September 17, 2010 @ 6:51 am

  4. But on the other hand, if we are to use the API function, wich would be the normal Drupal way, your function would look like this:


    function _translation_nodelink($nid) {
    $translations = translation_node_get_translations($nid);
    if (!empty($translations)) {
    global $language;
    if (!empty($translations[$language->language])) {
    return l($translations[$language->language]->title,'node/'.$translations[$language->language]->nid);
    }
    }
    $node = node_load($nid);
    return l($node->title,'node/'.$node->nid);
    }

    Then:

    <?php echo _translation_nodelink(1);?>

    Comment by Gabriel Ungureanu — September 17, 2010 @ 6:59 am

  5. Hello, That was really very interesting and informative. For long I had been searching for a way out to provide such SEO friendly dynamic links for translated pages. Thanks a lot for some nice tips and codes. I wonder whether the same idea can be applied in case of google translated pages like when we use Google translate module ( http://drupal.org/project/gtrans ) we get the translated page but the page URL remains the same instead of showing the language link like example.com/fr or it etc. Can your code be applied in such cases. Please help.

    Comment by Athena Adam — September 21, 2010 @ 3:06 pm

  6. is there a good free french translation tool on the internet ?.“

    Comment by PA Amplifier : — October 28, 2010 @ 11:52 am

  7. Thanks, Gabriel! Just what I needed, my code to print out a translated node:

    <?php
    $nid = 66;

    $translations = translation_node_get_translations($nid);
    if (!empty($translations)) {
    global $language;
    if (!empty($translations[$language->language])) {
    $nid = $translations[$language->language]->nid;
    }
    }
    $node = node_load($nid);
    $build = node_view($node);
    print drupal_render($build);
    ?>

    Comment by Matt — March 7, 2011 @ 3:20 am

  8. I am an user of WordPress and I am just wondering whether there is a code which can be added, which will enable visitors to a site to easily translate the article on screen?

    Comment by Joanne — May 9, 2011 @ 11:05 am

RSS feed for comments on this post. TrackBack URL

Leave a comment