Connecting Tech Pros Worldwide Forums | Help | Site Map

Localized month names?

Jarek Zgoda
Guest
 
Posts: n/a
#1: Mar 13 '06
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
Guest
 
Posts: n/a
#2: Mar 13 '06

re: Localized month names?


Jarek Zgoda wrote:
[color=blue]
> 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')[/color]

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:
[color=blue][color=green][color=darkred]
>>> import locale
>>> locale.setlocale(locale.LC_ALL, "sv_SE")[/color][/color][/color]
'sv_SE'[color=blue][color=green][color=darkred]
>>> import _strptime
>>> vars(_strptime.LocaleTime())[/color][/color][/color]
{'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
Guest
 
Posts: n/a
#3: Mar 13 '06

re: Localized month names?


Jarek Zgoda wrote:[color=blue]
> 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:[/color]
[color=blue][color=green][color=darkred]
>>> import locale
>>> locale.nl_langinfo(locale.MON_1)[/color][/color][/color]
'January'

--
Benji York
Sibylle Koczian
Guest
 
Posts: n/a
#4: Mar 14 '06

re: Localized month names?


Benji York schrieb:[color=blue]
> Jarek Zgoda wrote:
>[color=green]
>> 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:[/color]
>
>[color=green][color=darkred]
>>>> import locale
>>>> locale.nl_langinfo(locale.MON_1)[/color][/color]
> 'January'
>[/color]

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.Koczian@Bibliothek.Uni-Augsburg.DE
Peter Otten
Guest
 
Posts: n/a
#5: Mar 14 '06

re: Localized month names?


Sibylle Koczian wrote:
[color=blue]
> Benji York schrieb:[color=green]
>> Jarek Zgoda wrote:
>>[color=darkred]
>>> 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:[/color]
>>
>>[color=darkred]
>>>>> import locale
>>>>> locale.nl_langinfo(locale.MON_1)[/color]
>> 'January'
>>[/color]
>
> What did you leave out?[/color]

Nothing, most likely.
[color=blue]
> 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.[/color]

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.[color=blue][color=green][color=darkred]
>>> import locale
>>> locale.nl_langinfo(locale.MON_1)[/color][/color][/color]
'January'[color=blue][color=green][color=darkred]
>>> locale.setlocale(locale.LC_ALL, "")[/color][/color][/color]
'de_DE.UTF-8'[color=blue][color=green][color=darkred]
>>> locale.nl_langinfo(locale.MON_1)[/color][/color][/color]
'Januar'


Peter
Kent Johnson
Guest
 
Posts: n/a
#6: Mar 14 '06

re: Localized month names?


Peter Otten wrote:[color=blue]
> Sibylle Koczian wrote:[color=green]
>>What did you leave out?[/color]
>
>
> Nothing, most likely.
>
>[color=green]
>>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.[/color]
>
>
> 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.
>[color=green][color=darkred]
>>>>import locale
>>>>locale.nl_langinfo(locale.MON_1)[/color][/color][/color]

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
Closed Thread