3
votes

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:

enter image description here

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?

1
As a first step try deleting the cache (app/tmp/cache/persistent).ndm
@ndm Good thinking. But it didn't change anything.emersonthis
A few other things to try, check whether valid cache files are being generated (*_default_fra), test with a single manually created .po file only that contains just one msgid/msgstr pair, check whether there's a locale set in the session (CakeSession::read('Config.language')) as this will take preceedence (and don't forget the clear cache in between tests :)).ndm
@ndm AHA! I posted the cache files above and the CakeSession returns null, which I think is correct since I'm not setting it yet... but chopping off all the extra entries in the .po file worked! The one remaining one works! So clearly there is something wrong with the file. But I can't imagine what. It's a brand new file generated from the .pot file that the i18n cake task outputs. Is it a problem that there were many missing entries? I thought was supposed to be fine. Any ideas how to debug the .po file?emersonthis
Are you now using the .po file only, ie have you deleted the .mo file? In that case the problem might be that the entry is marked as fuzzy which should normally cause it to be not compiled into the .mo file unless this is explicitly asked for (for msgfmt this would be the -f, --use-fuzzy option, not sure how poedit compiles the files). That's it for tonight, it's bedtime :)ndm

1 Answers

6
votes

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 with msgfmt.

Entries are automatically being marked as fuzzy when .pot and po files are merged, and two msgid 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 files

A 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() and I18n::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 :)