0

I have a custom template for my front page, where I have to manually set URLs. Some of them are Views paths (such as news), some - aliased node URLs (projects/project-1). How to get their path translation URL in raw PHP code?

If I say something like:

<a href="<?php print url('news', array('language'=> $langcode, 'alias' => TRUE)) ?>" ...></a>

the rendered link is invalid langauge-crossed, such as mysite.com/es/news.

I tried also with the l() function but I'm getting untranslated link pointing to the root mysite.com/news.

2 Answers 2

1

This might be an obvious answer to experts, but hopefully this will help some newbie someday. I painstakingly investigated the url stuff in Drupal and my findings surprised me - especially when I referred them to the docs.

In the tpl I placed the following nasty debug lines:

<?php dpm(drupal_get_normal_path('alias-en')) ?>
<?php dpm(drupal_get_normal_path('alias-en', 'es')) ?>
<?php dpm(drupal_get_normal_path('alias-en', 'en')) ?>
<?php dpm(drupal_get_normal_path('en/alias-en')) ?>
<?php dpm(drupal_get_normal_path('/en/alias-en')) ?>
<?php dpm(drupal_get_normal_path('es/alias-es')) ?>
<?php dpm(drupal_get_normal_path('/es/alias-es')) ?>

And the results were:

alias-en
alias-en
node/298
en/alias-en
/en/alias-en
es/alias-es
/es/alias-es

Only the third result (node/298) was returned as desired, good for passing to i18n_get_path_translations. Even though the drupal_get_normal_path() function docs say the language parameter is optional, and subsequently called drupal_lookup_path() is said to put current language if NULL is passed, it does not seem to work this way.

drupal_get_normal_path() seems to require that you pass two parameters: alias and the language the alias is set for. Only then you'll get the result that will work reliably with i18n_get_path_translations.

I'm close to thinking it is a bug in Drupal or docs, and if no one sheds light on me, I'm going to file an issue.

0

You should use l function with language options to protect links against language prefix, like this one mysite/en/node/etc/1 for example:

<?php print l(t('title'), $path, array('language' => 'en')); ?>
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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