# HG changeset patch
# User Mads Kiilerich <
ma...@kiilerich.com>
# Date 1721086899 -7200
# Tue Jul 16 01:41:39 2024 +0200
# Branch stable
# Node ID 2a04200f98b3c28444a6c5cda3774206776be5da
# Parent 11f1ce257da48fff304e25599428114675f8a06d
i18n: Fix startup failure if LANG has invalid value
An invalid LANG value would make locale.getdefaultlocale raise a ValueError
when i18n.py was imported, causing a confusing failure like:
File "tortoisehg/hgqt/run.py", line 47, in <module>
from ..util.i18n import agettext as _
ImportError: cannot import name 'agettext' from 'tortoisehg.util.i18n'
diff --git a/tortoisehg/util/i18n.py b/tortoisehg/util/i18n.py
--- a/tortoisehg/util/i18n.py
+++ b/tortoisehg/util/i18n.py
@@ -49,7 +49,12 @@ def setlanguage(lang: Optional[str] = No
opts['languages'] = (lang,)
t = gettext.translation('tortoisehg', paths.get_locale_path(),
fallback=True, **opts)
- language = lang if lang else locale.getdefaultlocale(_localeenvs)[0]
+ if not lang:
+ try:
+ lang = locale.getdefaultlocale(_localeenvs)[0]
+ except ValueError: # 'unknown locale: %s'
+ lang = None
+ language = lang
setlanguage()