From d9b25f2d47eb38e3680fc9535c4a7305a7bb8f1b Mon Sep 17 00:00:00 2001 From: Leandro de Souza Date: Wed, 9 Nov 2022 16:54:35 -0300 Subject: [PATCH] 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 --- debug_toolbar/settings.py | 1 + debug_toolbar/toolbar.py | 5 ++++- docs/configuration.rst | 11 +++++++++++ tests/test_integration.py | 11 +++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 2bad251c1..5ea904ad1 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -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, } diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py index 419c67418..21d66bfe0 100644 --- a/debug_toolbar/toolbar.py +++ b/debug_toolbar/toolbar.py @@ -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 @@ -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( diff --git a/docs/configuration.rst b/docs/configuration.rst index 07e0a845c..2e20c454c 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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 ~~~~~~~~~~~~~ diff --git a/tests/test_integration.py b/tests/test_integration.py index 9f4065604..f41113938 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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 »"