@@ -548,15 +548,28 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
548548class different_locale :
549549 def __init__ (self , locale ):
550550 self .locale = locale
551+ self .oldlocale = None
551552
552553 def __enter__ (self ):
553- self .oldlocale = _locale .getlocale (_locale .LC_TIME )
554+ self .oldlocale = _locale .setlocale (_locale .LC_TIME , None )
554555 _locale .setlocale (_locale .LC_TIME , self .locale )
555556
556557 def __exit__ (self , * args ):
558+ if self .oldlocale is None :
559+ return
557560 _locale .setlocale (_locale .LC_TIME , self .oldlocale )
558561
559562
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+
560573class LocaleTextCalendar (TextCalendar ):
561574 """
562575 This class can be passed a locale name in the constructor and will return
@@ -566,7 +579,7 @@ class LocaleTextCalendar(TextCalendar):
566579 def __init__ (self , firstweekday = 0 , locale = None ):
567580 TextCalendar .__init__ (self , firstweekday )
568581 if locale is None :
569- locale = _locale . getlocale ( _locale . LC_TIME )
582+ locale = _get_default_locale ( )
570583 self .locale = locale
571584
572585 def formatweekday (self , day , width ):
@@ -586,7 +599,7 @@ class LocaleHTMLCalendar(HTMLCalendar):
586599 def __init__ (self , firstweekday = 0 , locale = None ):
587600 HTMLCalendar .__init__ (self , firstweekday )
588601 if locale is None :
589- locale = _locale . getlocale ( _locale . LC_TIME )
602+ locale = _get_default_locale ( )
590603 self .locale = locale
591604
592605 def formatweekday (self , day ):
0 commit comments