Translate your CakePHP Website

Translate

So, creating a translated website. I thought that was supposed to be easy with CakePHP.
Well actually, it is!

There are however some small steps you need to remember, especially regarding cache.

Use translatable tags

First off, you need to start with using the language tags for each text you want to translate. For the most convenient translating you will need to use English tags as base.

Just replace all text in your views and controllers with the gettext method, which is

echo __('text');

So instead of using

<strong>This is dog!</strong>

you would have to use

<strong><?php echo __('This is dog!'); ?></strong>

Create a POT file

Next up is to create a POT file, which actually is a source language file from which you translate to other languages.

The POT file is created with the Cake i18n command:

./cake i18n

Simply press E and follow the instructions, basically you can press enter until it is done.

Create a translation with POedit

First off, install POedit and open it.
Then click the option ‘Create new translation’ and select the ‘default.pot’ file saved by the cake i18n command. It is located in your project folder under [projectname]/app/Locale/.

POedit will ask you what language this translation is going to be in, so you can then pick the new language you want to create. Save this translation as [projectname]/app/Locale/[language]/LC_MESSAGES/default.po, where [language] is the short version for the language, conform the ISO 639-2 standards.

Note: Be sure to save the file as default.po. not as [language].po!

Informing CakePHP about using translations

Hi Cake, I want to use translations!

In your AppController’s beforeFilter function:

// Set language view and controller vars
if ($this->Session->check('Config.language')) {
    $lang = $this->Session->read('Config.language');
    Configure::write('Config.language', $lang);
} else {
    $lang = 'nl';
    $this->Session->write('Config.language', $lang);
    Configure::write('Config.language', $lang);
}

$this->userlanguage = $lang;
$this->set('userlanguage', $lang);

Restart your webserver

Small step that took me some time to figure out; Apparently the gettext function (that is used for translations in CakePHP) does caching…
So, to clear the cache, simply restart your webserver and all should be fine.

Changes in the project, new language tags?

You might need to change/add/delete some of the source tags.
If the files in your project changed simply create the .pot file again.

Now open your .po files located in the language folders; [projectname]/app/Locale/[language]/LC_MESSAGES/default.po, not the POT file in the Locale folder. Open the files 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.

BONUS: set language function

If you wish to use a system in which you can click on a flag or some other link to change the language, you can add the following function to the PagesController, or any other controller of choice:

public function set_language($lang){
	$this->Session->write('Config.language', $lang);
	Configure::write('Config.language', $lang);
	
	$this->redirect($this->referer());
}

Then simply add a route:

// Language
Router::connect('/setlanguage/:lang', array('controller' => 'pages', 'action' => 'set_language'), array('pass' => array('lang')));

And call the action with a link:

<a href="/setlanguage/eng"><?php if($userlanguage == 'eng'){ echo '<font style="color:#FFF">ENG</font>'; } else { echo 'ENG'; } ?></a>
<a href="/setlanguage/nl"><?php if($userlanguage == 'nl'){ echo '<font style="color:#FFF">NL</font>'; } else { echo 'NL'; } ?></a>

Thats all folks, thanks for reading!