Drupal coder

Drupal module for using the same clean url for multiple languages

I am using the Drupal i18n module for content translation. Although it sometimes isn't as straight forward in usability as it should be, mostly there's a workaround for a specific problem.

I couldn't find one myself for a certain problem I was having with Drupal clean urls. So I decided to write a simple module for it myself.

On a typical site, you have pages like '/contact', '/help', '/faq', ... that you all want to share the same path for each language (like '/fr/contact', 'en/contact', 'nl/contact', ...). You also want to have a single access point like '/contact' that points to the right translation of that page depending on the current user language.

By default, this is not possible using the i18n module. At least, there's no great solution that I know off.

My workaround is the following. For each language I add the language prefix to the clean url. So when creating my page (or whatever other node type instance), I specify 'fr/contact' and 'en/contact' instead of '/contact'.
This works great for all language specific clean urls; 'fr/contact' points to the french version etc.

But I also want the language independent url, '/contact', to point to the translation of the page in the current language. For this, I had to write a simple module, from which the code is listed below.
This content checks all your clean urls and looks for urls that are prefixed with a language. Those urls get a menu item in the menu hook. This menu item triggers a callback which redirects the user to the page with the right translation. Problem solved!

The disadvantage of this solution is that the user isn't taken straight forward to the requested page, but is rather redirected to it. So the url changes ('/contact' becomes for example '/fr/contact'). Not a big problem for me though ;)

/**
 * Implementation of hook_menu().
 */
function multilingual_path_menu($may_cache) {
  $items = array();
  
  if (!$may_cache) {
  
    $sql = "SELECT DISTINCT SUBSTR(dst,4) as dst FROM {url_alias} WHERE ";
    $languages = i18n_supported_languages();
    foreach($languages as $key => $name) {
      $subsql[] = "dst LIKE '".$key."/%'";
    }
    $sql = $sql . implode(' OR ', $subsql);
    $result = db_query($sql);
    
    while($path = db_fetch_object($result)) {
      if(!drupal_lookup_path('source',$path->dst)) {
        $items[] = array(
          'path' => $path->dst,
          'title' => '',
          'callback' => 'multilingual_path_get_path',
          'access' => TRUE,
          'type' => MENU_CALLBACK
        );
      }
    }
  }

  return $items;
}

function multilingual_path_get_path() {
  drupal_goto($_GET['q']);
}
AttachmentSize
multilingual_path.zip1.98 KB
April 03, 2008Drupal, i18n, url rewriting

Comments

The zip is available at the bottom of the post under the Attachments section.

I'm not seeing the zip. Would you be so kind to provide it? I'd really like to try it out as I found that my path aliases simply didn't work with clean urls and had to disable the "clean url" feature. This would at least provide a workaround.

Hi, I have just added the module as a zip file. I haven't tested it though but it should work fine. Good luck!

Hi, i just found this thru Google.

is this code available for download in module form?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options