Re: gettext and variable parsing

From: Date: Fri, 03 Oct 2003 07:46:08 +0000
Subject: Re: gettext and variable parsing
References: 1 2  Groups: php.i18n 
Request: Send a blank email to php-i18n+get-620@lists.php.net to get a copy of this message
On Mon, 11 Aug 2003 12:16:12 +0200, Christophe Chisogne <christophe@publicityweb.com> wrote:
Ramil Sagum wrote:
echo _("Displaying records from $from to $to of $total");
Try with printf( _("Displaying records from %d to %d of %d"), $from, $to, $total ); It seems you must quote the strings with ".." for gettext to work. For all "variable content" to be translated, you have to use "format strings" (1st argument of printf, sprintf...). By example, "%d" means to write the var as an integer. With printf, sprintf etc gettext translate a static string "Displaying records from %d to %d of %d" It can translate it to, say, "Affichage des enregistrements de %d à %d parmi %d". Next, printf receive this format string, as if no i18n has occured before.
One problem which might occur using printf/sprintf and gettext is that you have several arguments to printf, and the translated string uses them i a different order than specified in the code. Example: printf(_("There are %d monkeys in the tree, %d are red and %d are green\n"), $monkeys, $red, $green); Consider that translated into the following (both strings in english for simplicity): printf(_("%d green and %d red monkeys in a tree makes a total of %d monkeys\n", $monkeys, $red, $green); This will generate the wrong string. In stead use: printf(_("%3\$d green and %2\$d red monkeys in a tree makes a total of %1\$d monkeys\n", $monkeys, $red, $green); This will use the arguments in the correct order. In .po format: msgid "There are %d monkeys in the tree, %d are red and %d are green\n" msgstr "%3\$d green and %2\$d red monkeys in a tree makes a total of %1\$d monkeys\n" Hope this will be usefull to someone! -- mvh Bjørn Tore Hagen Programmer

« previous php.i18n (#620) next »