__() 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
Monday, March 12, 2012
Form Helper
When using the Form helper to create a form, there are some options you can utilize to make your forms better.
Radio
The following form radio selection is named 'Radio name' and lists two options, 'label1' and 'label2', with 'label1' selected by default.echo $this->Form->input('Radio name', array('type' => 'radio', 'legend' => false, 'options' => array('value1' => 'label1', 'value2' => 'label2'), 'default' => 'value1'));
Selection
The following form selection displays a select box with 4 directions and their corresponding single letter values and includes an empty first selection and a default selection of West.$dirs = array('N' => 'North', 'E' => 'East', 'S' => 'South', 'W' => 'West'); echo $this->Form->input('street_dir', array('label' => __('Direction', true), 'type' => 'select', 'options' => $dirs, 'empty' => true, 'default' => 'W'));
Subscribe to:
Posts (Atom)