Skip to content

Commit

Permalink
PICARD-2958: Handle gettext being called with empty string
Browse files Browse the repository at this point in the history
Calling gettext("") by default returns the header of the PO file for the
current locale. This is unexpected. Return an empty string instead.
  • Loading branch information
phw committed Aug 27, 2024
1 parent b0cf8ae commit 3c52fc2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions picard/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def setup_gettext(localedir, ui_language=None, logger=None):

def gettext(message: str) -> str:
"""Translate the messsage using the current translator."""
# Calling gettext("") by default returns the header of the PO file for the
# current locale. This is unexpected. Return an empty string instead.
if message == "":
return message
return _translation['main'].gettext(message)


Expand Down
4 changes: 4 additions & 0 deletions test/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def test_existing_locales(self):
# self.assertEqual('Französisch', gettext_constants('French'))
self.assertEqual('Frankreich', gettext_countries('France'))

def test_gettext_handles_empty_string(self):
setup_gettext(localedir, 'fr')
self.assertEqual('', _(''))

def test_sort_key(self):
setup_gettext(localedir, 'de')
self.assertTrue(sort_key('äb') < sort_key('ac'))
Expand Down

0 comments on commit 3c52fc2

Please sign in to comment.