Keeping Original Node Path in a Multilingual Drupal Site

What we do

Let’s say you have to make a multilanguage Drupal website… Besides English it also has to be available in Chinese and Japanese. Drupal makes it really easy for you to set up the basics. Using the Locale and I18n module, most of your site can be translated with a few clicks of your mighty mouse.

The problems come when you are trying to use pathauto in combination with placing the node title ([title-raw]) as path. You will be ending up with Chinese and Japanese characters in your URL. Most modern browsers do support this, but it’s still not a pretty sight and who knows what software/OS does not support it yet.

A solution might be typing Pinyin (for the Chinese version) in your paths… But this can be a long and tedious job (not to mention how many times your client will screw up or forget!).

Pathauto offers a relatively easy solution for our problem. We can “force” Drupal to use the original node paths for the translations too. This will not cause any conflicts as all translations are prepended with their ISO code. Just add the following code to one of your custom modules!

/**
 * Because we want to use our original paths, prepended with the language identifier
 * We are going to assume the default language is english!
 *
 * @param $alias
 * @param $context
 */
function mymodule_pathauto_alias_alter(&$alias,$context) {

	if($context['module'] == 'node' && $context['language'] != 'en') {

		$node = node_load($context['entity_id']);
		if($node->tnid > 0 && $tnode = node_load($node->tnid)) {
			$alias = $tnode->path;
		}

	}

}

2 thoughts on “Keeping Original Node Path in a Multilingual Drupal Site

  1. lurkerX

    Hi Thomas,

    This worked like a charm! Thank you so much for the post!

    Steve, this goes in a custom module. And you would update the function name “mymodule_pathauto_alias_alter” where is has “mymodule” to be the name of your module. For more about custom modules check out this link:
    http://drupal.org/node/416986

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>