Python and i18n

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • João Alfredo

    Python and i18n

    Hi people.

    My LANG environment variable is set to pt_BR.UTF-8 and when raw a date
    command:

    $ date
    Sáb Out 18 11:19:42 BRT 2003
    I get the current date in the apropriate locale.

    Now when i raw the following command in the python interpreter:

    $ python
    Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import time
    >>> time.asctime()[/color][/color][/color]
    'Sat Oct 18 11:25:19 2003'[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    i get the date in US locale format.

    So, does python support the pt_BR locale?? Am I doing something wrong??

    Thanks in advance!

    --
    João Alfredo <jagbdotx@yahoo .com.br>
    dotX


  • Martin v. Löwis

    #2
    Re: Python and i18n

    João Alfredo wrote:

    [color=blue]
    > So, does python support the pt_BR locale?? Am I doing something wrong??[/color]

    Yes, and yes. You have to invoke locale.setlocal e(locale.LC_ALL ,"")
    first.

    Regards,
    Martin

    Comment

    • Peter Otten

      #3
      Re: Python and i18n

      João Alfredo wrote:
      [color=blue]
      > Hi people.
      >
      > My LANG environment variable is set to pt_BR.UTF-8 and when raw a date
      > command:
      >
      > $ date
      > Sáb Out 18 11:19:42 BRT 2003
      > I get the current date in the apropriate locale.
      >
      > Now when i raw the following command in the python interpreter:
      >
      > $ python
      > Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
      > [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
      > Type "help", "copyright" , "credits" or "license" for more information.[color=green][color=darkred]
      >>>> import time
      >>>> time.asctime()[/color][/color]
      > 'Sat Oct 18 11:25:19 2003'[color=green][color=darkred]
      >>>>[/color][/color]
      > i get the date in US locale format.
      >
      > So, does python support the pt_BR locale?? Am I doing something wrong??[/color]

      You have to set the locale explicitly:
      [color=blue][color=green][color=darkred]
      >>> locale.setlocal e(locale.LC_ALL , ("pt", None))[/color][/color][/color]
      'pt_PT.ISO8859-1'

      Even then asctime() will not work as you wish:
      [color=blue][color=green][color=darkred]
      >>> time.asctime()[/color][/color][/color]
      'Sat Oct 18 20:44:29 2003'


      But strftime() has it right:
      [color=blue][color=green][color=darkred]
      >>> time.strftime(" %c")[/color][/color][/color]
      'S\xe1b 18 Out 2003 20:44:44 CEST'[color=blue][color=green][color=darkred]
      >>> print time.strftime(" %c")[/color][/color][/color]
      Sáb 18 Out 2003 20:44:54 CEST[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Peter

      Comment

      Working...