From 8ab34d8838a8f17e2a2034cdfee6c069de513af8 Mon Sep 17 00:00:00 2001 From: Dominik George <nik@naturalnet.de> Date: Tue, 14 Jan 2020 22:55:51 +0100 Subject: [PATCH] Normalise locales --- calendarweek/calendarweek.py | 10 ++++++---- calendarweek/util.py | 10 ++++++++++ pyproject.toml | 2 +- 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 calendarweek/util.py diff --git a/calendarweek/calendarweek.py b/calendarweek/calendarweek.py index 319d2f9..538e249 100644 --- a/calendarweek/calendarweek.py +++ b/calendarweek/calendarweek.py @@ -6,6 +6,8 @@ from datetime import date, datetime, timedelta import locale from typing import Optional, Sequence, Tuple, Union +from .util import normalise_locale + @dataclass class CalendarWeek: @@ -18,28 +20,28 @@ class CalendarWeek: def day_names(cls, loc: Optional[str] = None) -> Tuple[str]: """ Return a tuple of day names for the selected locale. """ - with calendar.different_locale(loc): + with calendar.different_localenormalise_locale(loc): return tuple(calendar.day_name) @classmethod def day_abbrs(cls, loc: Optional[str] = None) -> Tuple[str]: """ Return a tuple of day name abbreviations for the selected locale. """ - with calendar.different_locale(loc): + with calendar.different_localenormalise_locale(loc): return tuple(calendar.day_abbr) @classmethod def month_names(cls, loc: Optional[str] = None) -> Tuple[str]: """ Return a tuple of month names for the selected locale. """ - with calendar.different_locale(loc): + with calendar.different_localenormalise_locale(loc): return tuple(calendar.month_name[1:]) @classmethod def month_abbrs(cls, loc: Optional[str] = None) -> Tuple[str]: """ Return a tuple of month name abbreviations for the selected locale. """ - with calendar.different_locale(loc): + with calendar.different_localenormalise_locale(loc): return tuple(calendar.month_abbr[1:]) @classmethod diff --git a/calendarweek/util.py b/calendarweek/util.py new file mode 100644 index 0000000..3d8c10e --- /dev/null +++ b/calendarweek/util.py @@ -0,0 +1,10 @@ +from locale import locale_alias, locale_encoding_alias +from typing import Optional + + +def normalise_locale(loc: str, enc: Optional[str] = None) -> str: + loc = locale_alias.get(loc, loc) + if enc: + enc = locale_encoding_alias.get(enc.replace("-", ""), enc) + loc = loc.split(".")[0] + "." + enc + return loc diff --git a/pyproject.toml b/pyproject.toml index 55f4cd0..f43ff27 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "calendarweek" -version = "0.3.1" +version = "0.4.0" description = "Utilities for working with calendar weeks in Python and Django" authors = ["Dominik George <nik@naturalnet.de>", "Jonathan Weth <git@jonathanweth.de>"] license = "MIT" -- GitLab