gettext: handle unsupported languages properly

With "fallback=True" gettext.translation behaves the same as
gettext.install and uses NullTranslations in case the
translation file for given language was not found (as opposed to
throwing an exception).

Fixes:
  # LANG is set to any "unsupported" language, e.g. en_US.UTF-8
  $ chcat --help
  Traceback (most recent call last):
  File "/usr/bin/chcat", line 39, in <module>
    t = gettext.translation(PROGNAME,
  File "/usr/lib64/python3.9/gettext.py", line 592, in translation
    raise FileNotFoundError(ENOENT,
  FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'

Signed-off-by: Vit Mojzis <vmojzis@redhat.com>
Reviewed-by: Daniel Burgener <dburgener@linux.microsoft.com>
Acked-by: Petr Lautrbach <plautrba@redhat.com>
This commit is contained in:
Vit Mojzis 2022-06-24 16:24:25 +02:00 committed by Petr Lautrbach
parent 25e9c91a8b
commit 344463076b
21 changed files with 44 additions and 22 deletions

View file

@ -46,7 +46,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -38,7 +38,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -55,7 +55,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -37,7 +37,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -38,7 +38,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -71,7 +71,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -43,7 +43,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -30,7 +30,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -43,7 +43,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -53,7 +53,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -37,7 +37,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -38,9 +38,10 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except ImportError:
except:
try:
import builtins
builtins.__dict__['_'] = str

View file

@ -38,7 +38,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -42,7 +42,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -19,7 +19,9 @@
try:
import gettext
t = gettext.translation( 'selinux-python' )
t = gettext.translation("selinux-python",
localedir="/usr/share/locale",
fallback=True)
_ = t.gettext
except:
def _(str):

View file

@ -36,7 +36,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -31,7 +31,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -56,7 +56,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -49,7 +49,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -38,7 +38,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try:

View file

@ -45,7 +45,8 @@ try:
kwargs['unicode'] = True
t = gettext.translation(PROGNAME,
localedir="/usr/share/locale",
**kwargs)
**kwargs,
fallback=True)
_ = t.gettext
except:
try: