Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • InterNationalization in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 984
    Comment on it

    Hello Readers ,

    Today we will learn about the concept of " InterNationalization in CakePHP " .

    "InterNationalization" simply means if you are making an application and you wants to make that applications to reach a larger audience to cater for multiple languages.

    To do this below are the steps-

    Create a new CakePHP project:

    Download the latest version from http://cakephp.org/.

    Now to initialise the default and other languages just create a file global.php file in /app/Config/ , you may give your own name instead of global.php.

    Put these lines of code in global.php.

    <?php
    $config['defaultLanguage'] = 'eng'; // Default Language
    $config['availableLanguages'] = array('eng' => 'English', 'spa' => 'Spanish', 'ron' => 'Romanian', 'rus' => 'Russian', 'fra' => 'French'); // Available Languages
    ?>
    

    Now load our global.php config file. In /app/Config/core.php add the following line at the end:

    Configure::load('global');
    

    Now creates an AppController which will support InterNationalization.

    Open /app/Controllers/AppController and place the following lines of codes.

    <?php
    App::uses('Controller', 'Controller');
    class AppController extends Controller {
    public $components = array('Session');
    
    
    public $helpers = array('Html', 'Form', 'Text');
    var $language, $availableLanguages;
    
    public function beforeFilter() {
     parent::beforeFilter();
    if($this->Session->check('Config.language')) { // Check for existing language session
    $this->language = $this->Session->read('Config.language'); // Read existing language
    } else {           
     $this->language = Configure::read('defaultLanguage'); // No language session => get default language from Config file
      }
    
            $this->setLang($this->language); // call protected method setLang with the lang shortcode
            $this->set('language',$this->language); // send $this->language value to the view
            $this->availableLanguages = Configure::read('availableLanguages'); // get available languages from Config file
            $this->set('availableLanguages', $this->availableLanguages); // send $this->availableLanguages value to the view
        }
    
        protected function setLang($lang) { // protected method used to set the language
            $this->Session->write('Config.language', $lang); // write our language to session
            Configure::write('Config.language', $lang); // tell CakePHP that we're using this language
        }
    }
    ?>
    

    Now creates the PagesController for localization.

    Open /app/Controller/PagesController and remove public function display() {}. Instead of that create a home() method for our base path and a changeLanguage() method.

    <?php
    App::uses('AppController', 'Controller');
    class PagesController extends AppController {
    
        public $uses = array(); // No model
    
        public function home() { // Base path method
        }
    
        public function changeLanguage($lng) { // Change language method
            if(isset($this->availableLanguages[$lng])) { // If we support this language (see /app/Config/global.php)
                parent::setLang($lng); // call setLang() from AppController
                $this->Session->setFlash(__('The language has been changed to %s', $this->availableLanguages[$lng])); // Send a success flash message
            } else {
                throw new NotFoundException(__('Language %s is not supported', $lng)); // Throw a not found exception
            }
    
            $this->redirect($this->referer()); // redirect the user to the last page (referer)
        }
    }
    ?>
    

    Now we will create the View for our controller.

    Create (or change the content from) /app/Views/Pages/home.ctp file. Our objective is to echo a test message in all the available languages, to list all the available languages and to put in evidence the current language.

    Put the below lines codes.

    <!-- Change Language H1 -->
    <?php echo $this->Html->Tag('h1', __('Change language:')); ?>
    
    <!-- Change Language list -->
    <ul>
    <?php foreach($availableLanguages as $key=>$value) {
        $link = $this->Html->Link($value, array('controller' => 'pages', 'action' => 'changeLanguage', $key));
        echo $this->Html->Tag('li', $link, array('class' => $key == $language ? 'selected' : ''));
    } ?>
    </ul>
    
    <p>&nbsp;</p>
    <!-- Change Language test -->
    <?php echo $this->Html->Tag('h2', __('Hello!')); ?>
    
    <!-- Put in evidence the selected language -->
    <style>
        ul li a {text-decoration: none;}
        ul li.selected a {text-decoration: underline;}
    </style>
    

    Now delete all the tmp files from /app/tmp/cache/persistent/. and test your application.

    You will see some links occur of different languages which will defines the concept of InterNationalization in CakePHP.

    Good Luck!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: