note 68853 added to ref.gettext

From: Date: Sun, 13 Aug 2006 11:26:21 +0000
Subject: note 68853 added to ref.gettext
Groups: php.notes 
Request: Send a blank email to php-notes+get-115860@lists.php.net to get a copy of this message
I think that it bears mentioning that most documentation and tutorials you will find say that you should setenv(LC_ALL, $locale), but this only works IF THE LOCALE IS VALID ON THE LOCAL MACHINE. If you are using a locale that does not exist on the system or is not valid (like something returned from the HTTP Accept-Language header, such as 'ja', which is never a valid locale because it does not include the country code JP), you will probably be pulling your hair out for HOURS trying to figure out why the damn thing isn't working. What I have found is that setting the LANGUAGE variable (eg. setenv("LANGUAGE=$locale")) will allow gettext to search inside your locale directories, even if the locale is not actually valid on the local system. So, to summarize: This will work: <?php $locale = 'ja'; // Pretend this came from the Accept-Language header $locale_dir = './locale'; // your .po and .mo files should be at $locale_dir/$locale/LC_MESSAGES/messages.{po,mo} putenv("LANGUAGE=$locale"); bindtextdomain('messages', $locale_dir); textdomain('messages'); ?> This will not work: <?php $locale = 'ja'; $locale_dir = './locale'; putenv("LC_ALL=$locale"); bindtextdomain('messages', $locale_dir); textdomain('messages'); ?> This will also not work: <?php $locale = 'ja'; $locale_dir = './locale'; setlocale(LC_ALL, $locale); bindtextdomain('messages', $locale_dir); textdomain('messages'); ?> Nor will this work: <?php $locale = 'ja'; $locale_dir = './locale'; setlocale(LC_MESSAGES, $locale); bindtextdomain('messages', $locale_dir); textdomain('messages'); ?> For reference, this was tested on PHP 5.1.2 + LigHTTPd 1.4.11 + gettext 0.14.5 on Ubuntu Dapper 6.06. It should be true for any variant of Linux, however, as well as older versions of these software packages. ---- Server IP: 216.194.113.175 Probable Submitter: 70.92.160.23 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/ref.gettext.php Edit -- https://master.php.net/note/edit/68853 Del: integrated -- https://master.php.net/note/delete/68853/integrated Del: useless -- https://master.php.net/note/delete/68853/useless Del: bad code -- https://master.php.net/note/delete/68853/bad+code Del: spam -- https://master.php.net/note/delete/68853/spam Del: non-english -- https://master.php.net/note/delete/68853/non-english Del: in docs -- https://master.php.net/note/delete/68853/in+docs Del: other reasons-- https://master.php.net/note/delete/68853 Reject -- https://master.php.net/note/reject/68853 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115860) next »