Entry
How do I use the gettext function?
Oct 17th, 2009 06:14
adel85 adel85, Nathan Wallace, Khimenko Victor
Here's a quick summary that assumes you already have the .mo files
made:
1. if (!(isset($language))) {
$language = select_lang();
}
2. putenv("LANG=$language");
3. bindtextdomain('imp', './locale');
4. textdomain('imp');
Line by line:
1). select_lang() is a horde function that uses HTTP_ACCEPT_LANGUAGE
and
a preference cookie that we set to negotiate what the language should
be. Substitute something like: $language = 'fr' to just test french,
for example.
2). Set the LANG env variable. setlocale($language, LC_ALL) seems like
it ought to work, but it doesn't.
3). Tell gettext that we're looking for imp.mo files, and to search the
"locale" directory that's on the same level as this script. Note that
.mo files really need to be in ./locale/$language/LC_MESSAGES/imp.mo.
4). This seems to be somewhat redundant with the last call, but both
are
needed. Someone who's used gettext in other environments care to
explain it to me? =) It's easy. bindtext is slow initialization
function: it'll scan directories, parse files, etc. textdomain just
changes default domain, is fast and can be called quite often (think
about big project with few subsystems). Of course if you need more then
one domain with lots of switches between two better to use
dgettext/dcgettext instead...
Now, just use echo _("string") instead of echo "string" (or the long
form, gettext("string")), and you should be all set.
I hope this helps you out. Btw, you compile --with-gettext to get the
support - I don't know if it was in 3.0.7, but if not, I'm sure you can
find many other reasons to upgrade. :)
http://www.borsaat.com/vb/f2/
http://www.borsaat.com/vb/f4/
http://www.borsaat.com/vb/f8/
http://www.borsaat.com/vb/f14/
http://www.borsaat.com/vb/f28/
http://www.borsaat.com/vb/f30/
http://www.borsaat.com/vb/f10/
http://www.borsaat.com/vb/f11/
http://www.borsaat.com/vb/f12/
http://www.borsaat.com/vb/f13/
http://www.borsaat.com/vb/f6/
http://www.borsaat.com/vb/f64/
http://www.borsaat.com/vb/f38/
http://www.borsaat.com/vb/f41/
http://www.borsaat.com/vb/f44/
http://www.borsaat.com/vb/f56/
http://www.borsaat.com/vb/f45/
http://www.borsaat.com/vb/f65/
http://www.borsaat.com/vb/f57/
http://www.borsaat.com/vb/f23/
http://www.borsaat.com/vb/f54/
http://www.borsaat.com/vb/f22/
http://www.borsaat.com/vb/f55/
http://www.borsaat.com/vb/f43/
http://www.borsaat.com/vb/f66/
http://www.borsaat.com/vb/f67/
http://www.borsaat.com/vb/f69/
http://www.borsaat.com/vb/f42/
http://www.borsaat.com/vb/f68/
http://www.borsaat.com/vb/f70/
http://www.borsaat.com/vb/f26/
http://www.borsaat.com/vb/
http://www.borsaat.com/vb/f90/