Skip to content

Commit d9b25f2

Browse files
leandrodesouzadevtim-schilling
authored andcommitted
Added TOOLBAR_LANGUAGE setting.
Allows users to define the language that the toolbar will be rendered using the new settings `TOOLBAR_LANGUAGE` Adds test cases to TOOLBAR_LANGUAGE setting Removes trailing setting on example project
1 parent a0681c9 commit d9b25f2

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

debug_toolbar/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"SKIP_TEMPLATE_PREFIXES": ("django/forms/widgets/", "admin/widgets/"),
4141
"SQL_WARNING_THRESHOLD": 500, # milliseconds
4242
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
43+
"TOOLBAR_LANGUAGE": None,
4344
}
4445

4546

debug_toolbar/toolbar.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from django.urls import path, resolve
1515
from django.urls.exceptions import Resolver404
1616
from django.utils.module_loading import import_string
17+
from django.utils.translation import get_language, override as lang_override
1718

1819
from debug_toolbar import APP_NAME, settings as dt_settings
1920

@@ -76,7 +77,9 @@ def render_toolbar(self):
7677
self.store()
7778
try:
7879
context = {"toolbar": self}
79-
return render_to_string("debug_toolbar/base.html", context)
80+
lang = self.config["TOOLBAR_LANGUAGE"] or get_language()
81+
with lang_override(lang):
82+
return render_to_string("debug_toolbar/base.html", context)
8083
except TemplateSyntaxError:
8184
if not apps.is_installed("django.contrib.staticfiles"):
8285
raise ImproperlyConfigured(

docs/configuration.rst

+11
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,18 @@ Toolbar options
149149
the request doesn't originate from the toolbar itself, EG that
150150
``is_toolbar_request`` is false for a given request.
151151

152+
* ``TOOLBAR_LANGUAGE``
152153

154+
Default: ``None``
155+
156+
The language used to render the toolbar. If no value is supplied, then the
157+
application's current language will be used. This setting can be used to
158+
render the toolbar in a different language than what the application is
159+
rendered in. For example, if you wish to use English for development,
160+
but want to render your application in French, you would set this to
161+
``"en-us"`` and `settings.LANGUAGE_CODE`_ to ``"fr"``.
162+
163+
.. _settings.LANGUAGE_CODE: https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-LANGUAGE_CODE
153164

154165
Panel options
155166
~~~~~~~~~~~~~

tests/test_integration.py

+11
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,14 @@ def test_displays_server_error(self):
665665
self.selenium.find_element(By.CLASS_NAME, "BuggyPanel").click()
666666
self.wait.until(EC.visibility_of(debug_window))
667667
self.assertEqual(debug_window.text, \n500: Internal Server Error")
668+
669+
def test_toolbar_language_will_render_to_default_language_when_not_set(self):
670+
self.get("/regular/basic/")
671+
hide_button = self.selenium.find_element(By.ID, "djHideToolBarButton")
672+
assert hide_button.text == "Hide »"
673+
674+
@override_settings(DEBUG_TOOLBAR_CONFIG={"TOOLBAR_LANGUAGE": "pt-br"})
675+
def test_toolbar_language_will_render_to_locale_when_set(self):
676+
self.get("/regular/basic/")
677+
hide_button = self.selenium.find_element(By.ID, "djHideToolBarButton")
678+
assert hide_button.text == "Esconder »"

0 commit comments

Comments
 (0)