Skip to content

Add compatibility with Python 3.10+ and Django 3+ #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Changelog
=========

0.6.3 (unreleased)
0.6.4 (2022-11-17)
------------------

- Nothing changed yet.
- compatibility with Django 4.0 added

0.6.3 (2022-01-27)
------------------

- compatibility with Django 3.0 added


0.6.2 (2017-12-18)
Expand Down
51 changes: 21 additions & 30 deletions django_babel/extract.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# -*- coding: utf-8 -*-
from django.template.base import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
from django.template.base import Lexer, TokenType
from django.utils.translation import trim_whitespace
from django.utils.encoding import smart_text
from django.utils.encoding import smart_str
from django.utils.translation.template import inline_re, block_re, endblock_re, plural_re, constant_re

try:
from django.utils.translation.trans_real import (
inline_re, block_re, endblock_re, plural_re, constant_re)
except ImportError:
# Django 1.11+
from django.utils.translation.template import (
inline_re, block_re, endblock_re, plural_re, constant_re)
TOKEN_TEXT = TokenType.TEXT
TOKEN_VAR = TokenType.VAR
TOKEN_BLOCK = TokenType.BLOCK


def join_tokens(tokens, trim=False):
Expand Down Expand Up @@ -48,13 +45,7 @@ def extract_django(fileobj, keywords, comment_tags, options):

encoding = options.get('encoding', 'utf8')
text = fileobj.read().decode(encoding)

try:
text_lexer = Lexer(text)
except TypeError:
# Django 1.9 changed the way we invoke Lexer; older versions
# require two parameters.
text_lexer = Lexer(text, None)
text_lexer = Lexer(text)

for t in text_lexer.tokenize():
lineno += t.contents.count('\n')
Expand All @@ -68,32 +59,32 @@ def extract_django(fileobj, keywords, comment_tags, options):
yield (
lineno,
'npgettext',
[smart_text(message_context),
smart_text(join_tokens(singular, trimmed)),
smart_text(join_tokens(plural, trimmed))],
[smart_str(message_context),
smart_str(join_tokens(singular, trimmed)),
smart_str(join_tokens(plural, trimmed))],
[],
)
else:
yield (
lineno,
'ngettext',
(smart_text(join_tokens(singular, trimmed)),
smart_text(join_tokens(plural, trimmed))),
(smart_str(join_tokens(singular, trimmed)),
smart_str(join_tokens(plural, trimmed))),
[])
else:
if message_context:
yield (
lineno,
'pgettext',
[smart_text(message_context),
smart_text(join_tokens(singular, trimmed))],
[smart_str(message_context),
smart_str(join_tokens(singular, trimmed))],
[],
)
else:
yield (
lineno,
None,
smart_text(join_tokens(singular, trimmed)),
smart_str(join_tokens(singular, trimmed)),
[])

intrans = False
Expand Down Expand Up @@ -131,18 +122,18 @@ def extract_django(fileobj, keywords, comment_tags, options):
yield (
lineno,
'pgettext',
[smart_text(message_context), smart_text(g)],
[smart_str(message_context), smart_str(g)],
[],
)
message_context = None
else:
yield lineno, None, smart_text(g), []
yield lineno, None, smart_str(g), []
elif bmatch:
if bmatch.group(2):
message_context = bmatch.group(2)[1:-1]
for fmatch in constant_re.findall(t.contents):
stripped_fmatch = strip_quotes(fmatch)
yield lineno, None, smart_text(stripped_fmatch), []
yield lineno, None, smart_str(stripped_fmatch), []
intrans = True
inplural = False
trimmed = 'trimmed' in t.split_contents()
Expand All @@ -151,13 +142,13 @@ def extract_django(fileobj, keywords, comment_tags, options):
elif cmatches:
for cmatch in cmatches:
stripped_cmatch = strip_quotes(cmatch)
yield lineno, None, smart_text(stripped_cmatch), []
yield lineno, None, smart_str(stripped_cmatch), []
elif t.token_type == TOKEN_VAR:
parts = t.contents.split('|')
cmatch = constant_re.match(parts[0])
if cmatch:
stripped_cmatch = strip_quotes(cmatch.group(1))
yield lineno, None, smart_text(stripped_cmatch), []
yield lineno, None, smart_str(stripped_cmatch), []
for p in parts[1:]:
if p.find(':_(') >= 0:
p1 = p.split(':', 1)[1]
Expand All @@ -166,4 +157,4 @@ def extract_django(fileobj, keywords, comment_tags, options):
if p1[0] == '(':
p1 = p1.strip('()')
p1 = strip_quotes(p1)
yield lineno, None, smart_text(p1), []
yield lineno, None, smart_str(p1), []
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def read(*parts):
name='django-babel',
description='Utilities for using Babel in Django',
long_description=read('README.rst') + u'\n\n' + read('CHANGELOG.rst'),
version='0.6.3.dev0',
version='0.6.4',
license='BSD',
author='Christopher Grebs',
author_email='[email protected]',
maintainer='Thomas Grainger',
maintainer_email='[email protected]',
url='https://github.com/python-babel/django-babel/',
maintainer='Nikita Grygoriev',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are replacing the maintainer etc. here. This also changes the URL, not sure that is intended?

maintainer_email='[email protected]',
url='https://github.com/nikitagrygoriev/django-babel',
packages=find_packages(exclude=('tests',)),
install_requires=[
'django>=1.8,<3.0',
'django>=1.8',
'babel>=1.3',
],
classifiers=[
Expand Down