Translate variables with POEdit/xgettext

亡梦爱人 提交于 2020-02-05 08:33:33

问题


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.


回答1:


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:

function t($str){
    echo someZendFunction($str);
}

and then somewhere in your code:

t('Translate this string please!');

Hope this helps!




回答2:


I just got it! By creating a dummy function

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

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

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.




回答3:


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.



来源:https://stackoverflow.com/questions/7644302/translate-variables-with-poedit-xgettext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!