|
15 | 15 | "monthcalendar", "prmonth", "month", "prcal", "calendar",
|
16 | 16 | "timegm", "month_name", "month_abbr", "day_name", "day_abbr",
|
17 | 17 | "Calendar", "TextCalendar", "HTMLCalendar", "LocaleTextCalendar",
|
18 |
| - "LocaleHTMLCalendar", "weekheader"] |
| 18 | + "LocaleHTMLCalendar", "weekheader", |
| 19 | + "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", |
| 20 | + "SATURDAY", "SUNDAY"] |
19 | 21 |
|
20 | 22 | # Exception raised for bad input (with string parameter for details)
|
21 | 23 | error = ValueError
|
@@ -546,71 +548,67 @@ def formatyearpage(self, theyear, width=3, css='calendar.css', encoding=None):
|
546 | 548 | class different_locale:
|
547 | 549 | def __init__(self, locale):
|
548 | 550 | self.locale = locale
|
| 551 | + self.oldlocale = None |
549 | 552 |
|
550 | 553 | def __enter__(self):
|
551 |
| - self.oldlocale = _locale.getlocale(_locale.LC_TIME) |
| 554 | + self.oldlocale = _locale.setlocale(_locale.LC_TIME, None) |
552 | 555 | _locale.setlocale(_locale.LC_TIME, self.locale)
|
553 | 556 |
|
554 | 557 | def __exit__(self, *args):
|
| 558 | + if self.oldlocale is None: |
| 559 | + return |
555 | 560 | _locale.setlocale(_locale.LC_TIME, self.oldlocale)
|
556 | 561 |
|
557 | 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 | + |
558 | 573 | class LocaleTextCalendar(TextCalendar):
|
559 | 574 | """
|
560 | 575 | This class can be passed a locale name in the constructor and will return
|
561 |
| - month and weekday names in the specified locale. If this locale includes |
562 |
| - an encoding all strings containing month and weekday names will be returned |
563 |
| - as unicode. |
| 576 | + month and weekday names in the specified locale. |
564 | 577 | """
|
565 | 578 |
|
566 | 579 | def __init__(self, firstweekday=0, locale=None):
|
567 | 580 | TextCalendar.__init__(self, firstweekday)
|
568 | 581 | if locale is None:
|
569 |
| - locale = _locale.getdefaultlocale() |
| 582 | + locale = _get_default_locale() |
570 | 583 | self.locale = locale
|
571 | 584 |
|
572 | 585 | def formatweekday(self, day, width):
|
573 | 586 | with different_locale(self.locale):
|
574 |
| - if width >= 9: |
575 |
| - names = day_name |
576 |
| - else: |
577 |
| - names = day_abbr |
578 |
| - name = names[day] |
579 |
| - return name[:width].center(width) |
| 587 | + return super().formatweekday(day, width) |
580 | 588 |
|
581 | 589 | def formatmonthname(self, theyear, themonth, width, withyear=True):
|
582 | 590 | with different_locale(self.locale):
|
583 |
| - s = month_name[themonth] |
584 |
| - if withyear: |
585 |
| - s = "%s %r" % (s, theyear) |
586 |
| - return s.center(width) |
| 591 | + return super().formatmonthname(theyear, themonth, width, withyear) |
587 | 592 |
|
588 | 593 |
|
589 | 594 | class LocaleHTMLCalendar(HTMLCalendar):
|
590 | 595 | """
|
591 | 596 | This class can be passed a locale name in the constructor and will return
|
592 |
| - month and weekday names in the specified locale. If this locale includes |
593 |
| - an encoding all strings containing month and weekday names will be returned |
594 |
| - as unicode. |
| 597 | + month and weekday names in the specified locale. |
595 | 598 | """
|
596 | 599 | def __init__(self, firstweekday=0, locale=None):
|
597 | 600 | HTMLCalendar.__init__(self, firstweekday)
|
598 | 601 | if locale is None:
|
599 |
| - locale = _locale.getdefaultlocale() |
| 602 | + locale = _get_default_locale() |
600 | 603 | self.locale = locale
|
601 | 604 |
|
602 | 605 | def formatweekday(self, day):
|
603 | 606 | with different_locale(self.locale):
|
604 |
| - s = day_abbr[day] |
605 |
| - return '<th class="%s">%s</th>' % (self.cssclasses[day], s) |
| 607 | + return super().formatweekday(day) |
606 | 608 |
|
607 | 609 | def formatmonthname(self, theyear, themonth, withyear=True):
|
608 | 610 | with different_locale(self.locale):
|
609 |
| - s = month_name[themonth] |
610 |
| - if withyear: |
611 |
| - s = '%s %s' % (s, theyear) |
612 |
| - return '<tr><th colspan="7" class="month">%s</th></tr>' % s |
613 |
| - |
| 611 | + return super().formatmonthname(theyear, themonth, withyear) |
614 | 612 |
|
615 | 613 | # Support for old module level interface
|
616 | 614 | c = TextCalendar()
|
|
0 commit comments