2010-10-30 08:28:13

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;