Language specific menu links hack

UPDATE: This post applies to Drupal 5, in Drupal 6 it doesn't work. An alternative is to use full URLs in the menu, like "", but that might not be acceptable in a given situation.

This is one of those hacks that just look wrong, but seems to work all-right.

Goal: Linking to a language specific path in a menu.

Problem: Just adding the path 'en' to a menu to create a link to the homepage, causes i18n to first strip the language prefix (if the path has one) in order to get the real path, and then append the language code for the active language. So if we're on a, say, danish page, it ends up as 'da' in the menu, defeating the purpose.

Solution: Double the language prefix like so: 'en/en'.

Why/how does it work: I18n will still strip the first language prefix, and then use the rest as the 'real' path, but it wont add a language prefix to a path that seems to have one. So it leaves the second 'en' in place when the menu is generated, making the link work as expected.

Disclaimer: I haven't done thorough testing of the hack, so it might break on some configuration or module combination. But from what I've gleaned from the code, I can't see why it should break.

Blog tags: