Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PHP library for parsing gettext PO POT files? [closed]

if is not in PHP, is possible use some command line tools which convert PO file into some structured format e.g. XML or some other which I can simple process in PHP?

like image 973
MarekLi Avatar asked Dec 08 '11 15:12

MarekLi


2 Answers

Update: As of 2018/05, the best options are:

  • https://github.com/oscarotero/Gettext
  • https://symfony.com/doc/current/components/translation.html
  • https://docs.zendframework.com/zend-i18n/translation/

See http://www.phptherightway.com#i18n_l10n_title

There are some:

  • https://github.com/jyxo/php/tree/master/Jyxo/Gettext
  • https://github.com/clinisbut/PHP-po-parser
  • http://code.google.com/p/php-po-parser/
  • http://pear.php.net/package/File_Gettext/redirected
  • http://drupalcode.org/project/drupal.git/tree/HEAD:/core/lib/Drupal/Component/Gettext

The last one is part of Drupal, and it's probably the most maintained.

like image 131
Pere Avatar answered Nov 03 '22 10:11

Pere


Some simple regular expressions will allow you to parse .PO/.POT files. I did a similar thing recently. Take the following regex from a script I have recently written:

$poMsgIdAndMsgStrRegex = '/^#\s*(.+?)\nmsgid "(.+?)"\nmsgstr "(.+?)"/m';

This does only capture the final comment line but it has so far been suitable for my purposes. You may need to adapt it.

Anyway, you can use the above regex with preg_match_all() to capture all MsgId and MsgStr strings into an array of matches. Once you have the array then you can put it into any format you wish.

There may be libraries for doing this but I have not seen any.

Edit: You may also want to check out the PHP class for converting.po files .mo format that is referred to in the 'possible duplicate' comment. It doesn't sound like it will solve your problem (since you want to convert .po files to XML), but it is worth examining anyway. It helped me a lot.

like image 20
pb149 Avatar answered Nov 03 '22 11:11

pb149