diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index c31b7ec46d..63e52da6d9 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -259,6 +259,17 @@ class Command(BaseCommand): '--no-location', action='store_true', dest='no_location', help="Don't write '#: filename:line' lines.", ) + parser.add_argument( + '--add-location', dest='add_location', + choices=('full', 'file', 'never'), const='full', nargs='?', + help=( + "Controls '#: filename:line' lines. If the option is 'full' " + "(the default if not given), the lines include both file name " + "and line number. If it's 'file', the line number is omitted. If " + "it's 'never', the lines are suppressed (same as --no-location). " + "--add-location requires gettext 0.19 or newer." + ), + ) parser.add_argument( '--no-obsolete', action='store_true', dest='no_obsolete', help="Remove obsolete message strings.", @@ -293,6 +304,17 @@ class Command(BaseCommand): self.msguniq_options = self.msguniq_options[:] + ['--no-location'] self.msgattrib_options = self.msgattrib_options[:] + ['--no-location'] self.xgettext_options = self.xgettext_options[:] + ['--no-location'] + if options['add_location']: + if self.gettext_version < (0, 19): + raise CommandError( + "The --add-location option requires gettext 0.19 or later. " + "You have %s." % '.'.join(str(x) for x in self.gettext_version) + ) + arg_add_location = "--add-location=%s" % options['add_location'] + self.msgmerge_options = self.msgmerge_options[:] + [arg_add_location] + self.msguniq_options = self.msguniq_options[:] + [arg_add_location] + self.msgattrib_options = self.msgattrib_options[:] + [arg_add_location] + self.xgettext_options = self.xgettext_options[:] + [arg_add_location] self.no_obsolete = options['no_obsolete'] self.keep_pot = options['keep_pot'] diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 45fd50b587..9ffb624879 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -647,6 +647,20 @@ Suppresses writing '``#: filename:line``’ comment lines in language files. Using this option makes it harder for technically skilled translators to understand each message's context. +.. django-admin-option:: --add-location [{full,file,never}] + +.. versionadded:: 2.0 + +Controls ``#: filename:line`` comment lines in language files. If the option +is: + +* ``full`` (the default if not given): the lines include both file name and + line number. +* ``file``: the line number is omitted. +* ``never``: the lines are suppressed (same as :option:`--no-location`). + +Requires ``gettext`` 0.19 or newer. + .. django-admin-option:: --keep-pot Prevents deleting the temporary ``.pot`` files generated before creating the diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index e891cb4614..0411e8fb2d 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -182,6 +182,9 @@ Management Commands * :djadmin:`inspectdb` now translates MySQL's unsigned integer columns to ``PositiveIntegerField`` or ``PositiveSmallIntegerField``. +* The new :option:`makemessages --add-location` option controls the comment + format in PO files. + Migrations ~~~~~~~~~~ diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index e6945f1014..fc1c68a090 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -4,7 +4,7 @@ import shutil import time import warnings from io import StringIO -from unittest import mock, skipUnless +from unittest import mock, skipIf, skipUnless from admin_scripts.tests import AdminScriptTestCase @@ -23,6 +23,8 @@ from .utils import POFileAssertionMixin, RunInTmpDirMixin, copytree LOCALE = 'de' has_xgettext = find_command('xgettext') +gettext_version = MakeMessagesCommand().gettext_version if has_xgettext else None +requires_gettext_019 = skipIf(gettext_version < (0, 19), 'gettext 0.19 required') @skipUnless(has_xgettext, 'xgettext is mandatory for extraction tests') @@ -581,6 +583,41 @@ class LocationCommentsTests(ExtractorTests): self.assertLocationCommentNotPresent(self.PO_FILE, None, '.html.py') self.assertLocationCommentPresent(self.PO_FILE, 5, 'templates', 'test.html') + @requires_gettext_019 + def test_add_location_full(self): + """makemessages --add-location=full""" + management.call_command('makemessages', locale=[LOCALE], verbosity=0, add_location='full') + self.assertTrue(os.path.exists(self.PO_FILE)) + # Comment with source file relative path and line number is present. + self.assertLocationCommentPresent(self.PO_FILE, 'Translatable literal #6b', 'templates', 'test.html') + + @requires_gettext_019 + def test_add_location_file(self): + """makemessages --add-location=file""" + management.call_command('makemessages', locale=[LOCALE], verbosity=0, add_location='file') + self.assertTrue(os.path.exists(self.PO_FILE)) + # Comment with source file relative path is present. + self.assertLocationCommentPresent(self.PO_FILE, None, 'templates', 'test.html') + # But it should not contain the line number. + self.assertLocationCommentNotPresent(self.PO_FILE, 'Translatable literal #6b', 'templates', 'test.html') + + @requires_gettext_019 + def test_add_location_never(self): + """makemessages --add-location=never""" + management.call_command('makemessages', locale=[LOCALE], verbosity=0, add_location='never') + self.assertTrue(os.path.exists(self.PO_FILE)) + self.assertLocationCommentNotPresent(self.PO_FILE, None, 'test.html') + + @mock.patch('django.core.management.commands.makemessages.Command.gettext_version', new=(0, 18, 99)) + def test_add_location_gettext_version_check(self): + """ + CommandError is raised when using makemessages --add-location with + gettext < 0.19. + """ + msg = "The --add-location option requires gettext 0.19 or later. You have 0.18.99." + with self.assertRaisesMessage(CommandError, msg): + management.call_command('makemessages', locale=[LOCALE], verbosity=0, add_location='full') + class KeepPotFileExtractorTests(ExtractorTests):