简体   繁体   中英

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

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'); ?> <?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 file 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM