From d113e40aa6f79fa4c215575884c34010e2faa3f4 Mon Sep 17 00:00:00 2001 From: Christopher Grebs Date: Sat, 22 Feb 2014 14:52:15 +0100 Subject: [PATCH] Some more unicode/py3k compat --- django_babel/extract.py | 25 +++++++++++++++++-------- django_babel/templatetags/babel.py | 2 +- setup.py | 4 ++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/django_babel/extract.py b/django_babel/extract.py index d2dda19..62b7a50 100644 --- a/django_babel/extract.py +++ b/django_babel/extract.py @@ -2,6 +2,7 @@ from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK from django.utils.translation.trans_real import ( inline_re, block_re, endblock_re, plural_re, constant_re) +from django.utils.encoding import smart_unicode def extract_django(fileobj, keywords, comment_tags, options): @@ -34,10 +35,18 @@ def extract_django(fileobj, keywords, comment_tags, options): pluralmatch = plural_re.match(t.contents) if endbmatch: if inplural: - yield lineno, 'ngettext', (unicode(''.join(singular)), - unicode(''.join(plural))), [] + yield ( + lineno, + 'ngettext', + (smart_unicode(u''.join(singular)), + smart_unicode(u''.join(plural))), + [] else: - yield lineno, None, unicode(''.join(singular)), [] + yield ( + lineno, + None, + smart_unicode(u''.join(singular)), + []) intrans = False inplural = False singular = [] @@ -68,22 +77,22 @@ def extract_django(fileobj, keywords, comment_tags, options): g = g.strip('"') elif g[0] == "'": g = g.strip("'") - yield lineno, None, unicode(g), [] + yield lineno, None, smart_unicode(g), [] elif bmatch: for fmatch in constant_re.findall(t.contents): - yield lineno, None, unicode(fmatch), [] + yield lineno, None, smart_unicode(fmatch), [] intrans = True inplural = False singular = [] plural = [] elif cmatches: for cmatch in cmatches: - yield lineno, None, unicode(cmatch), [] + yield lineno, None, smart_unicode(cmatch), [] elif t.token_type == TOKEN_VAR: parts = t.contents.split('|') cmatch = constant_re.match(parts[0]) if cmatch: - yield lineno, None, unicode(cmatch.group(1)), [] + yield lineno, None, smart_unicode(cmatch.group(1)), [] for p in parts[1:]: if p.find(':_(') >= 0: p1 = p.split(':', 1)[1] @@ -95,4 +104,4 @@ def extract_django(fileobj, keywords, comment_tags, options): p1 = p1.strip("'") elif p1[0] == '"': p1 = p1.strip('"') - yield lineno, None, unicode(p1), [] + yield lineno, None, smart_unicode(p1), [] diff --git a/django_babel/templatetags/babel.py b/django_babel/templatetags/babel.py index 5246f35..ca99406 100644 --- a/django_babel/templatetags/babel.py +++ b/django_babel/templatetags/babel.py @@ -8,7 +8,7 @@ try: except ImportError: timezone = None -from ..middleware import get_current_locale +from django_babel.middleware import get_current_locale babel = __import__('babel', {}, {}, ['core', 'support']) Format = babel.support.Format diff --git a/setup.py b/setup.py index 95d75e2..ef633af 100755 --- a/setup.py +++ b/setup.py @@ -5,11 +5,11 @@ from setuptools import setup, find_packages from setuptools.command.test import test as TestCommand -with open('README.md') as fobj: +with open('README.md', 'rb') as fobj: readme = fobj.read() -with open('CHANGES.md') as fobj: +with open('CHANGES.md', 'rb') as fobj: history = fobj.read()