Localized month names?

4 views
Skip to first unread message

Jarek Zgoda

unread,
Mar 13, 2006, 4:54:37 PM3/13/06
to
How do I get a list of localized month names for current locale? The
first thing that came to my mind was an ugly hack:

import datetime
for i in range(12):
# as I remember, all months in 2005 had 1st in days
datetime.date(2005, i + 1, 1).strftime('%B')

but I am sure there is a better way...

--
Jarek Zgoda
http://jpa.berlios.de/

Fredrik Lundh

unread,
Mar 13, 2006, 5:20:29 PM3/13/06
to pytho...@python.org
Jarek Zgoda wrote:

> How do I get a list of localized month names for current locale? The
> first thing that came to my mind was an ugly hack:
>
> import datetime
> for i in range(12):
> # as I remember, all months in 2005 had 1st in days
> datetime.date(2005, i + 1, 1).strftime('%B')

doesn't look very ugly to me...

the strftime tables are hidden deep inside the C library, but I guess you
could use the _strptime implementation module to dig out the information
you're after:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "sv_SE")
'sv_SE'
>>> import _strptime
>>> vars(_strptime.LocaleTime())
{'lang': ('sv_SE', 'ISO8859-1'), 'am_pm': ['', ''], 'f_month': ['', 'januari',
'februari', 'mars', 'april', 'maj', 'juni', 'juli', 'augusti', 'september', 'oktober',
'november', 'december'], 'LC_date': '%Y-%m-%d', 'a_weekday': ['m\xe5n',
'tis', 'ons', 'tor', 'fre', 'l\xf6r', 's\xf6n'], 'f_weekday': ['m\xe5ndag', 'tisdag',
'onsdag', 'torsdag', 'fredag', 'l\xf6rdag', 's\xf6ndag'], 'LC_date_time':
'%a %d %b %Y %H.%M.%S', 'timezone': (frozenset(['utc', 'cet', 'gmt']),
frozenset(['cest'])), 'a_month': ['', 'jan', 'feb', 'mar', 'apr', 'maj', 'jun',
'jul', 'aug', 'sep', 'okt', 'nov', 'dec'], 'LC_time': '%H.%M.%S'}

(I'm pretty sure _strptime uses your ugly hack-approach to extract this
information from the C library...)

hope this helps!

</F>

Benji York

unread,
Mar 13, 2006, 5:45:56 PM3/13/06
to Jarek Zgoda, pytho...@python.org
Jarek Zgoda wrote:
> How do I get a list of localized month names for current locale? The
> first thing that came to my mind was an ugly hack:

>>> import locale
>>> locale.nl_langinfo(locale.MON_1)
'January'

--
Benji York

Sibylle Koczian

unread,
Mar 14, 2006, 5:04:33 AM3/14/06
to
Benji York schrieb:

What did you leave out? I get

Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
locale.nl_langinfo(locale.MON_1)
AttributeError: 'module' object has no attribute 'nl_langinfo'

(Python 2.4, german Windows XP Pro)

Moreover, 'January' is probably not localized.

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Sibylle...@Bibliothek.Uni-Augsburg.DE

Peter Otten

unread,
Mar 14, 2006, 6:44:26 AM3/14/06
to
Sibylle Koczian wrote:

> Benji York schrieb:
>> Jarek Zgoda wrote:
>>
>>> How do I get a list of localized month names for current locale? The
>>> first thing that came to my mind was an ugly hack:
>>
>>
>>>>> import locale
>>>>> locale.nl_langinfo(locale.MON_1)
>> 'January'
>>
>
> What did you leave out?

Nothing, most likely.

> I get
>
> Traceback (most recent call last):
> File "<pyshell#3>", line 1, in -toplevel-
> locale.nl_langinfo(locale.MON_1)
> AttributeError: 'module' object has no attribute 'nl_langinfo'
>
> (Python 2.4, german Windows XP Pro)
>
> Moreover, 'January' is probably not localized.

Python 2.4.2 (#4, Nov 19 2005, 13:25:59)
[GCC 3.3.3 (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.nl_langinfo(locale.MON_1)
'January'
>>> locale.setlocale(locale.LC_ALL, "")
'de_DE.UTF-8'
>>> locale.nl_langinfo(locale.MON_1)
'Januar'


Peter

Kent Johnson

unread,
Mar 14, 2006, 8:29:26 AM3/14/06
to
Peter Otten wrote:

> Sibylle Koczian wrote:
>>What did you leave out?
>
>
> Nothing, most likely.
>
>
>>I get
>>
>>Traceback (most recent call last):
>> File "<pyshell#3>", line 1, in -toplevel-
>> locale.nl_langinfo(locale.MON_1)
>>AttributeError: 'module' object has no attribute 'nl_langinfo'
>>
>>(Python 2.4, german Windows XP Pro)
>>
>>Moreover, 'January' is probably not localized.
>
>
> Python 2.4.2 (#4, Nov 19 2005, 13:25:59)
> [GCC 3.3.3 (SuSE Linux)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>>import locale
>>>>locale.nl_langinfo(locale.MON_1)

From the docs:
nl_langinfo( option)

Return some locale-specific information as a string. This function
is not available on all systems, and the set of possible options might
also vary across platforms.

It doesn't seem to be available on Windows:
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32


Type "help", "copyright", "credits" or "license" for more information.

In [1]: import locale

In [2]: locale.nl_langinfo(locale.MON_1)
------------------------------------------------------------


Traceback (most recent call last):

File "<ipython console>", line 1, in ?


AttributeError: 'module' object has no attribute 'nl_langinfo'

Kent

Reply all
Reply to author
Forward
0 new messages