Browse Source

Fixed #18714 -- Added 'fuzzy' compilemessages option

Anton Baklanov 10 years ago
parent
commit
d63703f1cd

+ 4 - 0
django/core/management/commands/compilemessages.py

@@ -43,11 +43,15 @@ class Command(BaseCommand):
             'Can be used multiple times.')
         parser.add_argument('--exclude', '-x', dest='exclude', action='append', default=[],
             help='Locales to exclude. Default is none. Can be used multiple times.')
+        parser.add_argument('--use-fuzzy', '-f', dest='fuzzy', action='store_true', default=False,
+            help='Use fuzzy translations.')
 
     def handle(self, **options):
         locale = options.get('locale')
         exclude = options.get('exclude')
         self.verbosity = int(options.get('verbosity'))
+        if options.get('fuzzy'):
+            self.program_options = self.program_options + ['-f']
 
         if find_command(self.program) is None:
             raise CommandError("Can't find %s. Make sure you have GNU gettext "

+ 1 - 1
docs/man/django-admin.1

@@ -21,7 +21,7 @@ script found at the top level of each Django project directory.
 .BI cleanup
 Cleans out old data from the database (only expired sessions at the moment).
 .TP
-.BI "compilemessages [" "\-\-locale=LOCALE" "] [" "\-\-exclude=LOCALE" "]"
+.BI "compilemessages [" "\-\-locale=LOCALE" "] [" "\-\-exclude=LOCALE" "] [" "\-\-use\-fuzzy" "]"
 Compiles .po files to .mo files for use with builtin gettext support.
 .TP
 .BI "createcachetable [" "tablename" "]"

+ 9 - 4
docs/ref/django-admin.txt

@@ -165,18 +165,23 @@ the builtin gettext support. See :doc:`/topics/i18n/index`.
 Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
 specify the locale(s) to process. If not provided, all locales are processed.
 
-.. versionadded:: 1.8
-
 Use the :djadminopt:`--exclude` option (or its shorter version ``-x``) to
 specify the locale(s) to exclude from processing. If not provided, no locales
 are excluded.
 
+You can pass ``--use-fuzzy`` option (or ``-f``) to include fuzzy translations
+into compiled files.
+
+.. versionchanged:: 1.8
+
+    Added ``--exclude`` and ``--use-fuzzy`` options.
+
 Example usage::
 
     django-admin compilemessages --locale=pt_BR
-    django-admin compilemessages --locale=pt_BR --locale=fr
+    django-admin compilemessages --locale=pt_BR --locale=fr -f
     django-admin compilemessages -l pt_BR
-    django-admin compilemessages -l pt_BR -l fr
+    django-admin compilemessages -l pt_BR -l fr --use-fuzzy
     django-admin compilemessages --exclude=pt_BR
     django-admin compilemessages --exclude=pt_BR --exclude=fr
     django-admin compilemessages -x pt_BR

+ 3 - 0
docs/releases/1.8.txt

@@ -312,6 +312,9 @@ Management Commands
   :djadminopt:`--exclude` which allows exclusion of specific locales from
   processing.
 
+* :djadmin:`compilemessages` now has a ``--use-fuzzy`` or ``-f`` option which
+  includes fuzzy translations into compiled files.
+
 * The :djadminopt:`--ignorenonexistent` option of the :djadmin:`loaddata`
   management command now ignores data for models that no longer exist.
 

+ 28 - 0
tests/i18n/commands/locale/ru/LC_MESSAGES/django.po

@@ -0,0 +1,28 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr ""
+"Project-Id-Version: PACKAGE VERSION\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2013-03-30 12:51+0000\n"
+"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+"Language-Team: LANGUAGE <LL@li.org>\n"
+"Language: \n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
+"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
+
+#
+msgid "Lenin"
+msgstr "Ленин"
+
+#, fuzzy
+msgid "Vodka"
+msgstr "Водка"

+ 30 - 0
tests/i18n/test_compilation.py

@@ -1,14 +1,19 @@
+# -*- coding: utf-8 -*-
+
 import os
 import shutil
 import stat
 import sys
 import unittest
+import gettext as gettext_module
 
 from django.core.management import call_command, CommandError, execute_from_command_line
 from django.core.management.utils import find_command
 from django.test import SimpleTestCase
 from django.test import override_settings
 from django.utils import translation
+from django.utils.translation import ugettext
+from django.utils.encoding import force_text
 from django.utils._os import upath
 from django.utils.six import StringIO
 
@@ -188,3 +193,28 @@ class CompilationErrorHandling(MessageCompilationTests):
     def test_error_reported_by_msgfmt(self):
         with self.assertRaises(CommandError):
             call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
+
+
+class FuzzyTranslationTest(MessageCompilationTests):
+
+    LOCALE = 'ru'
+    MO_FILE = 'locale/%s/LC_MESSAGES/django.mo' % LOCALE
+
+    def setUp(self):
+        super(FuzzyTranslationTest, self).setUp()
+        self.addCleanup(self.rmfile, os.path.join(self.test_dir, self.MO_FILE))
+        gettext_module._translations = {}  # flush cache or test will be useless
+
+    def test_nofuzzy_compiling(self):
+        with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
+            call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO())
+            with translation.override(self.LOCALE):
+                self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
+                self.assertEqual(ugettext('Vodka'), force_text('Vodka'))
+
+    def test_fuzzy_compiling(self):
+        with override_settings(LOCALE_PATHS=(os.path.join(self.test_dir, 'locale'),)):
+            call_command('compilemessages', locale=[self.LOCALE], fuzzy=True, stdout=StringIO())
+            with translation.override(self.LOCALE):
+                self.assertEqual(ugettext('Lenin'), force_text('Ленин'))
+                self.assertEqual(ugettext('Vodka'), force_text('Водка'))