PHP version 4
Dev machine : Gnu/Linux Kubuntu Dapper

Hello, new here.
I was going to ask a question, but I did just a few more experiments and
suddenly I got this i18n thing working; so I thought I'd document my take
on the situation. The online stuff, like that O'Reilly tutorial, just plain
did not work for me. Go figure!

I am sure there are things I am missing, and others are just plain wrong;
all knowledge is welcome :)

My initial beef with Gettext:
=============================
Does gettext require the system (server) to have the locale you need (i.e.
many if not all of them) installed in some way? If so, and you have no
access to the server, how can you translate your pages for the Klingon
behind his browser?

It turns out that the server doesn't need any special locales, you can
make-up the names of the languages you want to support, this is how I did
it.

The PHP code
============
A simple page with a combo-box to choose a "language".

<?php
$text = "";
if ($_POST["from"] == "go") {

#If under bash I say : LANGUAGE=klingon
#AND I have: setlocale(LC_MESSAGES,'')
#THEN it translates properly

$lang = $_POST["lang"];

putenv("LANGUAGE=$lang"); //This works
#putenv("LANG=$lang"); // This DOES NOT WORK

//Just set it blank. If you put your lang into this func
//it will NOT work.
setlocale(LC_MESSAGES,'');

$domain = "appname"; //What you named your .po files
bindtextdomain($domain,"./locale"); //Where you put the locale dir.
textdomain($domain);
//So, mo's go ionto ./locale/$lang/LC_MESSAGES/appname.mo

$text = _("This is a test of gettext in PHP");
}
?>
<html><head><meta charset="utf-8"><title>langtest</title></head><body>
<FORM action="" method="POST" accept-charset="utf-8">
  <SELECT name="lang">
    <option>--choose--</option> 
    <option>english</option>
    <option>af_ZA.UTF-8</option>
    <option>xhosa</option>
    <option>klingon</option>
  </SELECT>
  <INPUT type="submit" name="from" value="go">
</FORM>

<h1><?=$text?></h1>
</body></html>

Details & mysteries
===================
The thing that really puzzles me right now is that putenv statement. From
what the manual says it should NOT work when "safe mode" is off, which it
is on my apache and on my test server online. Still, gift-horses and
mouths ...

The important part is that putenv command. Use "LANGUAGE=" not "LANG=".

The next step is to use setlocale, but do not put your language into the
second parameter, if you do then things go mad. (I presume this is the part
where the server needs the locale installed, IF you use that 2nd param.)

Then I made a folder called "locale"
Under that I made three folders called:
"af_ZA.UTF-8" -- To be pedantic
"xhosa" -- Starting to experiment
"klingon" -- Gone right off the rails.

Within each a "LC_MESSAGES" folder.
Within those, the appropriate .mo files. (I won't expand, there are many
tuts online re gettext commands)

At the end I include the .po files. 

Conclusion
==========
So, using this code I can have translation .po files for any number of
randomly named languages. 
I will look into what the browser sends by way of locale information,
perhaps I can use a code from that, otherwise I can have my users choose a
language and dictate the codes as I like to.

hth
Donn.

I include only the translations, not the whole .po file.

appname.af.po
#: gettexttest.php:6
msgid "This is a test of gettext in PHP"
msgstr "Dit is 'n toets van gettext in PHP"

appname.xhosa.po
#: gettexttest.php:6
msgid "This is a test of gettext in PHP"
msgstr "ngaZama uGettext ipakathi iPHP"

appname.klingon.po
#: gettexttest.php:6
msgid "This is a test of gettext in PHP"
msgstr "Graaak mur zgrkk grrr aaaargh!"

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

Reply via email to