Localized month names?

J

Jarek Zgoda

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...
 
F

Fredrik Lundh

Jarek said:
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:
{'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>
 
S

Sibylle Koczian

Benji said:
'January'

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.
 
P

Peter Otten

Sibylle said:
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.'Januar'


Peter
 
K

Kent Johnson

Peter said:
Sibylle said:
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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top