Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #7599: TypeError when the translated string does not have the correct number of positional parameters #7615

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/tribler/gui/i18n/ru_RU.qm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/tribler/gui/i18n/ru_RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@
<message>
<location filename="../widgets/downloadspage.py" line="463"/>
<source>An error occurred when exporting the torrent file: %s</source>
<translation>Ошибка при экспорте торрент-файла:</translation>
<translation>Ошибка при экспорте торрент-файла: %s</translation>
</message>
<message>
<location filename="../widgets/downloadspage.py" line="469"/>
Expand Down
34 changes: 33 additions & 1 deletion src/tribler/gui/tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def test_missed_key_in_translated_string(warning: Mock):
# In this test, we pass the correct param 'key1' presented in the original string but missed in the translation.
# The KeyError is intercepted, the original string is used instead of the translation, and the error is logged
# as a warning.
assert s % {'key1': '123'} == 'original 123'
result = s % {'key1': '123'}
assert result == 'original 123'

warning.assert_called_once_with('KeyError: No value provided for \'key2\' in translation "translated %(key2)s", '
'original string: "original %(key1)s"')
Expand All @@ -215,3 +216,34 @@ def test_missed_key_in_both_translated_and_original_strings(warning: Mock):

warning.assert_called_once_with('KeyError: No value provided for \'key2\' in translation "translated %(key2)s", '
'original string: "original %(key1)s"')


@patch('tribler.gui.utilities.logger.warning')
def test_wrong_parameters_in_translated_string(warning: Mock):
original_string = 'original: %s'
translated_string = 'translated'
s = TranslatedString(translated_string, original_string)

# In this test, we pass the correct positional param value '123' for a positional parameter that presents in the
# original string but is missed in the translation. The TypeError is intercepted; the original string is used
# instead of the translation, and the error is logged as a warning.
result = s % ('123',)
assert result == 'original: 123'

warning.assert_called_once_with('TypeError: Wrong number of parameters in translation "translated", '
'original string: "original: %s"')


@patch('tribler.gui.utilities.logger.warning')
def test_wrong_parameters_in_original_string(warning: Mock):
original_string = 'original'
translated_string = 'translated'
s = TranslatedString(translated_string, original_string)

# If neither translated nor original string have the matched number of positional parameters,
# then the TypeError exception is propagated.
with pytest.raises(TypeError, match='^not all arguments converted during string formatting$'):
_ = s % ('123',)

warning.assert_called_once_with('TypeError: Wrong number of parameters in translation "translated", '
'original string: "original"')
4 changes: 4 additions & 0 deletions src/tribler/gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def __mod__(self, other):
msg = f'No value provided for {e} in translation "{self}", original string: "{self.original_string}"'
logger.warning(f'{type(e).__name__}: {msg}')
return self.original_string % other
except TypeError as e:
msg = f'Wrong number of parameters in translation "{self}", original string: "{self.original_string}"'
logger.warning(f'{type(e).__name__}: {msg}')
return self.original_string % other


def tr(key):
Expand Down