Thursday, May 27, 2010

Internationalizating CakePHP Sites

Making an app easy to translate and could show it in different languages is a simple task in CakePHP, CakePHP brings a tool and a set of functions that help the developer on this job

The first that has to be done is to use the function __() when writing strings that we want to be translate. this function has the next params:

        __($stringToTransalte, $boolean);
        $stringToTranslate: is the string we want to be translated
        $boolean: indicate whether the string is only returned or also echo, false to echo the string



After you have to generate the .pot file or files, you can use the tool cake that may be found in the folder cake/console/, adding the param "i18n", like this:

        ./cake i18n

You select the E from the showed options, this will search for all the usages of __() function on the application files, and will generated a .pot file or files(you set wherter to generate a unique file or separated files), this file will be the source to make the .po file whit all the translations.

This file will be saved in the folder, app/locale/spa/LC_MESSAGES/, the 'spa' string stands for the language to translate, this following the ISO 639-2 standard.

When translating is adviced to use po translating software, to avoid problemas like bad characters. You can use PoEdit.

To translate some special strings like the months, you can use the __(), whit the string without echoing it, so the search made by ./cake i18n will find this translations, and when you translate them the tranlation will be applied for all the application.

I hope this post to be useful, thanks

No comments:

Post a Comment