Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pendulum/locales/locale.py: 55%
69 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
1# -*- coding: utf-8 -*-
2from __future__ import unicode_literals
4import os
5import re
7from importlib import import_module
8from typing import Any
9from typing import Optional
10from typing import Union
12from pendulum.utils._compat import basestring
13from pendulum.utils._compat import decode
16class Locale:
17 """
18 Represent a specific locale.
19 """
21 _cache = {}
23 def __init__(self, locale, data): # type: (str, Any) -> None
24 self._locale = locale
25 self._data = data
26 self._key_cache = {}
28 @classmethod
29 def load(cls, locale): # type: (Union[str, Locale]) -> Locale
30 if isinstance(locale, Locale):
31 return locale
33 locale = cls.normalize_locale(locale)
34 if locale in cls._cache:
35 return cls._cache[locale]
37 # Checking locale existence
38 actual_locale = locale
39 locale_path = os.path.join(os.path.dirname(__file__), actual_locale)
40 while not os.path.exists(locale_path):
41 if actual_locale == locale:
42 raise ValueError("Locale [{}] does not exist.".format(locale))
44 actual_locale = actual_locale.split("_")[0]
46 m = import_module("pendulum.locales.{}.locale".format(actual_locale))
48 cls._cache[locale] = cls(locale, m.locale)
50 return cls._cache[locale]
52 @classmethod
53 def normalize_locale(cls, locale): # type: (str) -> str
54 m = re.match("([a-z]{2})[-_]([a-z]{2})", locale, re.I)
55 if m:
56 return "{}_{}".format(m.group(1).lower(), m.group(2).lower())
57 else:
58 return locale.lower()
60 def get(self, key, default=None): # type: (str, Optional[Any]) -> Any
61 if key in self._key_cache:
62 return self._key_cache[key]
64 parts = key.split(".")
65 try:
66 result = self._data[parts[0]]
67 for part in parts[1:]:
68 result = result[part]
69 except KeyError:
70 result = default
72 if isinstance(result, basestring):
73 result = decode(result)
75 self._key_cache[key] = result
77 return self._key_cache[key]
79 def translation(self, key): # type: (str) -> Any
80 return self.get("translations.{}".format(key))
82 def plural(self, number): # type: (int) -> str
83 return decode(self._data["plural"](number))
85 def ordinal(self, number): # type: (int) -> str
86 return decode(self._data["ordinal"](number))
88 def ordinalize(self, number): # type: (int) -> str
89 ordinal = self.get("custom.ordinal.{}".format(self.ordinal(number)))
91 if not ordinal:
92 return decode("{}".format(number))
94 return decode("{}{}".format(number, ordinal))
96 def match_translation(self, key, value):
97 translations = self.translation(key)
98 if value not in translations.values():
99 return None
101 return {v: k for k, v in translations.items()}[value]
103 def __repr__(self):
104 return "{}('{}')".format(self.__class__.__name__, self._locale)