.PO to .MO - Programmatic Conversion (PHP)

折月煮酒 提交于 2019-12-23 16:48:34

问题


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.


回答1:


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

  • php-msgfmt
  • php.mo/gh

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.)




回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/8167924/po-to-mo-programmatic-conversion-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!