multilingual in CakePHP 2.X step-by-step

Step1.  To internationalize your code, all you need to do is to wrap strings in __() like so:

<h2><?php echo __('Posts'); ?></h2>
Step 2: Create pot file for insert string in different language

/app/Locale/eng/LC_MESSAGES/default.po (English) /app/Locale/ara/LC_MESSAGES/default.po (Arabic)

Step 3: Add below code in app/Config/routes.php

Router::connect(‘/:language/:controller/:action/*’, array(), array(‘language’ => ‘eng|ara’));

Router::connect(‘/:language/:controller’, array(‘action’ => ‘index’), array(‘language’ => ‘eng|ara’));

Router::connect(‘/:language’, array(‘controller’ => ‘welcome’, ‘action’ => ‘index’), array(‘language’ => ‘eng|ara’)) ;

Step 4:  Set default language in app/Config/core.php

Configure::write(‘Config.language’, ‘ara’);

Step 5: create a helper file which  named MyHtmlHelper.php Insert below code in app/View/Helper/MyHtmlHelper.php App::uses(‘HtmlHelper’, ‘View/Helper’);

class MyHtmlHelper extends HtmlHelper { public function url($url = null, $full = false)

{ if(!isset($url[‘language’]) && isset($this->params[‘language’]))

{ $url[‘language’] = $this->params[‘language’]; }

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

} }

Step 6: add helper and Cookie component in app/Controller/AppController.php

public $components = array(‘Cookie’,’Session’);

//set an alias for the newly created helper: Html<->MyHtml

public $helpers = array(‘Html’ => array(‘className’ => ‘MyHtml’));

Step 7: add a method in app/Controller/AppController.php

private function _setLanguage()

{ //if the cookie was previously set, and Config.language has not been set

//write the Config.language with the value from the Cookie

if ($this->Cookie->read(‘lang’) && !$this->Session->check(‘Config.language’))

{ $this->Session->write(‘Config.language’, $this->Cookie->read(‘lang’)); }

//if the user clicked the language URL

else if (isset($this->params[‘language’]))

{ if ($this->params[‘language’] != $this->Session->read(‘Config.language’))

{  //then update the value in Session and the one in

Cookie $this->Session->write(‘Config.language’, $this->params[‘language’]);

$this->Cookie->write(‘lang’, $this->params[‘language’], false, ’20 days’);

} } }

Step 7: call  _setLanguage(); under  beforeFilter() in  app/Controller/AppController.php

public function beforeFilter()

{ $this->_setLanguage(); }

Step 8:  add the links to the languages:

// in : app/View/layout/default.ctp

echo $this->Html->link(‘English’, array(‘language’=>’eng’));

echo $this->Html->link(‘Arabic’, array(‘language’=>’ara’));


by  : Saidul Haque


4 thoughts on “multilingual in CakePHP 2.X step-by-step

  1. Thank you for your idea

  2. I followed the above post, its showing me Controller not found error. Could you plz help me??

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s