From f63f3cdf0969c23fd0c05de0f4a2a1df0cd5112e Mon Sep 17 00:00:00 2001 From: manav014 Date: Tue, 27 Oct 2020 17:22:30 +0530 Subject: [PATCH] Fixed #29712 -- Made makemessages warn if locales have hyphens and skip them. --- AUTHORS | 1 + django/core/management/commands/makemessages.py | 8 ++++++++ docs/releases/3.2.txt | 4 ++++ tests/i18n/test_extraction.py | 14 ++++++++++++++ 4 files changed, 27 insertions(+) diff --git a/AUTHORS b/AUTHORS index 5f1ca7cceb..3dd968351b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -564,6 +564,7 @@ answer newbie questions, and generally made Django that much better: Mads Jensen Makoto Tsuyuki Malcolm Tredinnick + Manav Agarwal Manuel Saelices Manuzhai Marc Aymerich Gubern diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 593381c2f4..7aa184b02d 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -383,6 +383,14 @@ class Command(BaseCommand): # Build po files for each selected locale for locale in locales: + if '-' in locale: + self.stdout.write( + 'invalid locale %s, did you mean %s?' % ( + locale, + locale.replace('-', '_'), + ), + ) + continue if self.verbosity > 0: self.stdout.write('processing locale %s' % locale) for potfile in potfiles: diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index 0a12e2168c..83b717893e 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -600,6 +600,10 @@ Miscellaneous * The password reset mechanism now invalidates tokens when the user email is changed. +* :djadmin:`makemessages` command no longer processes invalid locales specified + using :option:`makemessages --locale` option, when they contain hyphens + (``'-'``). + .. _deprecated-features-3.2: Features deprecated in 3.2 diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index a6a0060910..73336816cb 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -152,6 +152,20 @@ class BasicExtractorTests(ExtractorTests): with self.assertRaisesRegex(CommandError, msg): management.call_command('makemessages') + def test_valid_locale(self): + out = StringIO() + management.call_command('makemessages', locale=['de'], stdout=out, verbosity=1) + self.assertNotIn('invalid locale de', out.getvalue()) + self.assertIn('processing locale de', out.getvalue()) + self.assertIs(Path(self.PO_FILE).exists(), True) + + def test_invalid_locale(self): + out = StringIO() + management.call_command('makemessages', locale=['pl-PL'], stdout=out, verbosity=1) + self.assertIn('invalid locale pl-PL, did you mean pl_PL?', out.getvalue()) + self.assertNotIn('processing locale pl-PL', out.getvalue()) + self.assertIs(Path('locale/pl-PL/LC_MESSAGES/django.po').exists(), False) + def test_comments_extractor(self): management.call_command('makemessages', locale=[LOCALE], verbosity=0) self.assertTrue(os.path.exists(self.PO_FILE))