summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Scott <michael.scott250@gmail.com>2016-09-12 21:39:34 +0100
committerTim Graham <timograham@gmail.com>2016-09-14 20:14:29 -0400
commit416004c043d2c5dbb2cc851ecb977a5628c3aea7 (patch)
tree30c30d8bf44874516350d3ec548aeb97ef93135c
parentca825b1779097168545b79c96317da3e1780282c (diff)
[1.10.x] Fixed #15143 -- Doc'd how to set the language for the test client.
Backport of 3c447b108ac70757001171f7a4791f493880bf5b from master
-rw-r--r--docs/topics/i18n/translation.txt2
-rw-r--r--docs/topics/testing/tools.txt38
2 files changed, 40 insertions, 0 deletions
diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt
index 97555508f5..5d1c7f2ad4 100644
--- a/docs/topics/i18n/translation.txt
+++ b/docs/topics/i18n/translation.txt
@@ -1950,6 +1950,8 @@ Here's example HTML template code:
In this example, Django looks up the URL of the page to which the user will be
redirected in the ``redirect_to`` context variable.
+.. _explicitly-setting-the-active-language:
+
Explicitly setting the active language
--------------------------------------
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index 6e14813d42..b60114d126 100644
--- a/docs/topics/testing/tools.txt
+++ b/docs/topics/testing/tools.txt
@@ -574,6 +574,44 @@ can access these properties as part of a test condition.
session['somekey'] = 'test'
session.save()
+Setting the language
+--------------------
+
+When testing applications that support internationalization and localization,
+you might want to set the language for a test client request. The method for
+doing so depends on whether or not the
+:class:`~django.middleware.locale.LocaleMiddleware` is enabled.
+
+If the middleware is enabled, the language can be set by creating a cookie with
+a name of :setting:`LANGUAGE_COOKIE_NAME` and a value of the language code::
+
+ from django.conf import settings
+
+ def test_language_using_cookie(self):
+ self.client.cookies.load({settings.LANGUAGE_COOKIE_NAME: 'fr'})
+ response = self.client.get('/')
+ self.assertEqual(response.content, b"Bienvenue sur mon site.")
+
+or by including the ``Accept-Language`` HTTP header in the request::
+
+ def test_language_using_header(self):
+ response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='fr')
+ self.assertEqual(response.content, b"Bienvenue sur mon site.")
+
+More details are in :ref:`how-django-discovers-language-preference`.
+
+If the middleware isn't enabled, the active language may be set using
+:func:`.translation.override`::
+
+ from django.utils import translation
+
+ def test_language_using_override(self):
+ with translation.override('fr'):
+ response = self.client.get('/')
+ self.assertEqual(response.content, b"Bienvenue sur mon site.")
+
+More details are in :ref:`explicitly-setting-the-active-language`.
+
Example
-------