Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Language Selection on the Toolbar #1703

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved
}


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 »"