Merge branch 'translation_fallback' of https://github.com/xxyzz/calibre

This commit is contained in:
Kovid Goyal 2022-09-09 06:44:02 +05:30
commit ea873e7c1c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -119,13 +119,15 @@ def load_translations(namespace, zfp):
_translations_cache[zfp] = None
return
with zipfile.ZipFile(zfp) as zf:
try:
mo = zf.read('translations/%s.mo' % lang)
except KeyError:
mo = None # No translations for this language present
if mo is None:
mo_path = zipfile.Path(zf, f"translations/{lang}.mo")
if not mo_path.exists() and "_" in lang:
mo_path = zipfile.Path(zf, f"translations/{lang.split('_')[0]}.mo")
if mo_path.exists():
mo = mo_path.read_bytes()
else:
_translations_cache[zfp] = None
return
from gettext import GNUTranslations
from io import BytesIO
trans = _translations_cache[zfp] = GNUTranslations(BytesIO(mo))