Trouble with Internationalization on CakePHP 2.0

2019-06-08 03:09发布

问题:

The internationalization process is not working.

I ran cake i18n export. After that I put the translations into the file ".po" in the folder app/locale/por/LC_MESSAGES/default.po..

I called the method beforeFilter() in the AppController Configure::write('Config.language', 'por');.

But the translation isn't shown in __()-methods.

回答1:

Here are some possible suggestions:

• Clear the Cake cache in PATH_YOUR_APP/tmp/cache. Delete the files in models and persistent, but not the directories itself. I believe persistent is where the localised strings are saved, but empty both to be sure.

• Do you override the beforeFilter in any of your controllers, other than the AppController? If so call parent::beforeFilter(); in the controller's beforeFilter method, before processing other stuff, like so:

public function beforeFilter() {
  parent:beforeFilter();
  //Do stuff here
}

• How did you create the translation files? Is there a .mo file in the .po directory as well? I believe directly editing .po and .pot files is not recommended due to character encoding issues (but I could be mistaken). Poedit is a good tool for manipulating .po files, though it could be more robust.



回答2:

Some more things to check

  1. Check whether your browser's language is selected properly Portuguese as the first language of choice
  2. Is your msgid and msgstr keys in individual lines and properly enclosed the text within double quotes?
  3. I'm not a CakePHP expert, but try moving the configuration to the bootstrap.php. Your code seems logical there.