Translating Views paths in Drupal

Drupal offers great multilanguage possibilities for your website. Drupal core already offers a way to translate the website interface AND your content (nodes) in multiple languages. Enabling multiple languages is a piece of cake and translating content is made very simple. However, you need to perform some extra tricks to get the full interface translated. For example: you might want to translate your taxonomy terms and your menu items as well. Or, also very common, using some blocks or menu-items just for a single language only. One of the great community modules that helps you achieving this goal is the Internationalization module (i18n for friends).

In the past years, I've made several multi-language websites. And until today, I never found a good way to translate paths made by the Views module. For example: if you have an path like www.domain.com/news, you'd probably want the translated versions to be www.domain.nl/nieuws (Dutch) and www.domain.fr/nouvelle (French), instead of www.domain.nl/news and www.domain.fr/news.

I used Google a lot, but never found a good solution. In the past, I always made a copy of my existing views, and entered a specific path for each language. Which made maintenance a crime, as every update to the views now should be done once for each language. That ended today, as I spotted this comment by our beloved merlinofchaos (the author of Views, Panels and Choas Tools), in which he said you just could use the default Drupal core module URL alias for this.

And it worked! Really.. I thought I've tried it before in the past without success, but to no avail. So easy, and so useful.

So.. what should you do to use one Page View for several paths? Easy:

  • Switch to the english interface (good practice if your not a native English speaker): ALWAYS create strings in English and translate them afterwards. Even if you don't plan to use more languages that your own: you might change your mind in the future, and then you're screwed.
  • Create a Page View and enter the Title and Path in English. Eg: Latest news and 'updates/news'.
  • Save your view
  • Navigate to your newly created view, so Drupal adds the strings to the translation table.
  • Then go to Admin > Site Building > URL aliases (admin/build/path) and click Add alias
  • In Existing system path, enter the URL you just created (eg updates/news).
  • In Path alias, enter the URL you want in the other language (eg actueel/nieuws for Dutch).
  • Make sure you set the language to the adjoining language (eg Dutch) and press Create new alias.
  • Repeat the last three steps for every language

That's all it takes to create multiple URLs for each language! No need to create multiple views, no need to create your own module, and no need to fiddle with hooks, theme functions or hook_menu_alter. Merlin: I owe you a beer @ DrupalCon CPH! Thanks mate for being so helpful on all those issues!

Next post

DrupalCon Copenhagen - why you should come too!

Read More »

Comments

Thanks!!!

Thanks for your solution! It's very helpful for me!
I also found the module Views Translation, but for that Views 3 is required.

Doubt on step 4... one page view per language?

Hi, I've read your small step by step tutorial and got stuck on step 4. What do you mean by "Navigate to your newly created view to add the strings to the translation table."??

I kept going and I've created several page views, one for each language on my website, but I always get mixed URLs or Views titles, i.e, instead of the expected result:

EN:
| URL: /news
| View title: News

PT:
| URL: /noticias
| View title: Notícias

I get links like "/en/noticias" or with the right link "/news" the title "Notícias" shows up.

Any idea what I might be doing wrong here?

Thanks in advance.

I changed the text

Thanks for your comment. What I meant to say was 'Navigate to your newly created view, so Drupal adds the strings to the translation table.'

By visiting your view, Drupal adds the Views title to the translation table. This step is not needed to translate the URL, only to translate the title of the view.

Alternative solution: two menus with translated system variables

Hi everyone!

I just spent a week figuring out how to do translated menus properly. I want to share this solution on a few pages I did NOT find the solution on.

There where always problems with either menu items with the same path for both languages (like /contact), which required a single, translated menu item, and items with different paths for the two languages (like node/10 and node/11 for the translation), which required two seperated menu items, one per language. There are situations where you can't combine the two approaches.

Here's a third one. Instead of having two different menu ITEMs, have two different MENUs. Select which one to use for each language by specifying that "menu_primary_menu" system variable is multilingual. Problem solved. Everything works fine for me.

See:
http://drupal.org/node/134002
http://becircle.com/translating_menus_drupal_6

Hope this helps...
Ingo

It doesn't work for path with arguments

Hi, I've already thougth that and I tried but it doesn't work when the URL has arguments like /products/[category name]
I could make an alias for each category but the're hundreds. any idea.

Thanks for your post!

Solution for views

I've found, I think, a elegant solution to translate views path when we've got arguments. So we need kind of URL alias with mapped wildcards.
This is for Drupal 6 and I used the custom_url_rewrite_inbound function.

In settings.php I wrote the following:

$conf['url-views'] = array('productes' => array('es' => 'productos','en' => 'products', 'fr' => 'produits'));

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
global $conf;

if (isset($conf['url-views'])) {
foreach($conf['url-views'] as $key => $url_view) {
if (preg_match('|^'.$url_view[i18n_get_lang()].'(/.*)|', $path, $matches)) {
$result = $key . $matches[1];
}
}

}

}

What it does is when url is "productos/[arguments]" the function rewrites url to "productes/[arguments]" but it's not a redirection and the original URL stills, good for SEO purposes.

I made this function to use $conf variable so you can configure to use multiple view paths for multiple languages.

I hope it helps!

http://www.dddesarrolloweb.com

Thanks so much!

But i found out that there is no need to create strings in english and translate them afterwards. I createt all strings in german and then i translated them into english.

Works perfect!

Now there's a module for this

This solution works nicely, thanks. I've also found a (relatively new) module for this: i18n Page Views. It creates a new Views page display type allowing you to specify a different path for each language.

It looks like there has been a recent commit for the 6.x version that makes it work with arguments, so hopefully that patch will be ported to 7.x shortly. Seems like that would be an ideal solution for D7 - if I can get multilingual Calendar paths I will be a happy camper.

Yes, but then what to do, when you have a menu?

Hi namebrother!  (we are a rare specie)

I was so happy when I found your blog post. Solved a lot of if's and ???. Now my views pages correponds to URL's, showing the correct content. But only for views WITHOUT  a menu link pointing to them. Somehow the Pathauto (or Views, or menu system) has issues, when in combination. I cannot figure this out!

Do you have any tips to share?

Kind regards,

 - Christian

Don't set your menu link in Views

Hi Christian.

Cool last name!

That's a weird issue you describe. I always set my menu items using the menu system and not from within Views. With i18n_menu enabled, you can select a language per menu item. 

Canonical meta tag

Great tip! Thanks for sharing! Made my day :-)

But... how about canonical meta tags? URL alias doesn't generate by itself (D7.28). How can that be achieved?

Thank you!

Drupal has made a trend in

Drupal has made a trend in the industry within a short span of time as a very flexible platform with

amazing features. The multilingual support in Drupal enables a lot of users to deal with this platform much

easier. Thanks for the info on translating Views Paths in Drupal.

EHR