note 15502 added to ref.gettext
| From: | Michele dot Manzato at verona dot miz dot it | Date: | Fri, 14 Sep 2001 07:52:19 +0000 |
| Subject: | note 15502 added to ref.gettext | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-18012@lists.php.net to get a copy of this message | ||
Gettext is great, but there are a few caveats you have to consider in order to make it work.
Unfortunately the gettext docs isn't so plain and clear...
The .mo file created with the gettext utilities must be:
[bindtextdomain's dir]/[language]/LC_MESSAGES/[domain].mo
otherwise gettext() function will fail to find it (this is true in win32, don't know about
Un*ces). By the way you don't get any error message, the strings will simply remain
untranslated.
Second, you must make sure which is the current directory if you use a relative path in
bindtextdomain. On some systems the script directory isn't the current directory, so you have
to chdir() there.
Then, of course, make sure that the appropriate gettext extension are loaded by PHP by looking at
the php configuration file.
Here is some sample code:
// Change to the script directory
$path = dirname(getenv(PATH_TRANSLATED));
chdir($path);
// Set the language as 'it'
$language = 'it';
putenv("LANG=$language");
setlocale(LC_ALL, $language);
// Set the text domain as 'mydomain'
$domain = 'mydomain';
bindtextdomain("$domain", "./locale");
textdomain("$domain");
// The .mo file searched is:
// ./locale/it/LC_MESSAGES/mydomain.mo
echo gettext("Hello world!");
Have fun!
Michele
http://www.php.net/manual/en/ref.gettext.php