@@ -548,15 +548,28 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
548
548
class different_locale :
549
549
def __init__ (self , locale ):
550
550
self .locale = locale
551
+ self .oldlocale = None
551
552
552
553
def __enter__ (self ):
553
- self .oldlocale = _locale .getlocale (_locale .LC_TIME )
554
+ self .oldlocale = _locale .setlocale (_locale .LC_TIME , None )
554
555
_locale .setlocale (_locale .LC_TIME , self .locale )
555
556
556
557
def __exit__ (self , * args ):
558
+ if self .oldlocale is None :
559
+ return
557
560
_locale .setlocale (_locale .LC_TIME , self .oldlocale )
558
561
559
562
563
+ def _get_default_locale ():
564
+ locale = _locale .setlocale (_locale .LC_TIME , None )
565
+ if locale == "C" :
566
+ with different_locale ("" ):
567
+ # The LC_TIME locale does not seem to be configured:
568
+ # get the user preferred locale.
569
+ locale = _locale .setlocale (_locale .LC_TIME , None )
570
+ return locale
571
+
572
+
560
573
class LocaleTextCalendar (TextCalendar ):
561
574
"""
562
575
This class can be passed a locale name in the constructor and will return
@@ -566,7 +579,7 @@ class LocaleTextCalendar(TextCalendar):
566
579
def __init__ (self , firstweekday = 0 , locale = None ):
567
580
TextCalendar .__init__ (self , firstweekday )
568
581
if locale is None :
569
- locale = _locale . getlocale ( _locale . LC_TIME )
582
+ locale = _get_default_locale ( )
570
583
self .locale = locale
571
584
572
585
def formatweekday (self , day , width ):
@@ -586,7 +599,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
586
599
def __init__ (self , firstweekday = 0 , locale = None ):
587
600
HTMLCalendar .__init__ (self , firstweekday )
588
601
if locale is None :
589
- locale = _locale . getlocale ( _locale . LC_TIME )
602
+ locale = _get_default_locale ( )
590
603
self .locale = locale
591
604
592
605
def formatweekday (self , day ):
0 commit comments