faqts : Computers : Programming : Languages : PHP : Common Problems : Internationalization

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

28 of 41 people (68%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I use the gettext function?

May 17th, 2000 00:50
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. :)