Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
pip-log.txt
.DS_Store
*.egg-info
.tox/
16 changes: 7 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: python
python: 2.6
env:
- TOX_ENV=py26-1.4
- TOX_ENV=py26-1.5
- TOX_ENV=py26-1.6
- TOX_ENV=py27-1.4
- TOX_ENV=py27-1.5
- TOX_ENV=py27-1.6
matrix:
include:
- python: 2.7
env: TOXENV=py27-1.5,py27-1.6,py27-1.7,py27-1.8
- python: 3.6
env: TOXENV=py36-1.8
install:
- pip install tox
script:
- tox -e $TOX_ENV
- tox
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and the Babel library.

* Author: Wil Clouser and contributors_
* Licence: BSD
* Compatibility: Python 2.6 and 2.7, Django 1.4, 1.5 and 1.6
* Compatibility: Python 2.7 and 3.6, Django 1.4, 1.5 and 1.6, 1.8, 1.8
* Requirements: django, babel, jinja2, jingo and translate-toolkit
* Project URL: https://github.com/clouserw/tower
* Documentation: http://tower.readthedocs.org/en/latest/
Expand Down
9 changes: 9 additions & 0 deletions examples/tower-project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ def JINJA_CONFIG():
}

SECRET_KEY = 'useless not secret key used for tests'

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ babel==1.3
jinja2
translate-toolkit
-e git://github.com/jbalogh/jingo.git#egg=jingo
future

# Requirements for running the tests
django-nose==1.1
Expand Down
6 changes: 6 additions & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@
from django.core.management import call_command

# Run the equivalent of "django-admin.py test"
try:
from django import setup
setup()
except ImportError:
# Django 1.6 and below does not require setup()
pass
call_command('test')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Topic :: Software Development :: Libraries :: Python Modules',
]
)
25 changes: 18 additions & 7 deletions tower/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from past.builtins import basestring
from builtins import object
from importlib import import_module
import copy
import gettext
import re

import django
from django.conf import settings
from django.utils import six
from django.utils.functional import lazy
from django.utils.importlib import import_module
from django.utils.translation import (trans_real as django_trans,
ugettext as django_ugettext,
ungettext as django_nugettext)
Expand Down Expand Up @@ -52,8 +55,9 @@ def ungettext(singular, plural, number, context=None):
return plural_stripped
return ret

ugettext_lazy = lazy(ugettext, unicode)
ungettext_lazy = lazy(ungettext, unicode)

ugettext_lazy = lazy(ugettext, six.text_type)
ungettext_lazy = lazy(ungettext, six.text_type)


def add_context(context, message):
Expand Down Expand Up @@ -87,7 +91,11 @@ class Translation(object):
ungettext = staticmethod(ungettext)

import jingo
jingo.env.install_gettext_translations(Translation)
try:
jingo.env.install_gettext_translations(Translation)
except Exception:
# jingo 0.8 requires get_env() instead.
jingo.get_env().install_gettext_translations(Translation)


def activate(locale):
Expand Down Expand Up @@ -116,6 +124,7 @@ def _activate(locale):

# Django caches the translation objects here
t = django_trans._translations.get(locale, None)

if t is not None:
return t

Expand All @@ -127,7 +136,9 @@ def _activate(locale):
# our foreign catalog into en-US. Since Django stuck the en-US catalog
# into its cache for this locale, we have to update that too.
t = copy.deepcopy(django_trans.translation(locale))
t.set_language(locale)
if hasattr(t, 'set_language'):
# not required for django 1.8+
t.set_language(locale)
try:
# When trying to load css, js, and images through the Django server
# gettext() throws an exception saying it can't find the .mo files. I
Expand All @@ -144,8 +155,8 @@ def _activate(locale):
# If you've got extra .mo files to load, this is the place.
path = import_module(settings_module).path
domain = getattr(settings, 'TEXT_DOMAIN', 'messages')
bonus = gettext.translation(domain, path('locale'), [locale],
django_trans.DjangoTranslation)
bonus = gettext.translation(domain=domain, localedir=path('locale'), languages=[locale],
codeset='utf-8')
t.merge(bonus)

# Overwrite t (defaults to en-US) with our real locale's plural form
Expand Down
15 changes: 8 additions & 7 deletions tower/management/commands/amalgamate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import os
import sys
from subprocess import Popen
Expand Down Expand Up @@ -58,10 +59,10 @@ def handle(self, *args, **options):
'messages.po')

if not os.path.isfile(r_messages):
print " Can't find (%s). Skipping..." % (r_messages)
print(" Can't find (%s). Skipping..." % (r_messages))
continue

