pb149
pb149

Reputation: 2298

.PO to .MO - Programmatic Conversion (PHP)

I am planning to allow users to generate .POT files/.PO files through a PHP user interface as part of CMS solution. Once these files have been generated (the easy bit) I would like to allow my system to automatically convert these files into .MO files in response to a user (POST) request.

I have seen the following question on SO:

.po to .mo convertor in php?

I understand that I could run msgfmt by using PHP's exec() function, but that seems to be a Linux only solution, if I am correct? How would I do this on other operating systems? Some example code of how this may be done in practice would also be really useful, if anybody would be kind enough to demonstrate. This is quite different from the work I usually do!

This is only a concept at the moment but I hope I'm going along the right lines. If there are any additional thoughts/suggestions you have regarding this method, I'd be glad to hear them. Background information follows.

Additional Background Information - Not required:

I am retrieving the original English text by parsing simple template files that consist of nothing more than basic HTML and calls to <?php _('the gettext method'); ?>. These templates are parsed when edited/saved and the language entries are retrieved. The .POT file will then be generated. The user would now have to edit translations manually (through a simple interface, not directly) to update/prepare all the .PO files. Once this is done, I would need to be able to convert them to .MO files, as is the title of my question.

Upvotes: 3

Views: 6697

Answers (3)

max4ever
max4ever

Reputation: 12142

try https://github.com/oscarotero/Gettext.git

use Gettext\Translations;
chdir('....');
        $translations = Translations::fromPoFile('messages.po');
        $translations->toMoFile('messages.mo');

Upvotes: 1

mario
mario

Reputation: 145482

There are also PHP-only reimplementations of msgfmt if that is what you are looking for:

As alternative there would also be the Translate/Pootle webapp, with its php2po script, but there must also be some .mo conversion functions in it... (Ooops no, it's in Python.)

Upvotes: 6

hakre
hakre

Reputation: 197564

Both PHP's exec and the msgfmt GNU gettext utility are not a linux only solution. They work on multiple computer systems. As with PHP you can compile for multiple platforms (as it's done naturally), so the exec command is available on mutliple systems, the same applies to msgfmt. Start on the GNU gettext homepage to obtain a version for your system.

Upvotes: 2

Related Questions