Skip to content

Commit

Permalink
Merge pull request #7615 from kozlovsky/fix/7599_translation_type_error
Browse files Browse the repository at this point in the history
Fixes #7599: TypeError when the translated string does not have the correct number of positional parameters
  • Loading branch information
kozlovsky authored Oct 2, 2023
2 parents 54c3331 + adcc752 commit c7f5d8a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
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

0 comments on commit c7f5d8a

Please sign in to comment.