Fixed #18714 -- Added 'fuzzy' compilemessages option

This commit is contained in:
Anton Baklanov 2014-11-16 00:56:37 +02:00 committed by Claude Paroz
parent d188101319
commit d63703f1cd
6 changed files with 75 additions and 5 deletions

View File

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

View File

@ -21,7 +21,7 @@ script found at the top level of each Django project directory.
.BI cleanup .BI cleanup
Cleans out old data from the database (only expired sessions at the moment). Cleans out old data from the database (only expired sessions at the moment).
.TP .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. Compiles .po files to .mo files for use with builtin gettext support.
.TP .TP
.BI "createcachetable [" "tablename" "]" .BI "createcachetable [" "tablename" "]"

View File

@ -165,18 +165,23 @@ the builtin gettext support. See :doc:`/topics/i18n/index`.
Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
specify the locale(s) to process. If not provided, all locales are processed. 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 Use the :djadminopt:`--exclude` option (or its shorter version ``-x``) to
specify the locale(s) to exclude from processing. If not provided, no locales specify the locale(s) to exclude from processing. If not provided, no locales
are excluded. 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:: Example usage::
django-admin compilemessages --locale=pt_BR 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
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
django-admin compilemessages --exclude=pt_BR --exclude=fr django-admin compilemessages --exclude=pt_BR --exclude=fr
django-admin compilemessages -x pt_BR django-admin compilemessages -x pt_BR

View File

@ -312,6 +312,9 @@ Management Commands
:djadminopt:`--exclude` which allows exclusion of specific locales from :djadminopt:`--exclude` which allows exclusion of specific locales from
processing. 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` * The :djadminopt:`--ignorenonexistent` option of the :djadmin:`loaddata`
management command now ignores data for models that no longer exist. management command now ignores data for models that no longer exist.

View File

@ -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 "Водка"

View File

@ -1,14 +1,19 @@
# -*- coding: utf-8 -*-
import os import os
import shutil import shutil
import stat import stat
import sys import sys
import unittest import unittest
import gettext as gettext_module
from django.core.management import call_command, CommandError, execute_from_command_line from django.core.management import call_command, CommandError, execute_from_command_line
from django.core.management.utils import find_command from django.core.management.utils import find_command
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.test import override_settings from django.test import override_settings
from django.utils import translation 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._os import upath
from django.utils.six import StringIO from django.utils.six import StringIO
@ -188,3 +193,28 @@ class CompilationErrorHandling(MessageCompilationTests):
def test_error_reported_by_msgfmt(self): def test_error_reported_by_msgfmt(self):
with self.assertRaises(CommandError): with self.assertRaises(CommandError):
call_command('compilemessages', locale=[self.LOCALE], stdout=StringIO()) 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('Водка'))