| 23 | |
| 24 | @classmethod |
| 25 | def load(cls, locale: str | Locale) -> Locale: |
| 26 | from importlib import import_module, resources |
| 27 | |
| 28 | if isinstance(locale, Locale): |
| 29 | return locale |
| 30 | |
| 31 | locale = cls.normalize_locale(locale) |
| 32 | if locale in cls._cache: |
| 33 | return cls._cache[locale] |
| 34 | |
| 35 | # Checking locale existence |
| 36 | actual_locale = locale |
| 37 | locale_path = cast(Path, resources.files(__package__).joinpath(actual_locale)) |
| 38 | while not locale_path.exists(): |
| 39 | if actual_locale == locale: |
| 40 | raise ValueError(f"Locale [{locale}] does not exist.") |
| 41 | |
| 42 | actual_locale = actual_locale.split("_")[0] |
| 43 | |
| 44 | m = import_module(f"pendulum.locales.{actual_locale}.locale") |
| 45 | |
| 46 | cls._cache[locale] = cls(locale, m.locale) |
| 47 | |
| 48 | return cls._cache[locale] |
| 49 | |
| 50 | @classmethod |
| 51 | def normalize_locale(cls, locale: str) -> str: |