From f333b208a403ffff4636da35757f2a2b7fbe5115 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Fri, 8 Dec 2017 22:35:45 +0100 Subject: [PATCH] localization: differentiate between C and not set Linux env. variables In case of e.g. LANG=hu_HU.UTF-8 and LC_MESSAGES=C, the expected behavior is that the UI is English, not Hungarian. The problem was that we did not differentiate between a not set LC_MESSAGES and one that is set to C, as locale.getdefaultlocale() returns None in both cases. --- src/calibre/utils/localization.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/calibre/utils/localization.py b/src/calibre/utils/localization.py index a601a72824..dd9d869cf7 100644 --- a/src/calibre/utils/localization.py +++ b/src/calibre/utils/localization.py @@ -45,8 +45,16 @@ def get_system_locale(): traceback.print_exc() if lang is None: try: - lang = locale.getdefaultlocale(['LANGUAGE', 'LC_ALL', 'LC_CTYPE', - 'LC_MESSAGES', 'LANG'])[0] + envvars = ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'LANG'] + lang = locale.getdefaultlocale(envvars)[0] + + # lang is None in two cases: either the environment variable is not + # set or it's "C". Stop looking for a language in the later case. + if lang is None: + for var in envvars: + if var in os.environ.keys() and os.environ[var] == 'C': + lang = 'en_US' + break except: pass # This happens on Ubuntu apparently if lang is None and 'LANG' in os.environ: # Needed for OS X