Internationalization refers to the general process of accommodating
computer-based services to many different "locales" including different
languages.
There exists a uniform way to provide translated text, using the
"gettext" function. The computer user sets a "locale" in an environment variable, and each
internationalized program supplies the correct language, date and
number formatting, etc.
The manual says: "Beside marking the translatable string in the source
code and generating the translations the developers do not have
anything to do themselves."
However, for a web page, the locale of the server is not what the client wants, so a PHP program must
determine the client's preference, and set the appropriate locale
temporarily.
First of all, take note: any LANG
setting must exist as a
"locale" on your server. This can be determined by the terminal
command: locale -a
For example, Osiris has the locales: fr_CA and fr_CA.utf8 The
pattern is language_country.charset, and gettext will gracefully drop
the charset and country to find, for example, fr . So you can use fr for your subdirectory, but
beware, this will not do for the LANG
setting.
<h1>Hello World!</h1>
<h1><?php echo _("Hello World"); ?>!</h1>
echo "Book Title: $title<br>/n";
echo _("Book Title")."$title<br>/n";
// note the dot(.) to concatenate strings
The utility xgettext
goes through a file, recognizing double-quoted strings within calls to
gettext, and produces a file with all the messages, and a place to fill
in translation of each one.
xgettext hitme.phpThis will produce a file messages.po in the same directory
xgettext -d hitme -p translations --from-code=UTF-8 hitme.phpThis produced for me a file named hitme.po in the path /home/jensen/public_html/translations
The po file has some headings (fill in the information) and pairs
like this:
#: hitme.php:15Consider this a template file, if you are doing several translations, and move copies into the subdirectories. I did that. Or just translate it here. Let's assume we just add the french here, in translations.
msgid "your vote has been counted, from "
msgstr ""
#: hitme.php:17
msgid "BOO, HISS, Get lost!"
msgstr ""
#: hitme.php:15Now we need to convert it to .mo format, and put it in the subdirectory, with this command:
msgid "your vote has been counted, from "
msgstr "Votre vote est enregistre, de "
#: hitme.php:17
msgid "BOO, HISS, Get lost!"
msgstr "Desole, ca marche pas!"
msgfmt -v -o fr/LC_MESSAGES/hitme.mo hitme.poAnd we are good to go!
To find your translations, gettext needs to know the TEXTDOMAIN (file name of the
.mo files) and TEXTDOMAINDIR,
where
to
look for the various subdirectories.
It also needs to know what language the user wants. The simplest way is
to put the "locale" in the query string, you can also remember it using
a cookie. It needs to be "put" into the environment, and php must be
informed. Note that in many languages the charset is important, as
there are non-ascii characters.
<?php
header("Content-type: text/html; charset=UTF-8"); // tell the browser
$domain="hitme";
bind_textdomain_codeset($domain, 'UTF-8'); // tell php
bindtextdomain($domain,"./translations"); // ..where to find..
textdomain($domain); // hitme.mo
$locale = $_GET['lang']; // must be fr_CA, not just fr
putenv('LANG='.$locale); // set locale into 3 environment vars.
putenv('LC_ALL='.$locale);
putenv('LC_MESSAGES='.$locale);
setlocale(LC_ALL,$locale); // and tell php about it too
// (this may vary from system to system)
xgettext -p translations hitme.php #giving translations/messages.po
cd translations
msgmerge -U hitme.po messages.po #merges new strings into hitme.po
##-- now go translate..., then:
msgfmt -v -o fr/LC_MESSAGES/hitme.mo hitme.po #as before
Questions? Lin Jensen
Back to contents