Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Internationalization in Cakephp 2.7

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 312
    Comment on it

    Internationalization means to translate the data of your website to other languages.

    For internationalization you need to do few changes in your controller view and config file too.

    1. First you need to add a file. You can call it global.php in config folder.
    Add this code to it.
     

    <?php
    
    $config['defaultLanguage'] = 'eng'; // Default Language
    
    $config['availableLanguages'] = array('eng' => 'English','ron' => 'Romanian'); // Available languages
    
    ?>

    2. Second after that you need to call this global.php in core.php.

    Configure::load('global');

    3. Third in the app controller you need to copy this code.
     

    <?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
        }
    }

    4. Fourth you need to call the languages using function which can be copied to your pages controller.
     

    <?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
    
            }
    
             
    
    

     

    5. Next you obviously need a view where you can select the languages. You can create it in the header.
     

    <!-- 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>

    This is not all.

    What you have to keep in mind is that whatever data you want to convert to other languages can be static and should be also enclosed in "__()".

    Cakephp read only "__()" to convert the strings to other languages.

    Now you need to create the .pot file which will include all the strings that are being enclosed in "__()".

    For creating .pot file.

    => You need to point towards your app folder and then type command in terminal.

    Console/cake i18n

    Here i18n is the standard for cakephp internationalization.

     

    => Next you will see few options like E I H Q. Enter E and press enter.

    => Now you need to enter the name of the folders from where you want to fetch the strings. It is mostly Model View and Controller. You can enter each of the respectively and press enter after every folder name. At the end press D and press enter.

    => Now it will ask you where you want to create the .pot file.

    Here you have to give the path. Default path for cakephp

    Locale/LC_MESSAGES/your_language_code

    In our case the code is ron.

    If you haven't made the folder for every language do make it.

    Now press enter.

    => It will ask you if you want to merge the default cakephp messages also to your .pot file. Depending on your need you can press y or n that is no or yes.

    => It will now create the default.pot file and few other .pot file like cake.pot and other. Our work is with default.pot.

    => Cakephp read only .po file for language translation. Now this is what you need to do. You need to convert the .pot file to .po file.

    => You can download and can use poedit software for easy editing and creating .po file.

    => Open your default.pot file with poedit and you will see all the strings that you need to do translate. Select each and write the translation of each and press control+enter to move to next.

    => Now save it and convert it to .po from the menu that you can find on the top.

    The translation is done.

 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: