Skip to content

Commit f9ce542

Browse files
authored
Merge pull request python-babel#34 from python-babel/akx-one-ten
Akx one ten
2 parents 88b3893 + 76fd987 commit f9ce542

File tree

17 files changed

+163
-52
lines changed

17 files changed

+163
-52
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist
55
*.pyc
66
django_babel.egg-info
77
.tox
8+
htmlcov

.travis.yml

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,17 @@ language: python
22
python: 3.4
33
sudo: false
44
env:
5-
- TOX_ENV=py27-django15
6-
- TOX_ENV=py27-django16
7-
- TOX_ENV=py27-django17
5+
- TOX_ENV=docs
6+
- TOX_ENV=lint
87
- TOX_ENV=py27-django18
98
- TOX_ENV=py27-django19
9+
- TOX_ENV=py27-django110
1010
- TOX_ENV=py27-djangomaster
11-
- TOX_ENV=py34-django15
12-
- TOX_ENV=py34-django16
13-
- TOX_ENV=py34-django17
11+
- TOX_ENV=py33-django18
1412
- TOX_ENV=py34-django18
1513
- TOX_ENV=py34-django19
14+
- TOX_ENV=py34-django110
1615
- TOX_ENV=py34-djangomaster
17-
- TOX_ENV=py33-django15
18-
- TOX_ENV=py33-django16
19-
- TOX_ENV=py33-django17
20-
- TOX_ENV=py33-django18
21-
- TOX_ENV=py26-django15
22-
- TOX_ENV=py26-django16
23-
- TOX_ENV=lint
24-
- TOX_ENV=docs
2516
install:
2617
- pip install tox
2718
script:

django_babel/extract.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
# -*- coding: utf-8 -*-
2-
try:
3-
from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
4-
except ImportError:
5-
# Django 1.8 moved most stuff to .base
6-
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
7-
8-
try:
9-
from django.utils.translation import trim_whitespace as trim_django
10-
except ImportError:
11-
trim_django = False
12-
2+
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
3+
from django.utils.translation import trim_whitespace
134
from django.utils.encoding import smart_text
145
from django.utils.translation.trans_real import (
156
inline_re, block_re, endblock_re, plural_re, constant_re)
167

178

18-
def trim_whitespace(string):
19-
"""Trim whitespace.
20-
21-
This is only supported in Django>=1.7. This method help in cases of older
22-
Django versions.
23-
"""
24-
if trim_django:
25-
return trim_django(string)
26-
return string
27-
28-
299
def join_tokens(tokens, trim=False):
3010
message = ''.join(tokens)
3111
if trim:

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'):

django_babel/middleware.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
from babel import Locale, UnknownLocaleError
44
from django.utils.translation import get_language
5+
from threading import local
6+
57
try:
6-
from threading import local
8+
from django.utils.deprecation import MiddlewareMixin
79
except ImportError:
8-
from django.utils._threading_local import local
10+
# Not required for Django <= 1.9, see:
11+
# https://docs.djangoproject.com/en/1.10/topics/http/middleware/#upgrading-pre-django-1-10-style-middleware
12+
MiddlewareMixin = object
913

1014

1115
__all__ = ['get_current_locale', 'LocaleMiddleware']
@@ -22,7 +26,7 @@ def get_current_locale():
2226
return getattr(_thread_locals, 'locale', None)
2327

2428

25-
class LocaleMiddleware(object):
29+
class LocaleMiddleware(MiddlewareMixin):
2630

2731
"""Simple Django middleware that makes available a Babel `Locale` object
2832
via the `request.locale` attribute.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def read(*parts):
2424
url='https://github.com/python-babel/django-babel/',
2525
packages=find_packages(exclude=('tests',)),
2626
install_requires=[
27-
'django>=1.4,<1.10',
27+
'django>=1.4,<1.11',
2828
'babel>=1.3',
2929
],
3030
classifiers=[

tests/babel.cfg

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

tests/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.conf import settings
2+
3+
from testproject import settings as testproject_settings
4+
5+
6+
def pytest_configure():
7+
settings.configure(**vars(testproject_settings))

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+
import pkg_resources
4+
from django.core.management import call_command
5+
6+
TEST_LOCALE_DIR = pkg_resources.resource_filename(
7+
'testproject', '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', pkg_resources.resource_filename(__name__, '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

tests/test_render.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -- encoding: utf-8 --
2+
from __future__ import unicode_literals
3+
4+
import pytest
5+
6+
7+
@pytest.mark.parametrize('locale', ('en', 'fi', 'sv', 'pt-BR'))
8+
def test_babel_render(client, locale):
9+
"""
10+
Test the middleware and the rendery bits.
11+
"""
12+
response = client.get('/', HTTP_ACCEPT_LANGUAGE=locale)
13+
# "Parse" the key-value format
14+
lines = response.content.decode('utf-8').strip().splitlines()
15+
content = dict(kv.split('=', 1) for kv in lines)
16+
# See that we're rendering in the locale we expect
17+
assert content['language_code'] == locale.lower()
18+
# check that we could access `babel.Locale.language_name`
19+
assert content['language_name'] == {
20+
'en': 'English',
21+
'fi': 'suomi',
22+
'sv': 'svenska',
23+
'pt-BR': 'português',
24+
}[locale]
25+
# The rest are not really tested (aside from smoke tests) further;
26+
# the Babel test suite has taken care of that.

0 commit comments

Comments
 (0)