views:

112

answers:

2

I would like to know if I can use a gt function on a variable with php?

eg. : echo _($var);

+3  A: 

Feel free to. But you need to make sure the possible contents of the variable makes it into .po/.mo files. (one of the ways to do assure it is to create a dummy file never processed except for by xgettext, containing _("translate me"); expressions).

Michael Krelin - hacker
+1  A: 

I don't think gettext will recognize a variable since it scans the source code. If you want to include variables in a string, it's better to use

sprintf()

For example

echo sprintf(_("There are %d results!"), $numResults);

In gettext, the translator will see

There are %d results!

so therefore it can easily be translated as long as he/she knows that %d is a variable. When the script gets executed, gettext will first replace the translation, and then sprintf will insert the variable $numResults. Good luck! I just got done internationalizing my site.

Axsuul
In this specific case it should be `sprintf(ngettext("There is %d result!", "There are %d results!", $numResults), $numResults)`.
Lukáš Lalinský
I doubt if the question was about substitution (which is out of question), but rather than having a whole string retrieved from the other datasource (e.g. database). And, btw, gettext doesn't really scan the source, `xgettext` does and you aren't strictly obliged to use `xgettext` at all.
Michael Krelin - hacker
Lukáš Lalinský, right, this is how you know Slavic language speaker ;-) We care about plural forms.
Michael Krelin - hacker
yea, the overall question is kinda vague too
Axsuul
I'm open to any kind of answers, just looking for the best way to use i18n on variables.
mnml