Skip to content

Commit

Permalink
Fix KeyError for translations
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Apr 17, 2023
1 parent f43d795 commit f41f8c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/tribler/gui/i18n/es_ES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,27 +1123,27 @@ Si no está seguro, pulse "No". Más adelante podrá eliminar esas car
<message>
<location filename="../utilities.py" line="157"/>
<source>%(years)iy %(weeks)iw</source>
<translation>%(años)iy %(semanas)iw</translation>
<translation>%(years)ia %(weeks)is</translation>
</message>
<message>
<location filename="../utilities.py" line="159"/>
<source>%(weeks)iw %(days)id</source>
<translation>%(semanas)iw %(días)id</translation>
<translation>%(weeks)is %(days)id</translation>
</message>
<message>
<location filename="../utilities.py" line="161"/>
<source>%(days)id %(hours)ih</source>
<translation>%(días)id %(horas)ih</translation>
<translation>%(days)id %(hours)ih</translation>
</message>
<message>
<location filename="../utilities.py" line="163"/>
<source>%(hours)ih %(minutes)im</source>
<translation>%(horas)ih %(minutos)im</translation>
<translation>%(hours)ih %(minutes)im</translation>
</message>
<message>
<location filename="../utilities.py" line="165"/>
<source>%(minutes)im %(seconds)is</source>
<translation>%(minutos)im %(segundos)is</translation>
<translation>%(minutes)im %(seconds)is</translation>
</message>
<message>
<location filename="../utilities.py" line="166"/>
Expand Down
31 changes: 28 additions & 3 deletions src/tribler/gui/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from unittest.mock import MagicMock
from unittest.mock import MagicMock, Mock, patch
from urllib.parse import unquote_plus

import pytest

from tribler.gui.utilities import compose_magnetlink, create_api_key, dict_item_is_any_of, format_api_key, \
quote_plus_unicode, set_api_key, unicode_quoter
from tribler.gui.utilities import compose_magnetlink, create_api_key, dict_item_is_any_of, duration_to_string, \
format_api_key, \
quote_plus_unicode, set_api_key, tr, unicode_quoter


def test_quoter_char():
Expand Down Expand Up @@ -154,3 +155,27 @@ def test_set_api_key():
gui_settings = MagicMock()
set_api_key(gui_settings, "abcdef")
gui_settings.setValue.assert_called_once_with("api_key", b"abcdef")


TRANSLATIONS = [
(0, '0s'),
(61, '1m 1s'),
(3800, '1h 3m'),
(110000, '1d 6h'),
(1110000, '1w 5d'),
(91110000, '2y 46w'),
(11191110000, 'Forever'),
]


@pytest.mark.parametrize('seconds, translation', TRANSLATIONS)
@patch('tribler.gui.utilities.tr', new=Mock(side_effect=lambda x: x))
def test_duration_to_string(seconds, translation):
# test if the duration_to_string function returns the correct translation for all possible formats
assert duration_to_string(seconds) == translation


@patch('tribler.gui.utilities.QCoreApplication.translate', new=Mock(side_effect=KeyError('key is invalid')))
def test_tr_exception():
# test if the tr function returns the key if the translation is not found
assert tr('0s') == '0s'
6 changes: 5 additions & 1 deletion src/tribler/gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@


def tr(key):
return f"{QCoreApplication.translate('@default', key)}"
try:
return str(QCoreApplication.translate('@default', key))
except KeyError as e:
logger.warning(f'{type(e).__name__}: {e} in "{key}"')
return key


VOTES_RATING_DESCRIPTIONS = (
Expand Down

0 comments on commit f41f8c5

Please sign in to comment.