Multiple language CakePHP menggunakan Localization

Terimakasi to link http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/ yang udah ngeposting.. berguna bangettt :D.

Langkah-langkahnya sebagai berikut
1. setting di app/config/routes.php dengan:

Router::connect('/:language/:controller/:action/*', array(), array('language' => '[a-z]{3}'));

2. set default language, misal defaultnya English, pada file app/config/core.php

Configure::write('Config.language', 'eng');

3. Pada file AppController.php tambahkan dibawah ini, untuk create session language, jika belum ada pass Argument,
create session language dan cookie default (sudah didefinisikan pada core.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');
}
}

4. Create file AppHelper.php pada folder View

class AppHelper extends Helper {

function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}

return parent::url($url, $full);
}

}

5. Create link untuk pindah bahasa
<?php echo $this->Html->link(‘Bahasa’, array(‘language’=>’ind’));?> &nbsp;
<?php echo $this->Html->link(‘English’, array(‘language’=>’eng’));?>

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s