开发者

Possible to add a translation to a gettext (*.po) catalog using PHP?

开发者 https://www.devze.com 2023-04-12 13:00 出处:网络
I am adding localization to a website. This process could be fully automated if I could simply add the translation to the .po file through PHP, but I can\'t seem to find a library which allows this (a

I am adding localization to a website. This process could be fully automated if I could simply add the translation to the .po file through PHP, but I can't seem to find a library which allows this (a PHP version of POEdit for example).

What I've been doing, since the HTML largely follows the same format throughout, is run each phtml file (They are actually Zend Framework views if it makes a difference) through a class I wrote to isolate the original english text, assign it an index (page.block.1), and write a <?php echo _('page.block.1'); ?> function while wrapping the original text in HTML comments. I then manually open POEdit, check for new strings to be translated, and cut/paste the commented text from the phtml f开发者_JS百科ile to POEdit.

This is very time consuming. Assuming I could use a PHP library to add a new translation, I have both the index and the string ready, so it would not only eliminate human error but also complete the task in seconds rather than hours.

Does such a class exist, for PHP or even python/perl? I can't seem to find one.


There is much simpler way especially if you're using Zend_Translate gettext adapter.

You can use translate functions directly in your .phtml files like this:

<label><?php echo $this->translate('some fancy text'); ?><label>

And then use the xgettext utility to create .po file from your .phtml files. The invocation of the xgettext would be something like this:

xgettext -o nameOfTheGeneratedPOFile.po --keyword=translate -L php

Then you can use the POEdit to translate the .po file and msgfmt to create the .mo file.

Check out the Zend_Translate documentation for more info on how to use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号