Permalink
Browse files

Add translation context manager, bump version

  • Loading branch information...
1 parent ce6d804 commit 7864d45f7ff77a5106c7f6e0277bfeef3e3ecf67 @bradleyayers committed Jan 10, 2013
View
@@ -261,8 +261,8 @@ Context managers
django-attest has some context managers to simplify common tasks:
-settings
-^^^^^^^^
+settings(**settings)
+^^^^^^^^^^^^^^^^^^^^
Change global settings within a block, same functionality as Django 1.4's
``TestCase.settings``::
@@ -282,8 +282,21 @@ settings remaining constant.
``django.test.signals.setting_changed``.
-urlconf
-^^^^^^^
+translation(language_code, deactivate=False)
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Activate a specific translation/language. The semantics are the same as Django
+1.4's ``django.utils.translation.override``::
+
+ from django_attest import translation
+ from django.utils.translation import ugettext
+
+ with translation('de'):
+ assert ugettext('the apple') == 'der Apfel'
+
+
+urlconf(patterns)
+^^^^^^^^^^^^^^^^^
Takes a list of URL patterns and promotes them up as the root URLconf. This
avoids the need to have a dedicated *test project* and ``urls.py`` for simple
@@ -317,6 +330,7 @@ Changelog
v0.10.0
-------
+- Add ``translation`` context manager
- Add Travis CI testing
v0.9.1
View
@@ -5,7 +5,8 @@
from .signals import setting_changed
-__all__ = ("settings", "TransactionTestContext", "TestContext", "urlconf")
+__all__ = ("settings", "TransactionTestContext", "TestContext", "translation",
+ "urlconf")
class TransactionTestContext(TransactionTestCase):
@@ -68,6 +69,33 @@ def settings(**kwargs):
setting_changed.send(sender=sender, setting=key, value=value)
+@contextmanager
+def translation(language_code, deactivate=False):
+ """
+ Port of django.utils.translation.override from Django 1.4
+
+ @param language_code: a language code or ``None``. If ``None``, translation
+ is disabled and raw translation strings are used
+ @param deactivate: If ``True``, when leaving the manager revert to the
+ default behaviour (i.e. ``settings.LANGUAGE_CODE``)
+ rather than the translation that was active prior to
+ entering.
+ """
+ from django.utils import translation
+ original = translation.get_language()
+ if language_code is not None:
+ translation.activate(language_code)
+ else:
+ translation.deactivate_all()
+ try:
+ yield
+ finally:
+ if deactivate:
+ translation.deactivate()
+ else:
+ translation.activate(original)
+
+
@contextmanager
def urlconf(patterns):
"""
View
@@ -4,7 +4,7 @@
setup(
name='django-attest',
- version='0.9.1',
+ version='0.10.0',
description='Provides Django specific testing helpers to Attest',
author='Bradley Ayers',
Binary file not shown.
@@ -0,0 +1,16 @@
+# This file is distributed under the same license as the django-tables2 package
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: django-attest\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2013-01-10 09:29+1000\n"
+"PO-Revision-Date: 2013-01-10 09:29+1000\n"
+"Last-Translator: Bradley Ayers <bradley.ayers@gmail.com>\n"
+"Language: de\n"
+"MIME-Version: 1.0\n"
+"Content-Type: text/plain; charset=UTF-8\n"
+"Content-Transfer-Encoding: 8bit\n"
+
+msgid "the apple"
+msgstr "der Apfel"
View
@@ -12,8 +12,10 @@
from django.http import HttpResponse
from django.test.client import Client
from django.test.simple import build_test
+from django.utils.translation import ugettext
import django_attest
-from django_attest import queries, redirects, TestContext, signals, urlconf
+from django_attest import (
+ queries, redirects, TestContext, translation, signals, urlconf)
from pkg_resources import parse_version
from tests.app.models import Thing
@@ -178,3 +180,12 @@ def view(request):
urls = patterns('', (r'view/', view))
with urlconf(urls):
assert client.get(reverse(view)).content == b'success'
+
+
+@suite.test
+def translation_works():
+ assert ugettext('the apple') == 'the apple'
+ with translation('de'):
+ assert ugettext('the apple') == 'der Apfel'
+ with translation('en'):
+ assert ugettext('the apple') == 'the apple'
View
@@ -29,3 +29,8 @@
TEMPLATE_DIRS = (
join(ROOT, "templates"),
)
+
+LANGUAGES = (
+ ('de', 'German'),
+ ('en', 'English'),
+)
View
@@ -10,13 +10,13 @@ setenv =
testing =
coverage
six
- git+https://github.com/dag/attest/#egg=Attest
+ https://github.com/dag/attest/tarball/master
[django]
1.2.x = Django>=1.2,<1.3
1.3.x = Django>=1.3,<1.4
1.4.x = Django>=1.4,<1.5
-latest = git+https://github.com/django/django/#egg=Django
+latest = https://github.com/django/django/tarball/master
; -- python 3.2, Attest [latest], Django --------------------------------------

0 comments on commit 7864d45

Please sign in to comment.