How to retrieve URL of translated node in PHP?

Events happening in the community are now at Drupal community events on www.drupal.org.
Boland's picture

I've translated the site using i18n module, everything works fine. I have to write some PHP code that, given default, original node ID, returns the full URL of the page.
So, e.g. full_url($nid=10) returns when English selected www.host.com/?q=node/10 , but in Dutch e.g. www.host.com/?q=nl/node/30

Is such a function available which will help me?

I have developed www.cloneboy.eu and in the image with products I've hardcoded the links to the product nodes. This will always show the English page. When you e.g. select French and click on a product, the English version is shown. In the block I want to make these URL's dynamic...

Comments

you can use this code, it

SumyTheSlayer's picture

you can use this code, it make a list of translations for a given node #fnid (either current node or a different node)

<?php
global $language;
if(
arg(0) == 'node' && is_numeric(arg(1))) {
 
$lList = array();
 
#$fnid = arg(1); #get current nid
 
$fnid = '6';
#$nl = $node;
 
$nl = node_load($fnid);
   
$nt = module_invoke('translation', 'node_get_translations', $nl->tnid);
foreach(
$nt as $fp) {
     
$fp->path = drupal_lookup_path('alias', 'node/'.$fp->nid, $fp->language);
    
$ll = language_list();
     foreach(
$ll as $cl)
            if (
$cl->language == $fp->language) { $fp->native = $cl->native; $fp->prefix = $cl->prefix; }
       
$url_parts=parse_url((string)$_SERVER['SCRIPT_URI']);
       
$fl = l($fp->native, $fp->prefix?$fp->prefix.url($fp->path):$fp->path, $options = array());
    
array_push($lList, $fl);
   }
  print
theme_item_list($lList, $title = NULL, $type = 'ul', array('id' => 'lLinks'));
}
?>

Thanks, I am going to try

Boland's picture

Thanks, I am going to try this!

More simple way

sohail_arif's picture

Below is the code for getting translation of all nodes related to current node displayed. All links will be
inserted in $link array.

  foreach ($languages[1] as $lang) {
    $class = ($lang->language == $current_language->language) ? 'language-link current' : 'language-link';
    if (arg(0) == 'node' && !$is_front) {
      $links[$lang->language] = array(
        'href'       => $node_languages[$lang->language],
        'title'      => ($lang->prefix) ? $lang->prefix : 'EN',
        'language'   => $lang,
        'attributes' => array('class' => $class),
      );
    }
    else {
      $links[$lang->language] = array(
        'href'       => $_GET['q'],
        'title'      => ($lang->prefix) ? $lang->prefix : 'EN',
        'language'   => $lang,
        'attributes' => array('class' => $class),
      );
    }
  }

For Drupal 7 there is this

kelvincool's picture

For Drupal 7 there is this method: http://api.drupal.org/api/drupal/includes!language.inc/function/language_negotiation_get_switch_links/7

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week