From 691c4c3f7af9119cc643d5f9f375501340cf4de2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 16 Oct 2023 12:54:14 +0530 Subject: [PATCH] Those pythonic nitwits have deprecated getdefaultlocale and not provided a replacement. Blithering idiots. Fortunately we only really use it on Linux which has no system API for getting the user's preferred language instead relying on the usual smorgasboard of under specified, arcane, mutually incompatible env vars. So just copy the env var parsing code from the deprecated function and make do. Sigh. --- src/calibre/startup.py | 20 ++++++++++++-------- src/calibre/utils/localization.py | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/calibre/startup.py b/src/calibre/startup.py index c89d95d4b4..32ade5037c 100644 --- a/src/calibre/startup.py +++ b/src/calibre/startup.py @@ -6,10 +6,13 @@ __docformat__ = 'restructuredtext en' Perform various initialization tasks. ''' -import locale, sys, os +import locale +import os +import sys # Default translation is NOOP from polyglot.builtins import builtins + builtins.__dict__['_'] = lambda s: s # For strings which belong in the translation tables, but which shouldn't be @@ -19,7 +22,7 @@ builtins.__dict__['__'] = lambda s: s # For backwards compat with some third party plugins builtins.__dict__['dynamic_property'] = lambda func: func(None) -from calibre.constants import iswindows, ismacos, islinux, DEBUG, isfreebsd +from calibre.constants import DEBUG, isfreebsd, islinux, ismacos, iswindows def get_debug_executable(headless=False): @@ -127,7 +130,7 @@ def initialize_calibre(): # # Setup translations - from calibre.utils.localization import set_translators + from calibre.utils.localization import getlangcode_from_envvars, set_translators set_translators() @@ -141,16 +144,16 @@ def initialize_calibre(): string try: locale.setlocale(locale.LC_ALL, '') # set the locale to the user's default locale - except: - dl = locale.getdefaultlocale() + except Exception: try: + dl = getlangcode_from_envvars() if dl: - locale.setlocale(locale.LC_ALL, dl[0]) - except: + locale.setlocale(locale.LC_ALL, dl) + except Exception: pass builtins.__dict__['lopen'] = open # legacy compatibility - from calibre.utils.icu import title_case, lower as icu_lower, upper as icu_upper + from calibre.utils.icu import lower as icu_lower, title_case, upper as icu_upper builtins.__dict__['icu_lower'] = icu_lower builtins.__dict__['icu_upper'] = icu_upper builtins.__dict__['icu_title'] = title_case @@ -161,6 +164,7 @@ def initialize_calibre(): # Name all threads at the OS level created using the threading module, see # http://bugs.python.org/issue15500 import threading + from calibre_extensions import speedup orig_start = threading.Thread.start diff --git a/src/calibre/utils/localization.py b/src/calibre/utils/localization.py index e6912de4bc..e62b457fa5 100644 --- a/src/calibre/utils/localization.py +++ b/src/calibre/utils/localization.py @@ -28,6 +28,22 @@ def available_translations(): return _available_translations +default_envvars_for_langcode = ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'LANG') + + +def getlangcode_from_envvars(envvars=default_envvars_for_langcode): + lookup = os.environ.get + for k in envvars: + localename = lookup(k) + if localename: + if k == 'LANGUAGE': + localename = localename.split(':')[0] + break + else: + localename = 'C' + return locale._parse_localename(localename)[0] + + def get_system_locale(): from calibre.constants import ismacos, iswindows lang = None @@ -50,13 +66,12 @@ def get_system_locale(): traceback.print_exc() if lang is None: try: - envvars = ['LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LC_MESSAGES', 'LANG'] - lang = locale.getdefaultlocale(envvars)[0] + lang = getlangcode_from_envvars() # lang is None in two cases: either the environment variable is not # set or it's "C". Stop looking for a language in the latter case. if lang is None: - for var in envvars: + for var in default_envvars_for_langcode: if os.environ.get(var) == 'C': lang = 'en_US' break