From 3c52fc2470b277118172a693ee989c26b6e4ace0 Mon Sep 17 00:00:00 2001 From: Philipp Wolfer Date: Tue, 27 Aug 2024 18:48:10 +0200 Subject: [PATCH] PICARD-2958: Handle gettext being called with empty string Calling gettext("") by default returns the header of the PO file for the current locale. This is unexpected. Return an empty string instead. --- picard/i18n.py | 4 ++++ test/test_i18n.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/picard/i18n.py b/picard/i18n.py index eb5e4e3871..fe4d26810d 100644 --- a/picard/i18n.py +++ b/picard/i18n.py @@ -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) diff --git a/test/test_i18n.py b/test/test_i18n.py index 98d6c51819..ac06d46e5b 100644 --- a/test/test_i18n.py +++ b/test/test_i18n.py @@ -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'))