From c4c52750dbc269e1b73d3db461abfa383271fa7f Mon Sep 17 00:00:00 2001 From: xxyzz Date: Fri, 9 Sep 2022 08:02:07 +0800 Subject: [PATCH] Fallback to two letters .mo file if the plugin doesn't have the file For example, the calibre interface language might be `fr_CA` but the plugin only has `fr.mo`. --- src/calibre/customize/zipplugin.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/calibre/customize/zipplugin.py b/src/calibre/customize/zipplugin.py index 8bb21b2cbd..c5e0824835 100644 --- a/src/calibre/customize/zipplugin.py +++ b/src/calibre/customize/zipplugin.py @@ -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: - _translations_cache[zfp] = None - return + 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))