From 930376c0362cab2008e12445f3eba8894d3348d7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 31 Mar 2019 20:13:22 +0530 Subject: [PATCH] py3: Port calls to ugettext() --- manual/conf.py | 3 ++- src/calibre/customize/zipplugin.py | 3 ++- src/calibre/linux.py | 6 +++--- src/calibre/srv/http_response.py | 6 +++--- src/calibre/srv/tests/http.py | 3 ++- src/calibre/utils/localization.py | 5 +++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/manual/conf.py b/manual/conf.py index 0649b64c63..9e61d4b34c 100644 --- a/manual/conf.py +++ b/manual/conf.py @@ -18,6 +18,7 @@ from datetime import date base = os.path.dirname(os.path.abspath(__file__)) sys.path.append(base) sys.path.insert(0, os.path.dirname(base)) +ispy3 = sys.version_info.major > 2 from setup import __appname__, __version__ import calibre.utils.localization as l # Ensure calibre translations are installed import custom @@ -100,7 +101,7 @@ if language not in {'en', 'eng'}: except IOError: pass else: - title = t.ugettext(title) + title = getattr(t, 'gettext' if ispy3 else 'ugettext')(title) # If true, '()' will be appended to :func: etc. cross-reference text. # add_function_parentheses = True diff --git a/src/calibre/customize/zipplugin.py b/src/calibre/customize/zipplugin.py index a079d53744..91a567c43c 100644 --- a/src/calibre/customize/zipplugin.py +++ b/src/calibre/customize/zipplugin.py @@ -12,6 +12,7 @@ from collections import OrderedDict from functools import partial from calibre import as_unicode +from calibre.constants import ispy3 from calibre.customize import (Plugin, numeric_version, platform, InvalidPlugin, PluginNotFound) from polyglot.builtins import (itervalues, map, string_or_bytes, @@ -111,7 +112,7 @@ def load_translations(namespace, zfp): from io import BytesIO trans = _translations_cache[zfp] = GNUTranslations(BytesIO(mo)) - namespace['_'] = trans.ugettext + namespace['_'] = getattr(trans, 'gettext' if ispy3 else 'ugettext') namespace['ngettext'] = trans.ungettext diff --git a/src/calibre/linux.py b/src/calibre/linux.py index 8700c0ec08..e0862335f2 100644 --- a/src/calibre/linux.py +++ b/src/calibre/linux.py @@ -9,7 +9,7 @@ from subprocess import check_call, check_output from functools import partial from calibre import __appname__, prints, guess_type -from calibre.constants import islinux, isbsd +from calibre.constants import islinux, isbsd, ispy3 from calibre.customize.ui import all_input_formats from calibre.ptempfile import TemporaryDirectory from calibre import CurrentDir @@ -1103,7 +1103,7 @@ def write_appdata(key, entry, base, translators): for para in entry['description']: description.append(E.p(para)) for lang, t in iteritems(translators): - tp = t.ugettext(para) + tp = getattr(t, 'gettext' if ispy3 else 'ugettext')(para) if tp != para: description.append(E.p(tp)) description[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang) @@ -1120,7 +1120,7 @@ def write_appdata(key, entry, base, translators): type='desktop' ) for lang, t in iteritems(translators): - tp = t.ugettext(entry['summary']) + tp = getattr(t, 'gettext' if ispy3 else 'ugettext')(entry['summary']) if tp != entry['summary']: root.append(E.summary(tp)) root[-1].set('{http://www.w3.org/XML/1998/namespace}lang', lang) diff --git a/src/calibre/srv/http_response.py b/src/calibre/srv/http_response.py index c1b5547bf5..a4fcdef924 100644 --- a/src/calibre/srv/http_response.py +++ b/src/calibre/srv/http_response.py @@ -16,7 +16,7 @@ from functools import wraps from polyglot.builtins import iteritems, itervalues, reraise, map, is_py3 from calibre import guess_type, force_unicode -from calibre.constants import __version__, plugins +from calibre.constants import __version__, plugins, ispy3 from calibre.srv.loop import WRITE from calibre.srv.errors import HTTPSimpleResponse from calibre.srv.http_request import HTTPRequest, read_headers @@ -288,8 +288,8 @@ class RequestData(object): # {{{ if lang_code != self.lang_code: found, lang, t = self.get_translator(lang_code) self.lang_code = lang - self.gettext_func = t.ugettext - self.ngettext_func = t.ungettext + self.gettext_func = getattr(t, 'gettext' if ispy3 else 'ugettext') + self.ngettext_func = getattr(t, 'ngettext' if ispy3 else 'ungettext') # }}} diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 05b487b2a3..255d516d32 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -11,6 +11,7 @@ from io import BytesIO from tempfile import NamedTemporaryFile from calibre import guess_type +from calibre.constants import ispy3 from calibre.srv.tests.base import BaseTest, TestServer from calibre.srv.utils import eintr_retry_call from calibre.utils.monotonic import monotonic @@ -96,7 +97,7 @@ class TestHTTP(BaseTest): conn.request('GET', '/', headers={'Accept-Language': al}) r = conn.getresponse() self.ae(r.status, http_client.OK) - q += get_translator(q)[-1].ugettext('Unknown') + q += getattr(get_translator(q)[-1], 'gettext' if ispy3 else 'ugettext')('Unknown') self.ae(r.read(), q) test('en', 'en') diff --git a/src/calibre/utils/localization.py b/src/calibre/utils/localization.py index b9eb0a15ac..8f17007c3e 100644 --- a/src/calibre/utils/localization.py +++ b/src/calibre/utils/localization.py @@ -6,7 +6,7 @@ __license__ = 'GPL v3' __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' -import os, locale, re, io +import os, locale, re, io, sys from gettext import GNUTranslations, NullTranslations from polyglot.builtins import is_py3, iteritems, unicode_type @@ -380,7 +380,8 @@ def get_language(lang): # The translator was not active when _extra_lang_codes was defined, so # re-translate return translate(_extra_lang_codes[lang]) - return get_iso_language(getattr(_lang_trans, 'ugettext', translate), lang) + attr = 'gettext' if sys.version_info.major > 2 else 'ugettext' + return get_iso_language(getattr(_lang_trans, attr, translate), lang) def calibre_langcode_to_name(lc, localize=True):