Skip to content

Commit

Permalink
Added TOOLBAR_LANGUAGE setting.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
leandrodesouzadev authored and tim-schilling committed Nov 12, 2022
1 parent a0681c9 commit d9b25f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions debug_toolbar/settings.py
Expand Up @@ -40,6 +40,7 @@
"SKIP_TEMPLATE_PREFIXES": ("django/forms/widgets/", "admin/widgets/"),
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
}


Expand Down
5 changes: 4 additions & 1 deletion debug_toolbar/toolbar.py
Expand Up @@ -14,6 +14,7 @@
from django.urls import path, resolve
from django.urls.exceptions import Resolver404
from django.utils.module_loading import import_string
from django.utils.translation import get_language, override as lang_override

from debug_toolbar import APP_NAME, settings as dt_settings

Expand Down Expand Up @@ -76,7 +77,9 @@ def render_toolbar(self):
self.store()
try:
context = {"toolbar": self}
return render_to_string("debug_toolbar/base.html", context)
lang = self.config["TOOLBAR_LANGUAGE"] or get_language()
with lang_override(lang):
return render_to_string("debug_toolbar/base.html", context)
except TemplateSyntaxError:
if not apps.is_installed("django.contrib.staticfiles"):
raise ImproperlyConfigured(
Expand Down
11 changes: 11 additions & 0 deletions docs/configuration.rst
Expand Up @@ -149,7 +149,18 @@ Toolbar options
the request doesn't originate from the toolbar itself, EG that
``is_toolbar_request`` is false for a given request.

* ``TOOLBAR_LANGUAGE``

Default: ``None``

The language used to render the toolbar. If no value is supplied, then the
application's current language will be used. This setting can be used to
render the toolbar in a different language than what the application is
rendered in. For example, if you wish to use English for development,
but want to render your application in French, you would set this to
``"en-us"`` and `settings.LANGUAGE_CODE`_ to ``"fr"``.

.. _settings.LANGUAGE_CODE: https://docs.djangoproject.com/en/stable/ref/settings/#std-setting-LANGUAGE_CODE

Panel options
~~~~~~~~~~~~~
Expand Down
11 changes: 11 additions & 0 deletions tests/test_integration.py
Expand Up @@ -665,3 +665,14 @@ def test_displays_server_error(self):
self.selenium.find_element(By.CLASS_NAME, "BuggyPanel").click()
self.wait.until(EC.visibility_of(debug_window))
self.assertEqual(debug_window.text, \n500: Internal Server Error")

def test_toolbar_language_will_render_to_default_language_when_not_set(self):
self.get("/regular/basic/")
hide_button = self.selenium.find_element(By.ID, "djHideToolBarButton")
assert hide_button.text == "Hide »"

@override_settings(DEBUG_TOOLBAR_CONFIG={"TOOLBAR_LANGUAGE": "pt-br"})
def test_toolbar_language_will_render_to_locale_when_set(self):
self.get("/regular/basic/")
hide_button = self.selenium.find_element(By.ID, "djHideToolBarButton")
assert hide_button.text == "Esconder »"

0 comments on commit d9b25f2

Please sign in to comment.