Drupal 6 with i18n basics

Notes from a talk on how to use i18n with Drupal 6 for people who have never done it before, presented at DrupalCamp Montreal in October 2008.

Start with this page from the handbook: Tutorial - Building a multi-language site. You have the choice of switching based on path prefix (with optional browser language detection when no prefix is specified) or domain name.

Other useful modules:

  • Language Icons (prints flags beside language names)
  • Views translation (language-related filters, fields, arguments, relationships) (packaged with Views)
  • Translation Overview (summary page of translatable nodes)

Use Administer / Content management / Translation overview to see which nodes require translation, and Administer / Site building / Translate interface to translate individual strings (e.g. a CCK field name).

Go to Administer / Content management / Content types / Edit / Workflow settings to choose if a given node type will be translatable, and Synchronize translations under that for keeping things like author name and published status synchronized. CCK fields may be synchronized here as well.

Translating taxonomies

There are so many use cases... you can have separate vocabularies per language, shared vocabularies but separate terms per language, shared vocabularies with shared translated terms, or shared vocabularies with untranslated terms.

Translating menus

Menus aren't really translated now. A given menu item can be displayed in all languages or just one of the active languages. This is more useful than it appears since menu items set to "all languages", but corresponding to nodes that have a language set, are only displayed when the current language is active. However, you basically either need to:

  1. enter everything into each menu twice
  2. create independent menus for each language

You can deal with the special case of primary and secondary menus because all variables may be set per language. Put this in settings.php

/**
* Multilingual settings
*
* This is a collection of variables that can be set up for each language when i18n is enabled.
* These are the basic ones for Drupal core, but you can add your own here.
*/
$conf['i18n_variables'] = array(
// Site name, slogan, mission, etc..
'site_name',
'site_slogan',
'site_mission',
'site_footer',
'anonymous',
// Different front page for each language
'site_frontpage',
// Primary and secondary links
'menu_primary_links_source',
'menu_secondary_links_source',
// Contact form information
'contact_form_information',
);

Next, create the primary menu for each language, switch to that language, and make the appropriate menu into the primary menu at Administer / Site building / Menus. Note that this won't work unless you switch to the language whose settings you wish to change before making the change.
I say these aren't really translated because, for example, if you want to move a submenu item from one parent term to another, you have to do this with each language. Imagine rearranging a menu of 50 items each available three languages! This is planned for the future, but for now an ugly workaround is available (use at your own risk...)

How to switch on language in custom modules and templates

Pass strings through the t() function to have them processed by the string translation database (you can also pass arguments). To do this manually:

global $language;
if ($language->language == "fr") {
print "bonjour!";
} else {
print "hello!";
}

How to print dates, numbers, etc., using locales from templates

Put this in your template.php file:

// set locale based on language for every page
$locales['fr'] = 'fr_CA.UTF-8';
$locales['en'] = 'en_CA.UTF-8';
global $language;
if ($language->language == 'fr') {
setlocale(LC_ALL, $locales['fr']);
} else {
setlocale(LC_ALL, $locales['en']);
}

Now this will work in your template and module files:

print strftime('%d %B %Y', $my_calculated_timestamp);

Update: I've often used strftime() because it's not really possible to use the same date format strings in all languages. However, an even better approach is to set the date format variables separately per language. Here are the variables to include in the above block of code in your settings.php file:
date_format_short
date_format_short_custom
date_format_medium
date_format_medium_custom
date_format_long
date_format_long_custom

Update 2: I realized that month names weren't being translated properly during my demo because of a bug in Date API, but this has been fixed in CVS.

Drupal 6 i18n Examples

En Marge 12-17 (French-English switching on path prefix)
NDP.ca and NPD.ca (French-English switching based on hostname)