diff options
Diffstat (limited to 'src/hydrilla/util/_util.py')
-rw-r--r-- | src/hydrilla/util/_util.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/hydrilla/util/_util.py b/src/hydrilla/util/_util.py index 9bebdc1..c7e9f47 100644 --- a/src/hydrilla/util/_util.py +++ b/src/hydrilla/util/_util.py @@ -26,6 +26,8 @@ import re import json +import locale +import gettext from pathlib import Path from typing import Optional @@ -71,7 +73,7 @@ def strip_json_comments(text: str) -> str: # ignore this error, let json module report it stripped = line elif len(match[2]) == 1: - raise json.JSONDecodeError('bad comment', text, + raise json.JSONDecodeError(_('bad_comment'), text, processed + len(match[1])) else: stripped = match[1] @@ -127,3 +129,29 @@ def validator_for(schema_filename: str) -> Draft7Validator: """ return Draft7Validator(resolver.resolve(schema_filename)[1], resolver=resolver) + +def get_gettext(domain: str, lang: Optional[str]=None): + """ + Configure translation and return its gettext() function. + + If `lang` is set, look for translations for `lang`. Otherwise, try to + determine system's default language and use that. + """ + # https://stackoverflow.com/questions/3425294/how-to-detect-the-os-default-language-in-python + # But I am not going to surrender to Microbugs' nonfree, crappy OS to test + # it, to the lines inside try: may fail. + try: + from ctypes.windll import kernel32 as windll + lang = locale.windows_locale[windll.GetUserDefaultUILanguage()] + except: + lang = locale.getdefaultlocale()[0] or 'C' + + translation = gettext.translation( + domain, + localedir=(here.parent / 'locales'), + languages=[lang, 'en_US'] + ) + + return translation.gettext + +_ = get_gettext('hydrilla_builder') |