From cef2ecf00445fcff6ea40ddb910ffa3ddd3c5bbf Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 23 Apr 2021 16:32:32 -0700 Subject: [PATCH] i18n: Pass language name with every response. With a comment added by tabbott on live-update considerations. See https://chat.zulip.org/#narrow/stream/9-issues/topic/FormatJS.20error.20in.20standalone.20pages/near/1168413 for context. Signed-off-by: Anders Kaseorg --- static/js/i18n.js | 2 +- static/js/server_events_dispatch.js | 5 +++++ zerver/context_processors.py | 13 +++++++++++++ zerver/tests/test_home.py | 1 + zproject/jinja2/__init__.py | 10 +++++----- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/static/js/i18n.js b/static/js/i18n.js index 541cc42fde..f3d8d91439 100644 --- a/static/js/i18n.js +++ b/static/js/i18n.js @@ -9,7 +9,7 @@ import {page_params} from "./page_params"; const cache = createIntlCache(); export const intl = createIntl( { - locale: page_params.default_language, + locale: page_params.request_language, defaultLocale: "en", messages: page_params.translation_data, onError: /* istanbul ignore next */ (error) => { diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index 0dd696a49f..8f44865b76 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -573,6 +573,11 @@ export function dispatch_normal_event(event) { } if (event.setting_name === "default_language") { // We additionally need to set the language name. + // + // Note that this does not change translations at all; + // a reload is fundamentally required because we + // cannot rerender with the new language the strings + // present in the backend/Jinja2 templates. page_params.default_language_name = event.language_name; } if (event.setting_name === "twenty_four_hour_time") { diff --git a/zerver/context_processors.py b/zerver/context_processors.py index a1bff5b3d0..3229f63d23 100644 --- a/zerver/context_processors.py +++ b/zerver/context_processors.py @@ -2,9 +2,11 @@ from typing import Any, Dict, Optional from urllib.parse import urljoin from django.conf import settings +from django.contrib.staticfiles.storage import staticfiles_storage from django.http import HttpRequest from django.utils.html import escape from django.utils.safestring import SafeString +from django.utils.translation import get_language from version import ( LATEST_MAJOR_VERSION, @@ -28,6 +30,11 @@ from zproject.backends import ( require_email_format_usernames, ) +DEFAULT_PAGE_PARAMS = { + "debug_mode": settings.DEBUG, + "webpack_public_path": staticfiles_storage.url(settings.WEBPACK_BUNDLES), +} + def common_context(user: UserProfile) -> Dict[str, Any]: """Common context used for things like outgoing emails that don't @@ -129,6 +136,11 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]: # get the same result. platform = get_client_name(request) + default_page_params = { + **DEFAULT_PAGE_PARAMS, + "request_language": get_language(), + } + context = { "root_domain_landing_page": settings.ROOT_DOMAIN_LANDING_PAGE, "custom_logo_url": settings.CUSTOM_LOGO_URL, @@ -161,6 +173,7 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]: "platform": platform, "allow_search_engine_indexing": allow_search_engine_indexing, "landing_page_navbar_message": settings.LANDING_PAGE_NAVBAR_MESSAGE, + "default_page_params": default_page_params, } context["OPEN_GRAPH_URL"] = f"{realm_uri}{request.path}" diff --git a/zerver/tests/test_home.py b/zerver/tests/test_home.py index 9fc59dc8ec..1fc26246e5 100644 --- a/zerver/tests/test_home.py +++ b/zerver/tests/test_home.py @@ -202,6 +202,7 @@ class HomeTest(ZulipTestCase): "realm_waiting_period_threshold", "realm_wildcard_mention_policy", "recent_private_conversations", + "request_language", "root_domain_uri", "save_stacktraces", "search_pills_enabled", diff --git a/zproject/jinja2/__init__.py b/zproject/jinja2/__init__.py index 7400967661..02624abaa4 100644 --- a/zproject/jinja2/__init__.py +++ b/zproject/jinja2/__init__.py @@ -1,6 +1,5 @@ from typing import Any -from django.conf import settings from django.contrib.staticfiles.storage import staticfiles_storage from django.template.defaultfilters import pluralize, slugify from django.urls import reverse @@ -9,16 +8,17 @@ from django.utils.timesince import timesince from jinja2 import Environment from two_factor.templatetags.two_factor import device_action +from zerver.context_processors import DEFAULT_PAGE_PARAMS from zerver.templatetags.app_filters import display_list, render_markdown_path, webpack_entry def environment(**options: Any) -> Environment: env = Environment(**options) env.globals.update( - default_page_params={ - "debug_mode": settings.DEBUG, - "webpack_public_path": staticfiles_storage.url(settings.WEBPACK_BUNDLES), - }, + # default_page_params is provided here for responses where + # zulip_default_context is not run, including the 404.html and + # 500.html error pages. + default_page_params=DEFAULT_PAGE_PARAMS, static=staticfiles_storage.url, url=reverse, render_markdown_path=render_markdown_path,