__() function. For example, for echoed text like the following:
echo 'This text';
Replace it with the following:
__('This text');
For variables, such as the following:
$directions = array('N' => 'North', 'S' => 'South', 'E' => 'East', 'W' => 'West');
Replace it with the following:
$directions = array('N' => __('North', true), 'S' => __('South', true), 'E' => __('East', true), 'W' => __('West', true));
Then build your /app/locale/default.pot
file using the console
# cd /app
# /cake/console/cake i18n extract
Accept default options and the file is created.
Edit the /app/locale/default.pot
with a program like Poedit, add your translations, then save the file in the appropriate directory as a catalog (.po file) e.g. /app/locale/spa/LC_MESSAGES/default.po
Be sure to re-extract and update the .pot and catalog files every time you add new text to your application.
To add a language to the beginning of each path add the following to your /app/config/routes.php
:
/* For internationalization */
Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));
To specify a default language, add the following to your /app/config/core.php
and add the following lines:
/* For internationalization */
Configure::write('Config.language', 'eng');
Now to create a language switching link use the following:
echo $this->Html->link('EspaƱol', array('language' => 'spa'));
Ensure Cookies and Session components are loaded and put the following functions in the /app/app_controller.php
:
var $components = array('Session', 'Cookie');
function beforeFilter() {
$this->_setLanguage();
}
function _setLanguage() {
if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
$this->Session->write('Config.language', $this->Cookie->read('lang'));
}
else if (isset($this->params['language']) && ($this->params['language'] != $this->Session->read('Config.language'))) {
$this->Session->write('Config.language', $this->params['language']);
$this->Cookie->write('lang', $this->params['language'], false, '20 days');
}
}
Lastly, create the /app/app_helper.php
file
params['language'])) {
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}
}
?>
Monday, July 2, 2012
Multi-language Support
To allow your application to appear in multiple languages, first change all displayable text to utilize the
Subscribe to:
Posts (Atom)