Re: gettext and variable parsing

Bjørn Tore Hagen <[email protected]>
Newsgroups gmane.comp.php.internationalization
Organization Kreativ Software
Message-ID <[email protected]>
On Mon, 11 Aug 2003 12:16:12 +0200, Christophe Chisogne 
<[email protected]> 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

-- 
PHP Internationalization Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.