On Wednesday, July 24, 2002, at 10:57  AM, Hans Lellelid wrote:

> For translating text that might have dynamic values, I build a wrapper
> function for gettext() -- note that if you do this you'll probably need
> to use a shell/perl script to replace the wrapper function name with
> gettext() or _() before feeding it into thee xgettext tool.

I'm nearly finished with a huge gettext() based 
internationalization...there are 650+ static strings in my pot file. But 
I was a bit surprised to see your wrapper function, Hans.

The same result can be achieved using standard printf() functions around 
the gettext() call. See the sprintf() manual page, especially the 
section on "Argument swapping". (This applies only to PHP 4.0.6 or 
later).

Example:
printf(gettext("Search found %1\$d pages for %2\$s"),$count,$query);

will achieve the same results without writing your own wrapper. A 
translation would then look like

msgid "Search found %1\$d pages for %2\$s"
msgstr "En cherchant le database pour %2\$s, on a trouve %1\$d"

Other general app notes
-----------------------
1) I also have my app output a META content-type header with the 
appropriate charset declaration, based on the user's selected language.

2) I modified my submission forms as well, to include "accept-charset=" 
with any character sets I support to be permissible.

3) I store the user-selected language as a cookie, though it can be 
overridden by a $lang HTTP_GET_VAR. My "header.inc" file, which is 
included at the top of each page, has a section like this:

///////////////////////////////////////////////////////////////
// Language localization based on cookie variable or passed in
///////////////////////////////////////////////////////////////
$g_default_lang = 'en_US';
if (isset($lang)) {
        // GET_VARS trumps COOKIE_VARS
        if (isset($HTTP_GET_VARS['lang'])) {
                $g_lang = $HTTP_GET_VARS['lang'];
        } else {
                $g_lang = $HTTP_COOKIE_VARS['lang'];
        }
} else {
        $g_lang = $g_default_lang;
}
// Set language to desired language
putenv("LANG=$g_lang");
$checklang = setlocale(LC_ALL, $g_lang);
if ($checklang === FALSE) {
        error_log('Failure setting language to '.$g_lang);
} else {
        error_log("Setting language to $g_lang");
}
// Specify location of translation tables
bindtextdomain ("messages", "./locale");
textdomain ("messages");
setcookie('lang',$g_lang,time()+(60 * 60 * 24 * 
60),'/',$GLOBALS['HTTP_HOST']);
///////////////////////////////////////////////////////////////
// End language localization
///////////////////////////////////////////////////////////////

You may want to replace the $HTTP_GET_VARS, etc with the new 
superglobals, but the gist is still the same.

Cheers,
spud.

-------------------------------------------------------------------
a.h.s. boy
spud(at)nothingness.org            "as yes is to if,love is to yes"
http://www.nothingness.org/
-------------------------------------------------------------------


-- 
PHP Internationalization Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to