1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#
# Copyright (c), 2016-2022, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
#
from collections.abc import Iterable
from typing import cast, Any, Optional, Union
import gettext as _gettext
from pathlib import Path
__all__ = ['activate', 'deactivate', 'gettext']
_translation: Any = None
_installed: bool = False
def activate(localedir: Union[None, str, Path] = None,
languages: Optional[Iterable[str]] = None,
fallback: bool = True,
install: bool = False) -> None:
"""
Activate translation of xmlschema parsing/validation error messages.
:param localedir: a string or Path-like object to locale directory
:param languages: list of language codes
:param fallback: for default fallback mode is activated
:param install: if `True` installs function _() in Python’s builtins namespace
"""
global _translation
global _installed
if localedir is None: # pragma: no cover
localedir = Path(__file__).parent.joinpath('locale').resolve()
translation = _gettext.translation(
domain='xmlschema',
localedir=localedir,
languages=languages,
fallback=fallback,
)
deactivate()
_translation = translation
if install:
_translation.install()
_installed = True
def deactivate() -> None:
"""Deactivate translation of xmlschema parsing/validation error messages."""
global _translation
global _installed
if _installed and _translation is not None:
import builtins
if builtins.__dict__.get('_') == _translation.gettext: # pragma: no cover
builtins.__dict__.pop('_')
_translation = None
_installed = False
def gettext(message: str) -> str:
if _translation is None:
return message
return cast(str, _translation.gettext(message))
|