Get translations from .po or .mo file

2020-03-25 03:12发布

How can I extract all translations from a .po or .mo file? I need to create an array of all translations that are inside.

标签: php gettext
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-03-25 03:44

You can use PEAR File_Gettext. The code would then be:

$mocreator = new File_Gettext_MO();
$mocreator->load('/path/to/mo/file/file.mo');

foreach ($mocreator->strings as $key => $value) {
  echo "Key is $key \n";
  echo "Value is $value \n";
}
查看更多
冷血范
3楼-- · 2020-03-25 03:45

There is a small extraction AWK script in PhpWiki 1.2, called translate.sh. Use that (on Linux).
It greps all .po files and generates a .php array() script.

查看更多
4楼-- · 2020-03-25 03:47

msgunfmt is a unix tool that reads .mo files and produces a .po file.

查看更多
够拽才男人
5楼-- · 2020-03-25 03:56

I used po2csv to convert the po, then read the csv into php using the fgetcsv() function.

查看更多
一夜七次
6楼-- · 2020-03-25 04:07

You can use Zend Translate module from Zend Framework.

$translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en');
$translate->addTranslation('/path/to/german.mo', 'de');

echo $translate->_("Example");

$translate->setLocale('de');

echo $translate->_("Example");

or you can use php gettext module, but Zend is much more handy.

查看更多
登录 后发表回答