Drupal – Force Path Prefix for Default Language

Update: 2012-04-18: This solution is for Drupal 6 and it hacked the Drupal core. So please apply it with extreme caution. For Drupal 7, please try the approach suggested by outsourcer.

When you have more than one language on Drupal site, you have to define the default language among one of them. For example, if i set English as the default language, it will be shown in both http://<domain_name>/ and http://<domain_name>/en. but all the links in the default language will not have the en path prefix.

So if you want to force the default path prefix in all links. The following trick can do for you. Open the <drupal_root>/includes/language.inc and edit the language_url_rewrite() function as follow.

...
function language_url_rewrite(&$path, &$options) {
  global $language;

  // Only modify relative (insite) URLs.
  if (empty($options['external'])) {

    // Language can be passed as an option, or we go for current language.
    if (!isset($options['language'])) {
      $options['language'] = $language;
    }

    switch (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE)) {
      case LANGUAGE_NEGOTIATION_NONE:
        // No language dependent path allowed in this mode.
        unset($options['language']);
        break;

      case LANGUAGE_NEGOTIATION_DOMAIN:
        if ($options['language']->domain) {
          // Ask for an absolute URL with our modified base_url.
          $options['absolute'] = TRUE;
          $options['base_url'] = $options['language']->domain;
        }
        break;

      case LANGUAGE_NEGOTIATION_PATH_DEFAULT:
        /* Comment the following codes to enforce path prefix */
        /* START
        $default = language_default();
        if ($options['language']->language == $default->language) {
          break;
        }
        // Intentionally no break here.
        END */

      case LANGUAGE_NEGOTIATION_PATH:
        if (!empty($options['language']->prefix)) {
          $options['prefix'] = $options['language']->prefix .'/';
        }
        break;
    }
  }
}

 

Done =)

Reference: Links not rewritten to include prefixes with ‘Path prefix only’ language negotiation

11 thoughts on “Drupal – Force Path Prefix for Default Language”

  1. I’m not very sure that edit and modify drupal core module is a good way (unless is the only way you can have, obviously :P).
    I prefer to write custom module that can override some default behaviour and put it into my profile folder. In this way I can apply my profile to more than only one instance of Drupal.
    D’you know a way?
    I’m having some issues using drupal languages during development of a site that needs to be translated in english and in italian, and I found that you may have far fewer problems if you use english as default language.
    I’m searching a “clean way” to force english to be the default language but have a path prefix, and italian to be the secondary language but do not have path prefix. It’s a challenge i think…

    Like

    1. Yes, i think modifying the core code is the last resolution. So the tips in this post should be used with great care.

      Drupal is so powerful and convenient but at the same time it is not easy to customize it.

      By the way, what default behaviour you want to modify? can you list a few?

      And thanks for your comment. =D

      Like

    1. Hi Boshard,

      you can following this post to modify the /includes/language.inc your Drupal 6 installation. but as mentioned by g0blin79, it is not a good approach becoz we are modifying the Drupal core files. Use it with great cares.

      Kit

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.