I'm using CakePHP v2.5.6 and I'm following this documentation on CakePHP localization. I've read it 100 times but it still isn't working!
Using the cake console i18n
task, I've created the following files...
app/Locale/default.pot
app/Locale/fra/LC_MESSAGES/default.po
app/Locale/fra/LC_MESSAGES/default.mo
...I used Poedit to translate a few strings (for testing) purposes:
In bootstrap.php I have:
Configure::write('Config.language', 'fra');
(I've also tried placing it in my AppController's beforeFilter()
which also didn't work)
In my view I have:
<?php
Debugger::dump( Configure::read('Config.language') );
Debugger::dump( CakeSession::read('Config.language') );
echo __('Find a Place');
?>
But when I load the page I just see:
'fra'
null
Find a Place
Why?????!!!!!!
Update 1 @ndm raised some concerns about caching...
In my persistent cache the following files appear:
.../temp/cache/persistent/myapp_cake_core_cake_fra
.../temp/cache/persistent/myapp_cake_core_default_fra
.../temp/cache/persistent/myapp_cake_core_file_map
.../temp/cache/persistent/myapp_cake_core_method_cache
Update 2 Following @ndm's advice, I hacked out everything but one translation in the .po file and it WORKED!
# LANGUAGE translation of CakePHP Application
# Copyright YEAR NAME <EMAIL@ADDRESS>
#
msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"POT-Creation-Date: 2014-11-20 11:30-0500\n"
"PO-Revision-Date: 2014-11-20 11:43-0500\n"
"Last-Translator: SDP\n"
"Language-Team: <EMAIL@ADDRESS>\n"
"Language: fr_CA\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 1.6.10\n"
#: View/Elements/nav--main.ctp:3
#, fuzzy
msgid "Find a Place"
msgstr "Trouver une place"
Clearly something is wrong with my file, but it's weird because it's a brand new file generated by Poedit AND it passes the "Validate" function without problems. It just says that there are lots of entries that aren't translated yet.
The lines I removed from the file look like this:
...
#: Controller/FavoritesController.php:38;73;101
msgid "Invalid favorite"
msgstr ""
#: Controller/FavoritesController.php:53;77
msgid "The favorite has been saved."
msgstr ""
#: Controller/FavoritesController.php:56;80
msgid "The favorite could not be saved. Please, try again."
msgstr ""
...
Anything wrong with that?
Fuzzy entries are not being compiled
So as we figured in the comments, the problem here was that the problmatic translations have been marked as
fuzzy
, and such entries are not being compiled into the.mo
file unless this is explicitly requested using the-f
or--use-fuzzy
flag withmsgfmt
.Entries are automatically being marked as fuzzy when
.pot
andpo
files are merged, and twomsgid
strings are very close but do not match exactly, and since these matches might be incorrect they are by default not being compiled.I should have seen that in your Screenshot, which clearly says Fuzzy in the toolbar and the statusbar, and I'd suspect the yellowish marked entries are those that are fuzzy, but it was already very late.
CakePHP uses
.mo
files over.po
filesA little Cake background info, even though (currently) undocumented, CakePHP will use
.mo
files over.po
files in case they are present.These compiled binary variants are usually faster to parse (compare
I18n::loadMo()
andI18n::loadPo()
), as the format is simpler (no comments, unnecessary (like fuzzy) translations removed, etc...) and it can be expected to be valid, and of course.po
files might contain entries that shouldn't be used, like those fuzzy ones :)