[LON-CAPA-dev] I18N and dates

Jeremy Bowers lon-capa-dev@mail.lon-capa.org
Mon, 22 Sep 2003 17:07:36 -0500


Gerd Kortemeyer wrote:
> As a very very first cut, check out the code I already put into 
> "humanTime" to either use your code (in case that the language starts 
> with "en") or "localtime" else.
> 
> - Gerd.

*grin* That'll teach me to open my mouth without checking out the latest 
CVS code ;-)

Looks like "man perllocale" has the goods...

-------------

You can switch locales as often as you wish at run time with the 
POSIX::setlocale() function:

         # This functionality not usable prior to Perl 5.004
         require 5.004;

         # Import locale-handling tool set from POSIX module.
         # This example uses: setlocale -- the function call
         #                    LC_CTYPE -- explained below
         use POSIX qw(locale_h);

         # query and save the old locale
         $old_locale = setlocale(LC_CTYPE);

         setlocale(LC_CTYPE, "fr_CA.ISO8859-1");
         # LC_CTYPE now in locale "French, Canada, codeset ISO 8859-1"

         setlocale(LC_CTYPE, "");
         # LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
         # environment variables.  See below for documentation.

         # restore the old locale
         setlocale(LC_CTYPE, $old_locale);

--------------

Me again. That "setlocale" stuff looks a little dangerous, since it'll 
almost certainly propogate to the next request for a handler. Maybe a 
"cleanup" handler that resets the locale to a known state is called for, 
and then each handler can fiddle with it without fear it will leak into 
the next request.

Also note this puts us at the mercy of the locales on the system. AFAIK 
this is OK, *every* Linux system I've ever seen has a full locale 
database (in fact, it composes ~10% of the smallest Gentoo install CD 
image IIRC; on my system /usr/share/locale is 101MB now, it's not as 
large on a smaller install).

Also see the security notes on that manpage; I don't see anything 
terrifying but the more eyes the better.

http://www.perldoc.com/perl5.6/pod/perllocale.html#SECURITY