Even if the documentation is quite complete, I find that its better to have a “step-by-step” basic guide for internationalizing your pages. So here it is:

Step 1 – Prepare your code:

You need to use the __() function to translate your text, so instead of doing something like:

<h1> Hi, my name is <?php echo $name;?></h1>

It should be:

<h1><?php echo __("Hi, my name is %s",$name);?>

Step 2 – Create your pot file:

Using the bake command, cakephp will read all your source files and it will extract all the text to translate. So it’s very important that you write the string in the i18n function and don’t pass just variables. For example:

//This will give you a headache
if($isConnected){
$message = "hello";
} else {
$message = "login";
]
echo __($message);
//using bake, you wont be able to extract the two strings, "hello" and "login", and they wont be present in your pot file.

So, to extract the text, using the command line go to you app directory, and execute the i18n command:

$ cd /var/www/mysite/app
$ ./Console/cake i18n

You’ll see:

Welcome to CakePHP v2.0.6 Console
---------------------------------------------------------------
App : app
Path: /var/www/mysite/app/
---------------------------------------------------------------
I18n Shell
---------------------------------------------------------------
[E]xtract POT file from sources
[I]nitialize i18n database table
[H]elp
[Q]uit
What would you like to do? (E/I/H/Q)

Choose “E”, to extract the text. Now you’ll see:

What is the path you would like to extract?
[Q]uit [D]one
[/var/www/mysite/app/] >

here, cake is asking you where are your source files located, by default it will use the app folder and it will search recursively all the source files. However you might have some vendor libraries (or even a third party software like wordpress) installed on your app folder, so usually I only tell cake to extract the strings from the “View”,”Controller” and “Model” directories. But to make it simple.. let’s just use the app folder, so select “D” then press “enter”

Cake might ask you a few more questions,like if you already had a pot file or if you’re working with different domains. I’ll leave that to you. In the end you need to have the default.pot file inside app/Locale/

Step 3 – Translate your code using poedit

Download and install poedit then go to: File->New Catalog from POT file. And select the default.pot file you’ve just created. Fill in the form to add some information about the translation (if you have the time to do it XD) and now poedit will ask you where you want to save your .po file. You should save it in:

/app/Locale/<locale>/LC_MESSAGES/default.po

where <locale> is the three-character locale code. Go check the doc on cakephp for more info.

Step 4 – Localization in Cakephp

RTFM!

Step 5 – Updating your pot files:

Because we are not perfect, (or because the client ALWAYS wants to change everything), you might need to change/add/delete some of the source text So basically you need to repeat the step 1 and 2. That is: parse and extract all the strings in your source files and create the .pot file

Now open your .po file (attention: its the PO file, not the POT file!!!) with poedit and go to: Catalog->Update from POT file. It will compare your po file with the new pot file to check what text has been added, or has been deleted. Sometimes it will even try to help you with your translation (lines in yellow) using the previous translations.

And that’s it!!!

Final notes

  • Since cakephp 2.0 you ALWAYS need to echo the i18n function. So if you think that one of the strings is not been translated, the first thing to check is that you’re doing “echo __()” and not simply “__()”.
  • In poedit, the shortcut to pass to the next translation is “Ctrl+down” or “Ctrl+up”. Might save you a lot of time
  • Try to put complete sentences in the translations string. Avoid doing something like:
    echo __("Hello, my name is") . $name . __("and I'm") . $age . __("years old");

    It will not only make more translation strings, but depending on the language it could be impossible to translate the text because of the order of the variables.. Our example should be:

    echo __("Hello, my name is $s and I'm %d years old",$name,$age);

    And you’ll make the translator’s life easier n_n

This was just a basic guide to start. There are a few more options in cakephp, like domains, and I haven’t said anything about Localization. You can continue reading about it in the documentation of cakephp

Hope this helps