Drupal: localized node url

Example

We have:

  • English node, nid = 1, url = /home
  • its French translation, nid = 2, url = /fr/accueil

The function below, url_node(1) will return:

  • on English page: /home
  • on French page: /fr/accueil

Code

function url_node($nid) {
	global $language;
 
	// get node tnid
	$result = db_query("select * from {node} where nid='" . $nid . "'");
	while($data = db_fetch_object($result)) {
		$tnid = $data->tnid;
	}
 
	// get node with matching tnid and language
	$translated_nid = '';
	$query = "select * from {node} where tnid='" . $tnid . "' and language='" . $language->language . "'";
	$result = db_query($query);
	while($data = db_fetch_object($result)) {
		$translated_nid = $data->nid;
	}
 
	// if no node, use original nid
	if($translated_nid == '') {
		$translated_nid = $nid;
	}
 
	return url('node/' . $translated_nid);	
}

Why

Drupal url() is not good enough.

url('node/1') will return

  • /home on an English page
  • /fr/node/1 on a French page (incorrect)

url('node/2') will return

  • /node/2 on an English page (incorrect)
  • /fr/accueil on a French page

Limitations

  • it seems impossible to get an English url on a French page and vice-versa, whichever nid (French or English) is used.

Feedback

The Best Designer Replica Watches are on Sale. We provide the hottest New Replica Watches.<a href=" http://www.wholesale18.com">Replica Rolex Watches</a> | <a href="http://www.wholesale18.com/replica-watches-cate-129.html">Cheap Watches</a> | <a href="http://www.wholesale18.com">Discount Watches</a> | <a href=" http://www.wholesale18.com">Replica Watches Sale</a> | <a href="http://www.wholesale18.com/replica-handbags-replica-hermes-handbags-cate-21_41.html">Replicas Hermes</a> | <a href="http://www.wholesale18.com/replica-handbags-cate-21.html">Replica Bags</a> | <a href="http://www.wholesale18.com">Cheap Bags</a> | <a href="http://www.wholesale18.com/replica-men-shoes-cate-23.html">Replica Shoes</a>.No sales tax&free; shipping.100% Quality Assurance,100% Price guarantee! what do you thinking about? hurry up.
replica brand watches
Jan 14, 2012
#1