2

I've read absolutely every SO post regarding internationalization in CakePHP and nothing seems to be working for me. I'm using CakePHP 2.5.1.

In config/bootstrap.php:

//Default language
Configure::write('Config.language', 'spa');

In controller/AppController.php:

public $components = array('Session', 'DebugKit.Toolbar');

public function beforeFilter()
{
    //Set the UI lang preference
    if($this->Session->check('Config.language'))
    {
        Configure::write('Config.language', $this->Session->read('Config.language'));
    }
}

My file structure is as so:

/Locale/
   - default.mo
   - default.po
   - default.pot
   - eng/LC_MESSAGES/
       - default.mo
       - default.po

I have links on the view as such:

echo '<p class="">' . $this->Html->link('EN', array('controller' => 'users', 'action' => 'lang', 'en')) . '</p>';
echo '<p class="">' . $this->Html->link('ES', array('controller' => 'users', 'action' => 'lang', 'es')) . '</p>';

and my Users controller does the following with this:

public function lang($which)
{
    if($which == "en")
    {
        $this->Session->write('Config.language', 'eng');
    } elseif($which == "es")
    {
        $this->Session->write('Config.language', 'spa');
    } else
    {
        $this->Session->setFlash(_('Unknown language.'), 'flash_red');
    }
    $this->redirect('/');
}

When I click on any of the links (EN or ES), the page reloads but shows the normal text.

Any clue on what I could be missing? Or any tips on where to start debugging in the Cake core code?

PD. The error logs don't show anything.

Thanks!

Sandy
  • 2,572
  • 7
  • 40
  • 61
  • First step, delete the cache (`/app/tmp/cache/`). – ndm Oct 07 '14 at 03:55
  • 1) Change in config/bootstrap.php in order to not have a config variable named "Config.language" for default language: Configure::write('Config.defaultLanguage', 'spa'); 2) Change in controller/AppController.php if($this->Session->check('Config.language')) {Configure::write('Config.language', $this->Session->read('Config.language'));} else {Configure::write('Config.language', Configure::read('Config.defaultLanguage'));} 3) Delete all files from /app/tmp/cache/persistent/*.* 4) Validate your po files or use poedit 5) Use Debug Kit and follow the sessions values – wappy Oct 07 '14 at 11:56
  • @ndm I've tried deleting the cache but this doesn't fix it. – Sandy Oct 07 '14 at 15:36
  • @CristianFlorea where/how can I validate my po file? – Sandy Oct 07 '14 at 15:36
  • @CristianFlorea Using Debug Kit the session variables seem ok. I'm seeing `>Config` and when I click on it I can see the value `language` which changes between `eng` and `spa` when I click on the different links. I also removed the setting in bootstrap.php and removed the files from the cache folder. Still no luck :( – Sandy Oct 07 '14 at 23:06
  • If it's at all helpful, I do see some files in `/tmp/cache/persistent/` that end in `_eng` and `_spa` – Sandy Oct 07 '14 at 23:10
  • Didn't ask but you're echo your text like this in your CTP: And in your po files: /app/Locale/spa/LC_MESSAGES/default.po: #: View/Pages/home.ctp:1 msgid "hola!" msgstr "hello!" Simply use PO Edit – wappy Oct 09 '14 at 13:51
  • Try to follow all the steps described here and be careful to use __('your text'); method: http://wpy.me/en/blog/5-cakephp-simple-internationalization-tutorial-using-i18n-shell-and-poedit – wappy Oct 09 '14 at 18:45

1 Answers1

0

The error above is that ONE underscore is being used, instead of TWO:

$this->Session->setFlash(_('Unknown language.'), 'flash_red');

should be

$this->Session->setFlash(__('Unknown language.'), 'flash_red');

In all the other parts of the code I'm only using one underscore, when it should be two for internationalization.

Sandy
  • 2,572
  • 7
  • 40
  • 61