psanjib psanjib - 15 days ago 6
PHP Question

Cake php 2.x what is necessary of .po file and how to write translation word in .po file?

I am wondering about multilanguage in cake php. how to write transaction in .po file.
i want to use two language that is English and French, now i created two default.po file in the below path

/app/Locale/eng/LC_MESSAGES/default.po (English)
/app/Locale/fra/LC_MESSAGES/default.po (French)


how to access the default.po file and what i have to write inside default.po file

please help me to find out solution.

Thanks Sanjib

Answer

1. MINIMUM to check how it works

In Your View use function __(), for example, write in English (main language):

<?php echo __('My name is Tom');?>

Then in Your .po file for French /app/Locale/fra/LC_MESSAGES/default.po translate:

msgid "My name is Tom"
msgstr "Mon nom est Tom"

You can use poedit or good PHP editor (UTF-8) like eclipse

Then in Your controller - can be AppController.php -> public function beforeFilter() You can set Your language like:

$this->Session->write('Config.language', 'fra');


and...
don't forget to set Your main language in /Config/core.php like:

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

and use session component - in Your controller:

public $components = array('Session');


2. FOR COMPLETE MULTILANGUAGE SYSTEM You need to:

  • add function to init language like $this->_setLanguage(); to get language from URL params
  • add function to switch language with links in Your view/site layout with language param
  • set routing in Your Config/routes.php
  • can extends HtmlHelper to add language param to all your links, like:

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);
   }
}

UPDATE:

table with locale field for all languages: http://api.cakephp.org/2.4/source-class-L10n.html#177-326