1

I try to create resources files for a multi language PHP website. I created the .po files, then converted to binary .mo files with this small library https://github.com/josscrowcroft/php.mo, but they do not seem to work. But if I open the generated .po file with Poedit then hit save, I observed that the .mo file size change a little comparing with the initial one and the .po file works. I do not know how to get it working, directly, without using the Poedit Tool.

Please give me some suggestions if you have! Thanks

Bogdan
  • 11
  • 3
  • possible duplicate of [.po to .mo convertor in php?](http://stackoverflow.com/questions/1181681/po-to-mo-convertor-in-php) – Max Leske Oct 18 '14 at 14:06
  • the problem is not the converting of .po in .mo file, it is in the creation of the .po file. If I look at the .po file created by me and the one from Poedit, they seems identically on content, but somehow the size is a little different! I hope I made myself more clear – Bogdan Oct 18 '14 at 14:34

1 Answers1

1

Following text is copied from http://php.net/manual/en/book.gettext.php

" An important thing to keep in mind: Do not forget to set the charset in .po file! For example:

"Content-Type: text/plain; charset=UTF-8\n"

Then PHP will be able to find the .mo file you generated, using msgfmt, from the .po file WITH CHARSET SET.

Because of this I've wasted a lot of time debugging my code, testing every single little changes people suggested over this manual and Internet:

<?php

//this:
setlocale( LC_MESSAGES, 'pt_BR')
//or this:
setlocale( LC_MESSAGES, 'pt_BR.utf8')
//or this:
setlocale( LC_MESSAGES, '')

//this:
putenv("LANG=pt_BR.utf8");
//or this:
putenv("LANGUAGE=pt_BR.utf8");

//this:
bindtextdomain('mydomain', dirname(__FILE__).'/locale');
//or this:
bindtextdomain("*", dirname(__FILE__).'/locale');
//or this:
bindtextdomain('*', dirname(__FILE__).'/locale');

//setting or not "bind_textdomain_codeset()":
bind_textdomain_codeset("mydomain", 'UTF-8');
?>

As well as what locale directory name to set:

./locale/pt_BR.UTF8/LC_MESSAGES/mydomain.mo

or

./locale/pt_BR/LC_MESSAGES/mydomain.mo

or

./locale/pt/LC_MESSAGES/mydomain.mo

Finally, the code which brought the right translated strings (also with the correct charset) was:

<?php

$directory = dirname(__FILE__).'/locale';
$domain = 'mydomain';
$locale ="pt_BR.utf8";

//putenv("LANG=".$locale); //not needed for my tests, but people say it's useful for windows

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');
?>

And the three directory's names worked out, using the pt_BR.utf8 locale. (My tests were made restarting Apache then trying each directory).

I hope to help someone else not to waste as much time as I've wasted... =P

Using: Ubuntu 8.04 (hardy) Apache 2.2.8 PHP 5.2.4-2ubuntu5.6 "

Dubbo
  • 686
  • 6
  • 8
  • Thanks this did the trick for me for with pt_BR (my original strings are in spanish, then I translate to pt_BR). I didn't need the pt_BR.utf8 so far, but I was doing `bind_textdomain_codeset($domain, 'ISO-8859-1')` which was I guess because of spanish. Now with 'UTF-8' as second parameter it works fine for all (english too) – cdsaenz Feb 11 '20 at 19:35