使用 POEdit/xgettext 翻译变量
我正在使用 PHP、Zend Framework 和 Zend_Translate(gettext 适配器)。为了编辑翻译,我使用POEdit,它利用xgettext来获取要翻译的字符串。
POEdit (xgettext) 将搜索关键字以查找要翻译的字符串。因此,如果搜索关键字 translate
,当文本直接传递给翻译函数时,POEdit 将毫无问题地找到 'Text to translate'
字符串,如下所示:
echo translate('Text to translate');
但是,在其他情况下,字符串将传递给 Zend 函数,该函数将为我进行翻译,以变量作为参数调用翻译函数:
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 totrans'
,但由于它没有直接传递给 translate
函数,因此不会找到它。
那么,如何解决问题呢?
我当前的解决方案是创建一个虚拟文件,其中包含 POEdit 未找到的所有字符串的长列表:
<?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');
....
但此解决方案的缺点是,在更新字符串时,我都需要更改虚拟文件并找到原始字符串。这将使其更难以维护。
我想到的另一个解决方案是在调用 SomeZendFunction
之后将翻译字符串添加到注释中(参见上面的示例),但我无法使 xgettext 接受它,因为它忽略注释。
那么,有人知道如何使 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) 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');
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');
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.
So, how to solve the problem?
My current solution is to create a dummy file containing a long list of all the strings that was not found by POEdit:
<?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.
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.
So, anyone knows how to make xgettext to accept strings within comments? Or anyone has any other solution that might be better?
Thanks for any help!
Edit:
I don't know for what reason I was down voted. But I've tried to clarify the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要使用虚拟函数并向其传递字符串,为什么不重构 someZendFunction 以接受字符串参数,然后将该函数名称“someZendFunction”添加到 poedit 关键字的列表中?这消除了额外的函数调用并使代码更简洁。
或者您可以将“someZendFunction”包装到 t(9 函数中,以便它为您完成肮脏的工作!这也将使您免于额外输入!
示例:
然后在代码中的某个位置:
希望这有帮助!
If you are going to use a dummy function and pass strings to it, why no refactor the someZendFunction to accept a string parameter and then add that function name "someZendFunction" to the list in the poedit keywords? That eliminates the extra function calls and makes the code a bit cleaner.
Or you could wrap that "someZendFunction" into the t(9 function so that it does the dirty work for you! that will also save you from extra typing!
Example:
and then somewhere in your code:
Hope this helps!
我刚拿到!通过创建虚拟函数,
我可以将此
t
函数添加到 POEdit 中的翻译关键字中。然后我可以将 Zend 稍后翻译的所有字符串嵌入到这个虚拟函数中。这样 Zend 将被允许翻译它,并且 POEdit 会将其识别为要翻译的字符串。
如果有人有更好的解决方案,请发布。
I just got it! By creating a dummy function
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.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.
正如这个词所说,“变量”意味着值会随着时间的推移而变化,因此不可能提前知道该值是什么。您想要的翻译方式不正确。
As the word say, "Variable" means that the value varies from time to time, so there is no possibility to know in advance what will be the value. The way you are looking to translate is incorrect.