From d63703f1cd373f2a1d52e1947835b44d9565c1e0 Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Sun, 16 Nov 2014 00:56:37 +0200 Subject: [PATCH] Fixed #18714 -- Added 'fuzzy' compilemessages option --- .../management/commands/compilemessages.py | 4 +++ docs/man/django-admin.1 | 2 +- docs/ref/django-admin.txt | 13 +++++--- docs/releases/1.8.txt | 3 ++ .../commands/locale/ru/LC_MESSAGES/django.po | 28 +++++++++++++++++ tests/i18n/test_compilation.py | 30 +++++++++++++++++++ 6 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 tests/i18n/commands/locale/ru/LC_MESSAGES/django.po diff --git a/django/core/management/commands/compilemessages.py b/django/core/management/commands/compilemessages.py index 71a52d1d6f..26ceb19261 100644 --- a/django/core/management/commands/compilemessages.py +++ b/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 " diff --git a/docs/man/django-admin.1 b/docs/man/django-admin.1 index f0cd3d44bb..243a15edf5 100644 --- a/docs/man/django-admin.1 +++ b/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" "]" diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 27d2733943..66c721574b 100644 --- a/docs/ref/django-admin.txt +++ b/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 diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 73c0e7a040..bf6e9d3516 100644 --- a/docs/releases/1.8.txt +++ b/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. diff --git a/tests/i18n/commands/locale/ru/LC_MESSAGES/django.po b/tests/i18n/commands/locale/ru/LC_MESSAGES/django.po new file mode 100644 index 0000000000..ae23e448a3 --- /dev/null +++ b/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 , 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 \n" +"Language-Team: LANGUAGE \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 "Водка" diff --git a/tests/i18n/test_compilation.py b/tests/i18n/test_compilation.py index 49ea2d0dc1..be46e168aa 100644 --- a/tests/i18n/test_compilation.py +++ b/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('Водка'))