Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I've set up a multilanguage site in Drupal 7. In a template I need to add a link to a specific content node. This node has translations and path aliases like so:

node/42 = pathalias_de (german version)
node/43 = pathalias_en (english version)

In the template I'd like to call url() to get the link to the aliased version of the content node in the current language.

On the german page it should link to /de/pathalias_de on the english page to /en/pathalias_en

I've tried several variations, like calling the nid, calling the alias, passing the current language object to url().

Is this something url() should be able to do or do I need a different function? Thanks!

share|improve this question

4 Answers 4

After looking at the source of the "Content translation" module I've come up with this solution:

    global $language;
    $translations = translation_path_get_translations("node/42");
    /*
    Returns the paths of all translations of a node, based on its Drupal path:
    array(2) {
        ["de"]=>string(7) "node/42"
        ["en"]=>string(7) "node/43"
    }
    */
    print l(t('Link Title'), $translations[$language->language]); //l() will return the alias to node/42

This will output the link to the correct node alias. Works and isn't overly complicated - still wonder if there's a function that already does this.

share|improve this answer
    
Or use $translations = translation_path_get_translations(drupal_get_normal_path("pathalias_de","de")); to search for the alias instead of node id, it's more flexible. You have to pass the correct language though. –  Dooderoo Feb 27 at 8:15

Take a look at drupal_get_path_alias fuction

Given an internal Drupal path, return the alias set by the administrator.

If no path is provided, the function will return the alias of the current page.

Parameters

$path: An internal Drupal path.

$path_language: An optional language code to look up the path in.

As you can make out it takes a path_language parameter. So you could find out the current language and then pass on to this function.

Or simply use Multilink module

  1. Designed for multi-language sites. Especially relevant for sites where not all content is available in all languages, or may be made available (translated) at a later time.
  2. Allows you to easily create in-content links to other nodes, and those links automatically update to point to a suitable translated version of referenced nodes, when available.
share|improve this answer
    
drupal_get_path_alias() won't search for translations of a given node, just for aliases already in the given language. I found another workaround, see my answer below. –  Dooderoo Feb 26 at 12:00

You should probably use the link function. It allows you to pass the language as a parameter and it's the appropriate function to create a link with Drupal.

share|improve this answer
    
l() uses url() internally so it doesn't matter in this case. –  Dooderoo Feb 26 at 11:24

Using Translation Redirect you can link to nodes with language prefix.

This is what it says:

The Translation redirect module redirects anonymous users (including web crawlers) to the translation of the page in the requested language, if it exists, using a 301 redirect code. For example, if the request is for /de/node/23 and node/23 corresponds to an English page, the user will be redirected to the German translation of the English node if it exists. If the translation does not exist, then the source node content is displayed.

For example if nid 22 is for English version and it has French and German ones then you can simply link to them as fr/node/22 and de/node/22. This 22 can also be any nid of the translated version.

If particular translation is not found for example if you've not added for Japanese, then ja/node/22 will give 404 for Anonymous users.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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