I want to display a link to the spanish version of a page in my template, but another module is removing the language option on my link function:

l('Visit this page in Spanish', "node/{$node->nid}", array( 'language'=>'es' ));

I have the general routing set up the way I want; most of the time I want links to target the current language. Except this time - now I really want that link in spanish : )

How do I force l() to keep the language I specify? Or should I use another function?


It should work when using this function: https://api.drupal.org/api/drupal/modules!translation!translation.module/function/translation_node_get_translations/7

This will return all the translations of a given nid, when an ES translation is given you can print your l().

if ($node = menu_get_object()) {
  // Get the tnid
  $tnid = $node->tnid;
  $trans = translation_node_get_translations($tnid);


And inside $trans array you'll have basic information of all translations (including current language) - title, language, nid, status... But of course, you can use nid to load whole node if you need something more.

  • This is basically the same answer as this one from 2012 - any reason to post the same method again? – Mołot Sep 9 '15 at 12:18

If you want to get a specific page URL in a specific language URL use this code:

$languages = language_list();
$english_page_url = l(t('About'), 'node/22', array('language' => $languages['en']));
$arabic_page_url = l(t('About'), 'node/22', array('language' => $languages['ar']));

For your example you can use:

$languages = language_list();
l(t('Visit this page in Spanish'), 'node/' . $node->nid, array('language'=> $languages['es']));

Because you have to pass the language object not only the language prefix!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.