print "Mushing python strings into messages.po for %s" % (locale)
print("Mushing python strings into messages.po for %s" % (locale))

# Step 3: Merge our new combined .pot with the .po file
if locale == "en_US":
Expand Down Expand Up @@ -90,7 +91,7 @@ def handle(self, *args, **options):
# commands in the middle of Step 3.
for domain in standalone_domains:

print "Merging %s strings to each locale..." % domain
print("Merging %s strings to each locale..." % domain)
z_domain_keys = os.path.join(locale_dir, 'z-%s.pot' % domain)
if not os.path.isfile(z_domain_keys):
sys.exit("Can't find z-%s.pot" % domain)
Expand All @@ -104,11 +105,11 @@ def handle(self, *args, **options):
'z-%s.po' % domain)

if not os.path.isfile(z_domain_messages):
print " Can't find (%s). Creating..." % (z_domain_messages)
print(" Can't find (%s). Creating..." % (z_domain_messages))
t = open(z_domain_messages, 'w')
t.close()

print "Merging z-%s.po for %s" % (domain, locale)
print("Merging z-%s.po for %s" % (domain, locale))

z_domain_keys_file = open(z_domain_keys)

Expand All @@ -133,6 +134,6 @@ def handle(self, *args, **options):

p4.communicate()
mergeme.close()
print "Domain %s finished" % domain
print("Domain %s finished" % domain)

print "All finished"
print("All finished")
15 changes: 9 additions & 6 deletions tower/management/commands/extract.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function
from io import StringIO

import os
import tempfile
from optparse import make_option
Expand Down Expand Up @@ -72,9 +75,9 @@ def create_pofile_from_babel(extracted):
if settings.TOWER_ADD_HEADERS:
catalog = po.pofile()
else:
catalog = po.pofile(inputfile="")
catalog = po.pofile(inputfile=StringIO(u""))
except AttributeError:
catalog = po.pofile(inputfile="")
catalog = po.pofile(inputfile=StringIO(u""))

for extracted_unit in extracted:
# Babel 1.3 has an additional value: context.
Expand Down Expand Up @@ -121,19 +124,19 @@ def handle(self, *args, **options):
os.makedirs(outputdir)

if domains == "all":
domains = settings.DOMAIN_METHODS.keys()
domains = list(settings.DOMAIN_METHODS.keys())
else:
domains = [domains]

root = settings.ROOT

def callback(filename, method, options):
if method != 'ignore':
print " %s" % filename
print(" %s" % filename)

for domain in domains:

print "Extracting all strings in domain %s..." % (domain)
print("Extracting all strings in domain %s..." % (domain))

methods = settings.DOMAIN_METHODS[domain]
extracted = extract_from_dir(root,
Expand Down Expand Up @@ -179,4 +182,4 @@ def callback(filename, method, options):
for i in [x for x in domains if x not in standalone_domains]:
os.remove(os.path.join(outputdir, '%s.pot' % i))

print 'done'
print('done')
13 changes: 7 additions & 6 deletions tower/management/commands/merge.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import print_function
import os
import sys
from optparse import make_option
Expand Down Expand Up @@ -50,7 +51,7 @@ def handle(self, *args, **options):

for domain in domains:

print "Merging %s strings to each locale..." % domain
print("Merging %s strings to each locale..." % domain)
domain_pot = os.path.join(locale_dir, 'templates', 'LC_MESSAGES',
'%s.pot' % domain)
if not os.path.isfile(domain_pot):
Expand All @@ -69,7 +70,7 @@ def handle(self, *args, **options):
'%s.po' % domain)

if not os.path.isfile(domain_po):
print " Can't find (%s). Creating..." % (domain_po)
print(" Can't find (%s). Creating..." % (domain_po))
if not call(["which", "msginit"], stdout=PIPE) == 0:
raise Exception("You do not have gettext installed.")
p1 = Popen(["msginit",
Expand All @@ -80,7 +81,7 @@ def handle(self, *args, **options):
"--width=200",])
p1.communicate()

print "Merging %s.po for %s" % (domain, locale)
print("Merging %s.po for %s" % (domain, locale))

domain_pot_file = open(domain_pot)

Expand All @@ -100,13 +101,13 @@ def handle(self, *args, **options):
domain_po,
"-"]
if os.path.isfile(compendium):
print "(using a compendium)"
print("(using a compendium)")
command.insert(1, '--compendium=%s' % compendium)
p3 = Popen(command, stdin=mergeme)
p3.communicate()
mergeme.close()
print "Domain %s finished" % domain
print("Domain %s finished" % domain)

print "All finished"
print("All finished")

Command.help = Command.__doc__
Loading