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.
This commit is contained in:
Kovid Goyal 2023-10-16 12:54:14 +05:30
parent 324a1e1aed
commit 691c4c3f7a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 30 additions and 11 deletions

View File

@ -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

View File

@ -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