Browse Source

Fixed #28015 -- Added makemessages --add-location option.

Thanks François Freitag for review.
Ling-Xiao Yang 8 years ago
parent
commit
04ab96ec4f

+ 22 - 0
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']

+ 14 - 0
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

+ 3 - 0
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
 ~~~~~~~~~~
 

+ 38 - 1
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):