ANisus ANisus - 1 month ago 12
PHP Question

Translate variables with 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) 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.

Answer

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.