CakePHP i18n: problem of updating .po files with a text editor

According to CakePHP documentation, you can use the console to generate .pot file, a template file for all .po files used to localize your website to different languages.  The documentation also recommends using PoEdit to edit your .po files.

Since always I try to play nicely with CakePHP, I followed the the directions and used the console to generate the template and PoEdit to generate a .po file from this template, and then ……., nothing worked :-|

After digging into the framework source code, I found some details that’s not mentioned in the online tutorial by CakePHP.

.po files aren’t the only source of getting localized strings, another type of files; .mo files, is also used for this task.  While .po files are text files, PoEdit automatically creates those binary .mo files upon saving your .po files in the same location of the corresponding .po file.  And to make my problem more tricky, those .mo files have higher priority inside the CakePHP framework, i.e. if the .mo file exists, the framework ignores the .po files and searches those .mo files only.  omm, not tricky enough, huh?.  Let’s make it more tricky and mention the fact that when you try to save your .po file using PoEdit, and PoEdit isn’t happy for any reason with your .po file, it creates a kinda empty .mo file, and this is where the problem occurs.

Here is the scenario I faced:

  • I used the console to generate the .pot files.
  • For some reason, CakePHP generated .pot file having duplicate msgid’s.
  • I opened it with PoEdit, checked it and saved it.  It complained about having 6 duplicate entries.  I just ignored this error message.
  • Behind the scenes, PoEdit created a .mo file that contained only the metadata without any translation information.
  • CakePHP always kept checking the empty .mo file and ignored the .po file.
  • The problem could be solved by removing .mo file, or of course by removing duplicates from the .po file.

Of course I used to face the same problem when I was updating the .po files using any normal text editor because .mo files aren’t updated.  The solution is to either remove .mo files manually, or open the .po files using PoEdit after you finish editing, and then save the file from inside PoEdit.  This lets PoEdit generate the appropriate .mo file.

I hope if someone faces the same problem, Google would be wise enough to get him this post in the first 10 search results :-)

  1. Thanks a lot.

    Unfortunately I’m having a similar problem with CakePHP. It only displays the translations of some lines from my .po file.

    I’ve deleted .mo file but it doesn’t work correctly.

  2. QuinDev

    You are working in the Development mode or Production mode?
    You need to be in Development mode to be sure that the file isn’t cached.

    This can be configured in core.php
    Configure::write('debug', 1);

  3. Thanks, it works!!

  4. I wanted to spend a mnuite to thank you for this.

  5. Ahmad

    10000000000000000 Thanks

  6. yevgeniy

    Cheers, deleted .mo file open .po with poedit, save and bang it all works!

  7. Ben

    Thank you for this information, it saved a lot of time….

six + = 8