使用POEdit/xgettext翻译变量 [英] Translate variables with POEdit/xgettext

查看:136
本文介绍了使用POEdit/xgettext翻译变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PHP Zend Framework 和Zend_Translate(gettext适配器).要编辑翻译,我使用 POEdit ,它利用 xgettext 来获取要翻译的字符串.

I am using PHP, Zend Framework and Zend_Translate (gettext adapter). To edit translations I use POEdit which utilizes xgettext to fetch strings to be translated.

POEdit(xgettext)将搜索关键字以查找要翻译的字符串.因此,如果搜索关键字translate,则将文本直接传递给翻译函数时,POEdit将毫无问题地找到'Text to translate'字符串:

POEdit (xgettext) will search for keywords to find strings to translate. So, if searching for the keyword translate, POEdit will have no problem finding the 'Text to translate' string when the text is passed to the translate function directly like:

echo translate('Text to translate');

但是,在其他情况下,字符串将传递给Zend函数,这些函数将为我执行转换,并以变量作为参数调用转换函数:

But, in ofther cases strings will be passed to Zend functions that will do the translation for me, calling the translate function with a variable as a parameter:

function SomeZendFunction( $array ) {
    return translate( $array['string'] );
}

...

echo SomeZendFunction( array('string'=>'Another text to translate') );
// translate('Another text to translate');

这将导致POEdit(xgettext)无法找到要翻译的字符串.在上面的示例中,我希望POEdit查找的字符串是'Another text to translate',但是由于它没有直接传递给translate函数,因此找不到.

This will cause POEdit (xgettext) to fail finding the string to translate. In the above example the string I wanted POEdit to find is 'Another text to translate', but since it is not passed directly to the translate function, it will not be found.

那么,如何解决问题?

我当前的解决方案是创建一个虚拟文件,其中包含POEdit找不到的所有字符串的长列表:

My current solution is to create a dummy file containing a long list of all the strings that was not found by PO

<?php // Dummy file, only accessed by POEdit when scanning for strings to translate
translate('Text to translate');
translate('Another text to translate');
translate('A third text to translate');
....

但是该解决方案的缺点是,在更新字符串时,我俩都需要更改虚拟文件并找到原始字符串.这将使维护变得更加困难.

But the downside of this solution is that when updating a string, I both need to change the dummy file and find the original string. This will make it more hard to maintain.

我想到的另一种解决方案是在调用SomeZendFunction之后将翻译字符串添加到注释中(请参见上面的示例),但是我无法使xgettext接受它,因为它会忽略注释.

Another solution I thought of was adding the translation string to a comment after calling SomeZendFunction (See the above example), but I fail to make xgettext accept it as it ignores comments.

因此,有人知道如何使xgettext接受注释中的字符串吗?还是有人有其他更好的解决方案?

So, anyone knows how to make xgettext to accept strings within comments? Or anyone has any other solution that might be better?

感谢您的帮助!

我不知道我被否决的原因是什么.但是我试图澄清这个问题.

I don't know for what reason I was down voted. But I've tried to clarify the question.

推荐答案

我就知道了!通过创建一个虚拟函数

I just got it! By creating a dummy function

function t($string) {
    return $string;
}

echo SomeZendFunction( t('Another text to translate') );

我可以将此t函数添加到POEdit中的翻译关键字中.然后,我可以将所有字符串嵌入,稍后再由Zend转换为该Dummy函数.

I can add this t function to the translate keywords in POEdit. Then I can embed all strings that will later on be translated by Zend into this Dummy function.

通过这种方式,可以允许Zend进行翻译,而POEdit会将其识别为要翻译的字符串.

This way Zend will be allowed to translate it, and POEdit will recognize it as a string to translate.

如果有人有更好的解决方案,请发布它.

If anyone has a better solution, please post it.

这篇关于使用POEdit/xgettext翻译变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