Gettext placeholders

I am creating a multilingual application in PHP + CodeIgniter. I decided to use gettext to translate the user interface text, and so far it has proved to be efficient and easy to use.

But now I come across something really annoying: the function gettext()accepts only one parameter, while I like the behavior like printf that I get from the Zend Framework gettext adapter, where I can use it %1$s, %2$setc. as placeholders, and then specify replacement strings as additional parameters to the Zend function to view translate().

I do not want to interrupt gettext due to the simple translation management with .po files and poEdit (in the end, I can update it with one click). What are my options?

I already tried writing a helper to interact with gettext: run the first argument via gettext, and then run strtr in the resulting string. Are there any other / better approaches you would recommend?

+5
source share
1 answer

It is quite simple, you define a variational function as follows:

function myGettext($id)
{
    return vsprintf(gettext($id), array_slice(func_get_args(), 1));
}

Now execution myGettext('%u %s in a %s', 3, 'monkeys', 'tree')will return the expected string with the replacement arguments remaining arguments. You obviously also need to implement a multiple access function that calls instead ngettext().

poEdit, , , , , , , , (. ).

, !

+6

All Articles