Multilingual WordPress

Tuesday, November 1st, 2005 - Español English

Running a WordPress weblog in several languages simultaneously, means translating and coordinating several components in WordPress. Here are some ideas and solutions to this…

The things to be taken into account are:

Wordpress’ code

The error messages, control panel and texts which are part of Wordpress code may be easily translated thanks to the fact that Wordpress uses a PHPized version of gettext, the GNU/Linux standard on multilingual code. The translation process consists of:

  1. Mark every string in the code and templates as translatable, by wrapping them with functions __() and _e().
  2. Process every file with the tool xgettext, which produces a .pot (Portable Object Template) text file on which translators can work.
  3. Every translation is saved to a new file of the same format, but different extension: .po (Portable Object)
  4. These are then compiled by the tool msgfmt into a .mo (Machine Object) file, which is ready to be saved by the users under wp-includes/languages.

There’s a lot written on this: at the wordpress wiki there’s a complete descripction of the process, at the WordPress support page there are some comments and gettext’s documentation has everything you need, just to name a few docs. As usual, Google is of great help.

So, having all the code translated is as easy as getting the right translations file in .mo format and copying it in wp-includes/languages/ with the right name. Once this is done, just edit wp-config.php modifying the line which reads

define ('WPLANG', '');

to define WPLANG to hold our favourite language:

define ('WPLANG', 'es_ES');

It’s important that the .mo files and WPLANG follow the same naming convention; i.e.: for the spanish spoken in Spain: es_ES (followed by the file extension, of course), although if you are going to use Polyglot, as I suggest later, this convention changes.

Templates’ code and text.

I use a template very similar to WP’s default, which is in its turn based upon the Kubrick theme, but in its 1.5.2 version, it’s not ready to be used out of the box with gettext so you have to download a different one.

There were a couple of problems with this “international” version, though they’ll certainly be quickly solved. For instance, there’s a typo at lines 62, 63 of the file wp-contents/themes/default/comments.php:


<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
$redirect_url = get_option('siteurl').'/wp-login.php?redirect_to='.get_permalink();

should obviously read:


<?php if ( get_option('comment_registration') && !$user_ID ) :
$redirect_url = get_option('siteurl').'/wp-login.php?redirect_to='.get_permalink(); ?>

Once the files copied, new translations of the .mo have to be copied into wp-includes/languages/.
This new files should hold all the new strings and translations, but, unfortunately some are missing in the versions I found. If you want to download mine, you can here, it’s the one being used right now, so it might have things you don’t need. The .po file is also available for download.

Posts and pages

Posts should be translatable too. In order to have this, I use some tags from Polyglot, a very useful WP plugin.

Though the early Polyglot versions required a bit of effort (like hooking to many filters, etc.), the one I use, 0.6, not only translates posts, titles and categories, but it also selects the language via WPLANG from a selection made by the user or the browser’s configuration, so the language for the whole WP is changed.

But, if you are going to make use of the gettext translations, it is important that they be named not as they should (for instance, es_ES.mo), but using Polyglots scheme, which is two letters after the ISO639 standard.

To input the text in several languages using Polyglot, a special tag is introduced.

<lang_es>Here goes the english text</lang_es>
<lang_en>Y aquí el texto en español</lang_en>

Titles and category names can also be translated, though for the latter you’ll probably have to ALTER the category_name column in the database to hold more than the default 55 characters.

The comments

This is the only unsolved part for the moment. It should be easy to use the currently selected language configuration to automatically add the right tags to any comments posted, but the comments should be preprocessed before displaying and counting not to take into account those for another language. Of course it might be desirable to see them all, so there should be messages like “There are no comments in spanish, but there are 3 in french”, with “french” being a link to change the language.

As you can see, I haven’t implemented this yet. Maybe one day… ;)

5 Responses to “ Multilingual WordPress ”

  1. Guy Bouchard Says:

    Do you know how can i translate Archive title ?

    I always got french Archive title like “octobre 2006″ since my blog is localized in french.

    I use polyglot and it works pretty good but i can’t seen to find how i can make it switch between Archive title in french and english.

    Let me know.

    Thanks!

  2. Guy Bouchard Says:

    I have seen your Archive title are translating between spanish and english.

    Could you tell me how you achieve it?

    It would be really helpful for me!

    Thanks gain!

  3. nonick Says:

    Hi,

    I have checked your site (guybouchard.ca), and if I understand your question well, you want two things:

    (1) Translate the dates in your monthly archives listing (the list displayed on the left sidebar of your site) to read “October, 2006″, instead of “Octobre, 2006″.

    WordPress (at least the version I use, 1.5.2) uses an internal array $month defined in wp-includes/locale.php to store the names of the months which are used everywhere in the site (and in our case in function get_archives(), in wp-includes/template-functions-general.php). This array holds gettext-translatable strings of the form __('January'), etc.

    In order to have these translated, you have to add them to your fr_CA.po (or whatever it is called) file. Do that by adding stuff like the following to the file:


    msgid "January"
    msgstr "Janvier"

    msgid "February"
    msgstr "Février"

    Actually the untranslated strings should already be there since they are supposed to be tagged as translatable by means of the __() in the code, and thus should have been extracted by xgettext. If they are not tagged, you’ll want to add the necessary calls to __().

    Once you are done with the translation, you’ll need to call msgfmt upon that file, then upload the result to your the wp-includes/languages/ directory as fr.mo.

    Alternatively you may want to look for a translations file with the strings already there ;) .

    (2) Translate the category names (?). This I deduce from the fact that they are not translated on your site.

    Since I have added many little changes (and kept no log nor used any version control, shame on me) I’m not sure this is all you’ll need, but try to add the following lines to your wp-content/plugins/polyglot.php file:


    add_filter('category_description', 'lang_picker',1);
    add_filter('category_long_description', 'lang_picker',1);

    Now the usual <lang_xx> tags for category titles will work for the sidebar as well. You’ll probably also want to increase the size of the category_description column in the database.

    I hope this helped you…

    Cheers!

  4. Guy Bouchard Says:

    The translation of month is already working BUT i always have the french name like octobre . The viewer choose english language and i still get french month name in my get_archive().

    Your website seem to switch month name between language fine.

    I supposed i have to modify something else but i don’t know what!

    Thanks for helping.

  5. nonick Says:

    Hmm… I’m kind of clueless, but:

    In polyglot.php: is your $polyglot_settings['lang_change_locale'] = TRUE;? This tells polyglot to change the global WP language setting…

    And this one is a bit unrelated, but given any big enough piece of software one never knows:

    In wp-config.php: is your WPLANG variable set to anything? It should be with a line like define('WPLANG', 'en');, though that shouldn’t change much, since polyglot defaults to some language and your problem seems to be with WP localisation…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>