3

What is the best way to get absolute url with correct language prefix? The url() function accepts language parameter but it seems that it is not using it to generate the prefix - only to get translated alias. I don't care about aliases - I need correct domain prefix.

When I'm on http://en.example.com/something and use this code:

<?php
$all_languages = language_list();
url('something-else', array('language' => $all_languages['nl'], 'absolute' => true));
?>

I will get this: http://en.example.com/something-else (en prefix).

How can I get this: http://nl.example.com/something-else (nl prefix).

  • Problem is - URL doesn't know what's on the other end. It can be node, it can be view, it can be a page created by module... and most of these does not have language defined. So url is not allowed to base it's choices on the target's language. How could it when target usually does not have any? – Mołot Sep 4 '13 at 13:50
  • @Mołot but then what is the point of providing translated alias (by using language parameter) if you are still redirecting user to a different language version than the alias language? – SiliconMind Sep 4 '13 at 13:56
  • It's not so easy. There are 3 languages that takes part in the process. Language of a node, language of a GUI and language of the alias. Only the third is always available (even if it's always undefined), so only the third is taken into account by url(). I never said it's good, right? – Mołot Sep 4 '13 at 13:59
  • @Mołot I don't want the url() to know language of the target element. That is not the point of my question. I would only want it to set the prefix specified by the language parameter. After all I am providing language different than current $base_url not without a reason :) – SiliconMind Sep 4 '13 at 14:00
0

This was pulled from my theme file, but as long as you have access to $node and $language, it will do the trick:

<?php
    if (isset($node))
    {
?>
<div class="language-switcher">
        <?php
            $nodes = translation_path_get_translations("node/" . $node->nid);
            $path  = "";
            if ($language->language == "en")
            {
                if (array_key_exists("nl", $nodes))
                {
                    $path = drupal_get_path_alias($nodes["nl"], "nl");
                }
        ?>
        <a href="http://nl.example.com/<?php print $path; ?>">Change to NL</a>
        <?php
            }
            else
            {
                if (array_key_exists("en", $nodes))
                {
                    $path = drupal_get_path_alias($nodes["en"], "en");
                }
        ?>
        <a href="http://en.example.com/<?php print $path; ?>">Change to EN</a>
        <?php
            }
        ?>
</div>
<?php
    }
?>

The URL's are hard coded with the path information calculated.

  • I know that I can manually create links with correct prefixes, but that's not the point :) – SiliconMind Sep 4 '13 at 14:13
0

Apparently the url() function will create links like http://nl.example.com/something-else (note the nb prefix) BUT only for specific languages configuration. This will work correctly only if I set appropriate domain for each language here: /admin/config/regional/language.

By default the field Path prefix language code is set to language code (nl in this case) and Language domain is empty.

Now since I am using DOMAIN prefix for language selection, and not the PATH prefix, then for each language I should set language domain instead of path prefix.

For some weired reason Drupal insists on filling only one of those fields, but consequently fills Path prefix language code by default even if language detection is set to domain.

0

I format my urls like this.

 $order_url = url('user/' . $params['order']->uid . '/orders/'. $params['order']->order_id, array(
'absolute' => TRUE, 
'language' => (object)array(
  'language' => 'lang-key',
)
  ));

We can see this is reflected in the url function below.

$language = isset($options['language']) && isset($options['language']->language) ? $options['language']->language : '';
$alias = drupal_get_path_alias($original_path, $language);
if ($alias != $original_path) {
  $path = $alias;
}


}

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.