diff options
Diffstat (limited to 'libs/trakit/context.py')
-rw-r--r-- | libs/trakit/context.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/libs/trakit/context.py b/libs/trakit/context.py new file mode 100644 index 000000000..9a023ce08 --- /dev/null +++ b/libs/trakit/context.py @@ -0,0 +1,22 @@ +import typing + +import babelfish + + +class Context(dict): + def __init__(self, options: typing.Optional[typing.Mapping[str, typing.Any]] = None): + super().__init__(options or {}) + language = self['expected_language'] if 'expected_language' in self else None + if language and not isinstance(language, babelfish.Language): + language = babelfish.Language.fromietf(str(language)) + self.expected_language: typing.Optional[babelfish.Language] = language + + def accept(self, lang: babelfish.Language): + if self.expected_language is None: + return True + if self.expected_language.alpha3 != lang.alpha3: + return False + if self.expected_language.script and self.expected_language != lang.script: + return False + + return not self.expected_language.country or self.expected_language == lang.country |