Skip to content

Commit 34881b5

Browse files
committed
PICARD-1856: Use gettext.pgettext in Python 3.8
If available, use gettext.pgettext. Fall back to custom implementation for older Python versions.
1 parent 344841b commit 34881b5

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

.pylintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ min-similarity-lines=4
283283

284284
# List of additional names supposed to be defined in builtins. Remember that
285285
# you should avoid defining new builtins when possible.
286-
additional-builtins=_, N_, ngettext, gettext_countries, gettext_attributes
286+
additional-builtins=_, N_, ngettext, gettext_countries,
287+
gettext_attributes, pgettext_attributes
287288

288289
# Tells whether unused global variables should be treated as a violation.
289290
allow-global-unused-variables=yes

picard/coverart/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424

2525
from picard.const import MB_ATTRIBUTES
26-
from picard.i18n import gettext_attr
2726

2827

2928
# list of types from http://musicbrainz.org/doc/Cover_Art/Types
@@ -46,4 +45,4 @@ def translate_caa_type(name):
4645
return _(CAA_TYPES_TR[name])
4746
else:
4847
title = CAA_TYPES_TR.get(name, name)
49-
return gettext_attr(title, "cover_art_type")
48+
return pgettext_attributes("cover_art_type", title)

picard/i18n.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,22 @@ def setup_gettext(localedir, ui_language=None, logger=None):
9898
builtins.__dict__['gettext_countries'] = trans_countries.gettext
9999
builtins.__dict__['gettext_attributes'] = trans_attributes.gettext
100100

101+
if hasattr(trans_attributes, 'pgettext'):
102+
builtins.__dict__['pgettext_attributes'] = trans_attributes.pgettext
103+
else:
104+
def pgettext(context, message):
105+
return gettext_ctxt(trans_attributes.gettext, message, context)
106+
builtins.__dict__['pgettext_attributes'] = pgettext
107+
101108
logger("_ = %r", _)
102109
logger("N_ = %r", N_)
103110
logger("ngettext = %r", ngettext)
104111
logger("gettext_countries = %r", gettext_countries)
105112
logger("gettext_attributes = %r", gettext_attributes)
113+
logger("pgettext_attributes = %r", pgettext_attributes)
106114

107115

108-
# Workaround for po files with msgctxt which isn't supported by current python
116+
# Workaround for po files with msgctxt which isn't supported by Python < 3.8
109117
# gettext
110118
# msgctxt are used within attributes.po, and gettext is failing to translate
111119
# strings due to that
@@ -123,8 +131,3 @@ def gettext_ctxt(gettext_, message, context=None):
123131
# no translation found, return original message
124132
return message
125133
return translated
126-
127-
128-
def gettext_attr(message, context=None):
129-
"""Translate MB attributes, depending on context"""
130-
return gettext_ctxt(gettext_attributes, message, context)

picard/ui/options/releases.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
RELEASE_SECONDARY_GROUPS,
4242
)
4343
from picard.const.sys import IS_WIN
44-
from picard.i18n import gettext_attr
4544

4645
from picard.ui.options import (
4746
OptionsPage,
@@ -171,7 +170,7 @@ def __init__(self, parent=None):
171170
self._release_type_sliders = {}
172171

173172
def add_slider(name, griditer, context):
174-
label = gettext_attr(name, context)
173+
label = pgettext_attributes(context, name)
175174
self._release_type_sliders[name] = \
176175
ReleaseTypeScore(self.ui.type_group,
177176
self.ui.gridLayout,
@@ -184,7 +183,7 @@ def add_slider(name, griditer, context):
184183
for name in RELEASE_PRIMARY_GROUPS:
185184
add_slider(name, griditer, context='release_group_primary_type')
186185
for name in sorted(RELEASE_SECONDARY_GROUPS,
187-
key=lambda v: gettext_attr(v, 'release_group_secondary_type')):
186+
key=lambda v: pgettext_attributes('release_group_secondary_type', v)):
188187
add_slider(name, griditer, context='release_group_secondary_type')
189188

190189
self.reset_preferred_types_btn = QtWidgets.QPushButton(self.ui.type_group)
@@ -264,7 +263,7 @@ def _load_list_items(self, setting, source, list1, list2):
264263
source_list = [(c[0], gettext_countries(c[1])) for c in
265264
source.items()]
266265
elif setting == "preferred_release_formats":
267-
source_list = [(c[0], gettext_attr(c[1], "medium_format")) for c
266+
source_list = [(c[0], pgettext_attributes("medium_format", c[1])) for c
268267
in source.items()]
269268
else:
270269
source_list = [(c[0], _(c[1])) for c in source.items()]

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# E501: line too long (xx > 79 characters)
88
# W503: line break occurred before a binary operator
99
ignore = E127,E128,E129,E226,E241,E501,W503
10-
builtins = _,N_,ngettext,gettext_attributes,gettext_countries,string_
10+
builtins = _,N_,ngettext,gettext_attributes,pgettext_attributes,gettext_countries,string_
1111
exclude = ui_*.py,picard/resources.py
1212

1313
[coverage:run]

0 commit comments

Comments
 (0)