6

I am building a multilingual application in PHP + CodeIgniter. I have settled upon using gettext for UI text translation, and so far it has proven efficient and easy to work with.

But now I am facing something really annoying: the gettext() function only accepts one parametre, while I'd like a printf-like behaviour that I get from Zend Framework's gettext adapter, where I can use %1$s, %2$s etc. as placeholders and then specify the replacement strings as additional parametres to Zend view's translate() function.

I do not wish to ditch gettext due to the easy translation management with .po files and poEdit (I can get it updated with a single click, after all). What are my options?

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

mingos
  • 23,778
  • 12
  • 70
  • 107
  • I do realize that you said you don't want to ditch gettext, but you should be aware that you *can* use Zend_Translate and CI together without too much trouble. [The manual says](http://framework.zend.com/manual/en/zend.translate.adapter.html) that it can read `.mo` files. I'm not sure how those differ from `.po` files, thus the reason why I'm posting this as a comment instead of an answer. The manual does say that "POEdit" works with that format. – Charles Mar 27 '11 at 01:13
  • `.mo` is what `.po` becomes after it's compiled. Gettext reads `.mo`, but `.po` is human-readable and that's what you edit :). Yes, using Zend_Translate is definitely an option to consider. – mingos Mar 27 '11 at 01:31

1 Answers1

7

It's quite simple actually, you define a variadic function like this:

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

Now doing myGettext('%u %s in a %s', 3, 'monkeys', 'tree') will return the expected string with the placeholders replaced by the remaining arguments. You obviously also need to implement a plural aware function that calls ngettext() instead.

Regarding poEdit, you have to modify the keywords it searches for, it's been a while since I last used it but it was quite simple, the only problem I faced was identifying keywords for plural support (see this).

Hope it helps!

Community
  • 1
  • 1
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • This is more or less what my helper does, but the code is simpler (a single line vs. four, hehe). poEdit indeed has the possibility to define the keywords - this is how I pick up the translatable strings in Zend Framework's `translate`, `setLabel` and `setDescription` functions :). Thanks, I like your approach. – mingos Mar 27 '11 at 13:28
  • it wasn't exactly a "problem", but yes, I changed my helper to what you suggested and not only does it work like a charm, but I'm also lovin' the amount of code it requires :D. Thanks again. – mingos Mar 27 '11 at 15:56
  • @mingos: Oh, great. I'm glad I could be of some help. ;) – Alix Axel Mar 27 '11 at 16:29