Python calendar on Windows: unsupported locale setting

186 Views Asked by At

Windows 10 + Python 3.10

Hi there..

Trying to make my first steps with calender, I got an setlocale error..

This is my code

import locale
locale.setlocale(locale.LC_ALL, 'de_DE')
print(locale.getlocale())

import calendar

kalenderblatt = calendar.LocaleTextCalendar(calendar.MONDAY)
ausgabe = kalenderblatt.formatmonth(12,2022)
print(ausgabe)

This is my output:

('de_DE', 'ISO8859-1')
Traceback (most recent call last):
  File "C:\DATA\Python\Kurs\halloWelt.py", line 533, in <module>
    ausgabe = kalenderblatt.formatmonth(12,2022)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\calendar.py", line 360, in formatmonth
    s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\calendar.py", line 579, in formatmonthname
    with different_locale(self.locale):
  File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\calendar.py", line 554, in __enter__
    _locale.setlocale(_locale.LC_TIME, self.locale)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\locale.py", line 620, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

It seems that there problems with setlocale, but I think it is set correctly.

Changing

locale.setlocale(locale.LC_ALL, 'de_DE')

to

locale.setlocale(locale.LC_ALL, '')

as recommended here on Stackoverflow, throws the same error..

Can anyone help?

1

There are 1 best solutions below

0
Jiří Mauritz On

You need to specify also the encoding: locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")

In case this still does not work, you don't have the locale installed on your system.