-
Notifications
You must be signed in to change notification settings - Fork 444
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
Fix KeyError for translations #7370
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nice update of the tr
function!
It is also necessary to convert .ts files to .qm files by executing the update_translations.sh
script and commit the updated .qm files.
@kozlovsky it gives me the following error: ➜ ./update_translations.sh
./update_translations.sh: line 3: lrelease: command not found ref: |
bd35516
to
4315a09
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested Tribler with ES_ES locale, and it still crashes on
<message>
<location filename="../utilities.py" line="166"/>
<source>%(seconds)is</source>
<translation>%(segundos)is</translation>
</message>
Sentry issue: TMP-5P |
It looks like intercepting return tr("%(seconds)is") % data Here, the The If we want to prevent this type of |
For example, we can use the following class TranslatedString(str):
def __new__(cls, translation, original_string):
return super().__new__(cls, translation)
def __init__(self, translation: str, original_string: str): # pylint: disable=unused-argument
super().__init__()
self.original_string = original_string
def __mod__(self, other):
try:
return str.__mod__(self, other)
except KeyError as e:
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
def tr(key):
translated_string = QCoreApplication.translate('@default', key)
return TranslatedString(translated_string, original_string=key) Tests: def test_correct_translation():
original_string = 'original %(key1)s'
translated_string = 'translated %(key1)s'
s = TranslatedString(translated_string, original_string)
assert s % {'key1': '123'} == 'translated 123'
@patch('tribler.gui.utilities.logger.warning')
def test_missed_key_in_translated_string(warning: Mock):
original_string = 'original %(key1)s'
translated_string = 'translated %(key2)s'
s = TranslatedString(translated_string, original_string)
assert s % {'key1': '123'} == 'original 123'
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_missed_key_in_both_translated_and_original_strings(warning: Mock):
original_string = 'original %(key1)s'
translated_string = 'translated %(key2)s'
s = TranslatedString(translated_string, original_string)
with pytest.raises(KeyError, match=r"^'key1'$"):
s % {'key3': '123'}
warning.assert_called_once_with('KeyError: No value provided for \'key2\' in translation "translated %(key2)s", '
'original string: "original %(key1)s"') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of the .ts file still needs to be fixed (with recompilation of the .qm file):
<message>
<location filename="../utilities.py" line="166"/>
<source>%(seconds)is</source>
<translation>%(segundos)is</translation>
</message>
This PR fixes #7363 by fixing the root cause of the issue — incorrect translation files.
Also, it modifies the
tr
function in a way that it is safe from this type of error: