Localising WordPress categories

local-fr_FR.po → local-fr_FR.mo

I’m using the xili language plugin to handle the multi-lingual aspects of this blog. While the plugin is hardly user-friendly I managed to make it work, and built a child theme that patches the Sixteen theme so it works properly with multilingual data.

The next step was localise post categories. In theory, I could have used the xili dictionary plugin, but that one is even more confusing, so in the end, I decided to just write the localization files (so-called gettext files) by hand. While this sounds hardcore, it really isn’t.

Do do this, you really only need a text editor and a tool to generate .mo (machine readable) out of .po (portable) files. Here I’ll use msgfmt which is available on most Unix installations.

Basically, your .po file is a list of translation from a source language (typically english) to some other language. The file that explains how to translate my categories looks like this:

msgid ""
msgstr ""
"Project-Id-Version: Sixteen-child\n"
"Report-Msgid-Bugs-To: root@localhost\n"
"Last-Translator: Matthias Wiesmann <root@localhost>\n"
"Language-Team: Matthias Wiesmann <root@localhost>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"Language: fr_FR\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"

#
msgid "Stories"
msgstr "Histoires"

The only interesting line in header is the one that specifies that the language for this file is fr_FR, the last two lines explain how to translate Stories into French: Histoires.

This file should be named local-fr_FR.po and placed into the languages directory of your theme. WordPress does not understand .po files directly, they need to be compiled to .mo format, this is done with the following command:

msgfmt local-fr_FR.po -o local-fr_FR.mo

12 thoughts on “Localising WordPress categories”

  1. Hi Matthias,
    I am thinking about doing something similar, and wanted to ask your opinion, since you seem to have experimented with some mulitlingualization plugin. I want to have the following features:
    * auto-detection of the browser language, and change of the interface/theme language
    * possibility to translate tags/categories
    * by default all articles in all languages are shown, but users can select articles in a specific language

    What I don’t need (or not really) are translations of articles, I will probably not translate any article, just write them in different languages.

    What do you suggest in this case?

    Thanks

    Norbert

  2. This is roughly, the use case I have, with xili I managed to get the following working:
    • translation of categories
    • translation of the chrome (all the stuff around the article).
    • ability to select articles in one language
    • Some articles are defined as having no explicit language (multi-lingual stuff).
    Language selection is either explicitly chosen or based on the content more than the user settings, i.e. if you look at a french article (alone) then the language is set to French.
    I have very few articles translated (basically the about Thias one).

    • Thanks, so I will try to see if I can get this working out. I’ll let you know my experiences.

  3. Hmm, that works nicely for categories, but unfortunately not for tags, which I am using. I need to dig a bit more into that, maybe I can hack the theme I use for that, but I am not sure, considering that the tag cloud for example is a WordPress internal feature.

  4. I know, my hunch is that the tag cloud is implemented by Jetpack, and the plugin has trouble with everything Jetpack related.

  5. Indeed. I have managed to get more and more items translated by fixing for example the simple-links plugin. I also have changed my (child) theme so that the tags are translated on the normal single page output, next I will check how to fix the tag cloud and calendar.

    But the tag cloud, isn’t that a WordPress internal thingy? I thought it is not coming from jetpack. Anyway, I will dig into the code and see what can be done. I will write all my changes up in due time.

  6. Haa, got it working by translating the tags in the `tag_cloud_sort` filter. The code I use is:

    ““`
    function translate_instead_of_sort($tags) {
    foreach ( (array) $tags as $key => $tag ) {
    $tag->name = __( $tag->name , ‘mistylake’ );
    }
    return $tags;
    }
    add_action(‘tag_cloud_sort’, ‘translate_instead_of_sort’);
    ““`

    Of course you need to change the textdomain parameter.

    See http://www.preining.info/blog/?lang=ja for how far I have come ;-)

  7. That sounds really cool, I would be thankful if you wrote a blog entry and/or a patch explaining which files needs to be changed how.

Leave a Reply to NorbertCancel reply

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