How to force formatted date (month) language ?

Discussion in 'Perl Misc' started by Yohan N. Leder, Jul 7, 2006.

  1. Hi,

    I would like to produce a formatted GMT date & time string which will
    depend of a user choice in a web form (in a CGI framework) about human
    language he prefers (e.g. a french native may prefer to choose
    'english'). I wish that it works under both Unix and Windows.

    Here is a test script on which I'm trying to go through POSIX::setlocale
    () as described in 'perldoc perllocale' about LC_TIME :

    #!/usr/bin/perl -w
    use strict;
    use POSIX qw(locale_h strftime);
    print "Content-type: text/html\n\n";
    my $datetime;

    # english
    setlocale(LC_TIME, "en_US.ISO8859-1");
    $datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
    print "<p>ENGLISH =>".$datetime."</p>";

    # french
    setlocale(LC_TIME, "fr_FR.ISO8859-1");
    $datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
    print "<p>FRENCH => ".$datetime."</p>";
    exit 0;

    But it doesn't seems to work because it just displays (in client
    browser) the same french string in both cases (tested with ActivePerl
    5.8.8 under Windows 2K FR) :

    ENGLISH => 07 juil. 2006 @ 21:18:35 GMT
    FRENCH => 07 juil. 2006 @ 21:18:35 GMT

    When I would like to obtain something like :

    ENGLISH => 07 Jul 2006 @ 21:18:35 GMT
    FRENCH => 07 juil. 2006 @ 21:18:35 GMT

    I've also tried with Perl 5.00503 under FreeBSD US, and, this time, it
    gives english strings only (the only difference is the minus 'j' in
    French string, but still with English word):

    ENGLISH =>07 Jul 2006 @ 21:41:14 GMT
    FRENCH => 07 jul 2006 @ 21:41:14 GMT

    How to proceed to get this string in English or French (i.e. month here)
    for sure (ie. depending of a language value extracted from STDIN about
    web form POST in the real script) ?
    Yohan N. Leder, Jul 7, 2006
    #1
    1. Advertising

  2. Yohan N. Leder

    Sisyphus Guest

    "Yohan N. Leder" <> wrote in message
    news:...
    > Hi,
    >
    > I would like to produce a formatted GMT date & time string which will
    > depend of a user choice in a web form (in a CGI framework) about human
    > language he prefers (e.g. a french native may prefer to choose
    > 'english'). I wish that it works under both Unix and Windows.
    >
    > Here is a test script on which I'm trying to go through POSIX::setlocale
    > () as described in 'perldoc perllocale' about LC_TIME :
    >
    > #!/usr/bin/perl -w
    > use strict;
    > use POSIX qw(locale_h strftime);
    > print "Content-type: text/html\n\n";
    > my $datetime;
    >
    > # english
    > setlocale(LC_TIME, "en_US.ISO8859-1");


    For Win32 (and perhaps others ?), try:
    setlocale(LC_TIME, "English_USA.1252");

    > $datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
    > print "<p>ENGLISH =>".$datetime."</p>";
    >
    > # french
    > setlocale(LC_TIME, "fr_FR.ISO8859-1");
    > $datetime = strftime "%d %b %Y @ %H:%M:%S GMT", gmtime;
    > print "<p>FRENCH => ".$datetime."</p>";
    > exit 0;
    >


    I'm on an "English" Win32 Machine - and, with the second strftime() call, I
    still got 'Jul' instead of 'juil.' However, when I changed your second
    setlocale() call to:
    setlocale(LC_TIME, "French_France.1252");

    I then got the desired 'juil.' in the output.

    Cheers,
    Rob
    Sisyphus, Jul 8, 2006
    #2
    1. Advertising

  3. In article <44af432e$0$25284$>, sisyphus1
    @nomail.afraid.org says...
    > For Win32 (and perhaps others ?), try:
    > setlocale(LC_TIME, "English_USA.1252");
    > [...]
    > I'm on an "English" Win32 Machine - and, with the second strftime() call, I
    > still got 'Jul' instead of 'juil.' However, when I changed your second
    > setlocale() call to:
    > setlocale(LC_TIME, "French_France.1252");
    >
    > I then got the desired 'juil.' in the output.
    >
    > Cheers,
    > Rob


    Effectively, I've tried 'setlocale(LC_TIME, "English_USA.1252");' and
    'setlocale(LC_TIME, "French_France.1252");' under Win 2K FR and it
    respectively gives 'Jul' and "juil.' as expected. Does it means the
    'en_US' and 'fr_FR' are not supported under Win32 ? Thanks ;)

    Also, I've done the same test under a FreeBSD US and it still gives :
    ENGLISH => 08 Jul 2006 @ 17:26:58 GMT
    FRENCH => 08 Jul 2006 @ 17:26:58 GMT

    What's the right LC_TIME specification for Unix flavors and, more
    generally, no-Win32 operating systems ?
    Yohan N. Leder, Jul 8, 2006
    #3
  4. Yohan N. Leder

    DJ Stunks Guest

    Yohan N. Leder wrote:
    > In article <44af432e$0$25284$>, sisyphus1
    > @nomail.afraid.org says...
    > > For Win32 (and perhaps others ?), try:
    > > setlocale(LC_TIME, "English_USA.1252");
    > > [...]
    > > I'm on an "English" Win32 Machine - and, with the second strftime() call, I
    > > still got 'Jul' instead of 'juil.' However, when I changed your second
    > > setlocale() call to:
    > > setlocale(LC_TIME, "French_France.1252");
    > >
    > > I then got the desired 'juil.' in the output.
    > >
    > > Cheers,
    > > Rob

    >
    > Effectively, I've tried 'setlocale(LC_TIME, "English_USA.1252");' and
    > 'setlocale(LC_TIME, "French_France.1252");' under Win 2K FR and it
    > respectively gives 'Jul' and "juil.' as expected. Does it means the
    > 'en_US' and 'fr_FR' are not supported under Win32 ? Thanks ;)


    To build up a locale string in Win32 follow the instructions on this
    page:

    http://msdn.microsoft.com/library/d...ib/html/_crt_language_and_country_strings.asp

    The possible choices for lang are listed:

    http://msdn.microsoft.com/library/d...ry/en-us/vclib/html/_crt_language_strings.asp

    The optional Country/Region and Code Pages supported by Windows are:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_country_strings.asp
    http://www.microsoft.com/globaldev/reference/wincp.mspx

    > Also, I've done the same test under a FreeBSD US and it still gives :
    > ENGLISH => 08 Jul 2006 @ 17:26:58 GMT
    > FRENCH => 08 Jul 2006 @ 17:26:58 GMT
    >
    > What's the right LC_TIME specification for Unix flavors and, more
    > generally, no-Win32 operating systems ?


    as perllocale states, you should be able to produce a list of supported
    locales on a UNIX-ish OS by using
    $ locale -a

    in this case, I would say the easiest way to get the same script to
    produce similar** results is to simply use 'english' and 'french' as
    your locale strings.

    HTH,
    -jp

    Note **: on redhat linux, 'french' produced "jui" rather than "juil.".
    DJ Stunks, Jul 8, 2006
    #4
  5. Yohan N. Leder wrote:

    > Effectively, I've tried 'setlocale(LC_TIME, "English_USA.1252");' and
    > 'setlocale(LC_TIME, "French_France.1252");' under Win 2K FR and it
    > respectively gives 'Jul' and "juil.' as expected. Does it means the
    > 'en_US' and 'fr_FR' are not supported under Win32 ?


    Both English and French locales should be available.

    > Also, I've done the same test under a FreeBSD US and it still gives :
    > ENGLISH => 08 Jul 2006 @ 17:26:58 GMT
    > FRENCH => 08 Jul 2006 @ 17:26:58 GMT


    1252 is a Microsoft proprietary code page and thus not very useful for
    general use across various OS's.

    The code of your first post should be okay, but there is something
    going on you're not aware of.

    The "official" French month abbreviations are the following:
    janvier : janv.
    février : févr.
    mars : mars
    avril : avr.
    mai : mai
    juin : juin
    juillet : juil. ou juill.
    août : août
    septembre : sept.
    octobre : oct.
    novembre : nov.
    décembre : déc.
    (http://fr.wikipedia.org/wiki/Mois)

    It seems that MS' French_France.1252 follows these rules. But if we
    want to force a three-char month notation (more English & informatics
    style), then Houston has a problem:

    French for June = Juin
    French for July = Juillet
    So, the three first chars are 'jui' for both months.

    The logic would then be to take the first following character, like
    Juin = jui
    Juillet = jul

    > What's the right LC_TIME specification for Unix flavors and, more
    > generally, no-Win32 operating systems ?


    General UNIX/Linux: setlocale('LC_TIME', 'fr_FR.ISO_8859-1');
    Win32: setlocale('LC_TIME', 'fr');
    Solaris: setlocale("LC_TIME", "fr");
    FreeBSD: setlocale("LC_TIME", "fr_FR.ISO8859-1");
    (out of http://www.oscommerce-fr.info/faq/qa_info.php?qID=52)

    I also met:
    French_France.1252
    french.ISO_8859-1
    french
    fr_FR

    A last super-safe option is to switch to the month number (like '7' in
    stead of July). And then manually code out the part to tie it to the
    actual month name/abbreviation you wish (according to the language the
    user specified on the web page).

    --
    Bart
    Bart Van der Donck, Jul 8, 2006
    #5
  6. In article <>,
    says...
    > in this case, I would say the easiest way to get the same script to
    > produce similar** results is to simply use 'english' and 'french' as
    > your locale strings.
    >


    It would be effectively a middle way, but, after thought I think I'll go
    to the Bart suggestion : convert numeric month to abbreviated month name
    by myself. Thanks anyway.
    Yohan N. Leder, Jul 9, 2006
    #6
  7. In article <>,
    says...
    > A last super-safe option is to switch to the month number (like '7' in
    > stead of July). And then manually code out the part to tie it to the
    > actual month name/abbreviation you wish (according to the language the
    > user specified on the web page).
    >


    Hmm, understood your demonstration about jui/jul ambiguousness... I'll
    take the super-sage way ;) So, just a question : do you know the
    "official" English month abbreviations ? Not found on Wikipedia as for
    French side.

    Thanks, Bart
    Yohan N. Leder, Jul 9, 2006
    #7
  8. Yohan N. Leder wrote:

    >[...]
    > Hmm, understood your demonstration about jui/jul ambiguousness... I'll
    > take the super-sage way ;) So, just a question : do you know the
    > "official" English month abbreviations ? Not found on Wikipedia as for
    > French side.


    Me neither. But it's pretty safe to assume that those are 'jan feb mar
    apr may jun jul aug sep oct nov dec'.

    But, same as in French, it looks like these are no "hard" standards.

    --
    Bart
    Bart Van der Donck, Jul 9, 2006
    #8
  9. In article <>,
    says...
    > Me neither. But it's pretty safe to assume that those are 'jan feb mar
    > apr may jun jul aug sep oct nov dec'.
    >


    Adopted ! Maybe with upper case at begin ;-)
    Yohan N. Leder, Jul 9, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Maciejczak
    Replies:
    1
    Views:
    1,966
    Matthias Kaeppler
    Apr 23, 2005
  2. Bill
    Replies:
    10
    Views:
    712
    Fulvio
    Apr 13, 2006
  3. Replies:
    1
    Views:
    425
    Steve W. Jackson
    Dec 22, 2006
  4. Mufasa
    Replies:
    0
    Views:
    381
    Mufasa
    Sep 5, 2008
  5. VYAS ASHISH M-NTB837

    RE: Convert month name to month number faster

    VYAS ASHISH M-NTB837, Jan 6, 2010, in forum: Python
    Replies:
    0
    Views:
    598
    VYAS ASHISH M-NTB837
    Jan 6, 2010
Loading...

Share This Page