Fix up the babel command:

* Use `add_arguments` (`option_list` is old hat)
* Add tests
This commit is contained in:
Aarni Koskela
2016-09-06 18:49:09 +03:00
parent a46facb352
commit bdc6bd3948
7 changed files with 60 additions and 9 deletions

View File

@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import os
from distutils.dist import Distribution
from optparse import make_option
from subprocess import call
from django.core.management.base import LabelCommand, CommandError
@@ -15,23 +14,23 @@ class Command(LabelCommand):
args = '[makemessages] [compilemessages]'
option_list = LabelCommand.option_list + (
make_option(
'--locale', '-l', default=None, dest='locale', action='append',
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--locale', '-l', default=[], dest='locale', action='append',
help=(
'Creates or updates the message files for the given locale(s)'
' (e.g pt_BR). Can be used multiple times.'
),
),
make_option(
)
parser.add_argument(
'--domain', '-d', default='django', dest='domain',
help='The domain of the message files (default: "django").',
),
make_option(
parser.add_argument(
'--mapping-file', '-F', default=None, dest='mapping_file',
help='Mapping file',
)
)
def handle_label(self, command, **options):
if command not in ('makemessages', 'compilemessages'):

1
tests/babel.cfg Normal file
View File

@@ -0,0 +1 @@
[django: templates/**.*]

View File

View File

@@ -0,0 +1,8 @@
# the header message is required to turn off the fuzzy bit for the catalog
msgid ""
msgstr ""
#: tests/templates/test.txt:2
msgid "This could be translated."
msgstr "Tämän voisi kääntää."

View File

@@ -1,3 +1,5 @@
import os
SECRET_KEY = 'x'
USE_I18N = True
ROOT_URLCONF = 'tests.urls'
@@ -21,3 +23,6 @@ TEMPLATES = [
},
},
]
LOCALE_PATHS = [
os.path.join(os.path.dirname(__file__), 'locale'),
]

View File

@@ -1,4 +1,5 @@
{% load babel %}
{% load i18n babel %}
text={% trans "This could be translated." %}
language_code={{ LANGUAGE_CODE }}
language_name={{ locale.language_name }}
date={{ date|datefmt }}

37
tests/test_command.py Normal file
View File

@@ -0,0 +1,37 @@
import os
from django.core.management import call_command
TEST_LOCALE_DIR = os.path.join(
os.path.dirname(__file__),
'locale',
)
def test_babel_compilemessages():
call_command(
'babel',
'compilemessages',
'-l', 'fi',
)
# Assert that the .mo file was created by attempting to delete it.
os.unlink(
os.path.join(TEST_LOCALE_DIR, 'fi', 'LC_MESSAGES', 'django.mo')
)
def test_babel_makemessages():
call_command(
'babel',
'makemessages',
'-l', 'en',
'-F', os.path.join(os.path.dirname(__file__), 'babel.cfg'),
)
# See that the expected files get populated with the discovered message
for path in [
os.path.join(TEST_LOCALE_DIR, 'django.pot'),
os.path.join(TEST_LOCALE_DIR, 'en', 'LC_MESSAGES', 'django.po'),
]:
with open(path) as infp:
assert '"This could be translated."' in infp.read()
os.unlink(path) # clean up