Skip to content

Commit bdc6bd3

Browse files
committed
Fix up the babel command:
* Use `add_arguments` (`option_list` is old hat) * Add tests
1 parent a46facb commit bdc6bd3

File tree

7 files changed

+60
-9
lines changed

7 files changed

+60
-9
lines changed

django_babel/management/commands/babel.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import os
33
from distutils.dist import Distribution
4-
from optparse import make_option
54
from subprocess import call
65

76
from django.core.management.base import LabelCommand, CommandError
@@ -15,23 +14,23 @@ class Command(LabelCommand):
1514

1615
args = '[makemessages] [compilemessages]'
1716

18-
option_list = LabelCommand.option_list + (
19-
make_option(
20-
'--locale', '-l', default=None, dest='locale', action='append',
17+
def add_arguments(self, parser):
18+
super(Command, self).add_arguments(parser)
19+
parser.add_argument(
20+
'--locale', '-l', default=[], dest='locale', action='append',
2121
help=(
2222
'Creates or updates the message files for the given locale(s)'
2323
' (e.g pt_BR). Can be used multiple times.'
2424
),
25-
),
26-
make_option(
25+
)
26+
parser.add_argument(
2727
'--domain', '-d', default='django', dest='domain',
2828
help='The domain of the message files (default: "django").',
2929
),
30-
make_option(
30+
parser.add_argument(
3131
'--mapping-file', '-F', default=None, dest='mapping_file',
3232
help='Mapping file',
3333
)
34-
)
3534

3635
def handle_label(self, command, **options):
3736
if command not in ('makemessages', 'compilemessages'):

tests/babel.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[django: templates/**.*]

tests/locale/en/LC_MESSAGES/.gitkeep

Whitespace-only changes.

tests/locale/fi/LC_MESSAGES/django.po

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# the header message is required to turn off the fuzzy bit for the catalog
2+
msgid ""
3+
msgstr ""
4+
5+
#: tests/templates/test.txt:2
6+
msgid "This could be translated."
7+
msgstr "Tämän voisi kääntää."
8+

tests/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
SECRET_KEY = 'x'
24
USE_I18N = True
35
ROOT_URLCONF = 'tests.urls'
@@ -21,3 +23,6 @@
2123
},
2224
},
2325
]
26+
LOCALE_PATHS = [
27+
os.path.join(os.path.dirname(__file__), 'locale'),
28+
]

tests/templates/test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
{% load babel %}
1+
{% load i18n babel %}
2+
text={% trans "This could be translated." %}
23
language_code={{ LANGUAGE_CODE }}
34
language_name={{ locale.language_name }}
45
date={{ date|datefmt }}

tests/test_command.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
3+
from django.core.management import call_command
4+
5+
TEST_LOCALE_DIR = os.path.join(
6+
os.path.dirname(__file__),
7+
'locale',
8+
)
9+
10+
11+
def test_babel_compilemessages():
12+
call_command(
13+
'babel',
14+
'compilemessages',
15+
'-l', 'fi',
16+
)
17+
# Assert that the .mo file was created by attempting to delete it.
18+
os.unlink(
19+
os.path.join(TEST_LOCALE_DIR, 'fi', 'LC_MESSAGES', 'django.mo')
20+
)
21+
22+
23+
def test_babel_makemessages():
24+
call_command(
25+
'babel',
26+
'makemessages',
27+
'-l', 'en',
28+
'-F', os.path.join(os.path.dirname(__file__), 'babel.cfg'),
29+
)
30+
# See that the expected files get populated with the discovered message
31+
for path in [
32+
os.path.join(TEST_LOCALE_DIR, 'django.pot'),
33+
os.path.join(TEST_LOCALE_DIR, 'en', 'LC_MESSAGES', 'django.po'),
34+
]:
35+
with open(path) as infp:
36+
assert '"This could be translated."' in infp.read()
37+
os.unlink(path) # clean up

0 commit comments

Comments
 (0)