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 <anders@zulip.com>
This commit is contained in:
Anders Kaseorg
2021-04-23 16:32:32 -07:00
committed by Tim Abbott
parent 1229945add
commit cef2ecf004
5 changed files with 25 additions and 6 deletions

View File

@@ -9,7 +9,7 @@ import {page_params} from "./page_params";
const cache = createIntlCache(); const cache = createIntlCache();
export const intl = createIntl( export const intl = createIntl(
{ {
locale: page_params.default_language, locale: page_params.request_language,
defaultLocale: "en", defaultLocale: "en",
messages: page_params.translation_data, messages: page_params.translation_data,
onError: /* istanbul ignore next */ (error) => { onError: /* istanbul ignore next */ (error) => {

View File

@@ -573,6 +573,11 @@ export function dispatch_normal_event(event) {
} }
if (event.setting_name === "default_language") { if (event.setting_name === "default_language") {
// We additionally need to set the language name. // 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; page_params.default_language_name = event.language_name;
} }
if (event.setting_name === "twenty_four_hour_time") { if (event.setting_name === "twenty_four_hour_time") {

View File

@@ -2,9 +2,11 @@ from typing import Any, Dict, Optional
from urllib.parse import urljoin from urllib.parse import urljoin
from django.conf import settings from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage
from django.http import HttpRequest from django.http import HttpRequest
from django.utils.html import escape from django.utils.html import escape
from django.utils.safestring import SafeString from django.utils.safestring import SafeString
from django.utils.translation import get_language
from version import ( from version import (
LATEST_MAJOR_VERSION, LATEST_MAJOR_VERSION,
@@ -28,6 +30,11 @@ from zproject.backends import (
require_email_format_usernames, 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]: def common_context(user: UserProfile) -> Dict[str, Any]:
"""Common context used for things like outgoing emails that don't """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. # get the same result.
platform = get_client_name(request) platform = get_client_name(request)
default_page_params = {
**DEFAULT_PAGE_PARAMS,
"request_language": get_language(),
}
context = { context = {
"root_domain_landing_page": settings.ROOT_DOMAIN_LANDING_PAGE, "root_domain_landing_page": settings.ROOT_DOMAIN_LANDING_PAGE,
"custom_logo_url": settings.CUSTOM_LOGO_URL, "custom_logo_url": settings.CUSTOM_LOGO_URL,
@@ -161,6 +173,7 @@ def zulip_default_context(request: HttpRequest) -> Dict[str, Any]:
"platform": platform, "platform": platform,
"allow_search_engine_indexing": allow_search_engine_indexing, "allow_search_engine_indexing": allow_search_engine_indexing,
"landing_page_navbar_message": settings.LANDING_PAGE_NAVBAR_MESSAGE, "landing_page_navbar_message": settings.LANDING_PAGE_NAVBAR_MESSAGE,
"default_page_params": default_page_params,
} }
context["OPEN_GRAPH_URL"] = f"{realm_uri}{request.path}" context["OPEN_GRAPH_URL"] = f"{realm_uri}{request.path}"

View File

@@ -202,6 +202,7 @@ class HomeTest(ZulipTestCase):
"realm_waiting_period_threshold", "realm_waiting_period_threshold",
"realm_wildcard_mention_policy", "realm_wildcard_mention_policy",
"recent_private_conversations", "recent_private_conversations",
"request_language",
"root_domain_uri", "root_domain_uri",
"save_stacktraces", "save_stacktraces",
"search_pills_enabled", "search_pills_enabled",

View File

@@ -1,6 +1,5 @@
from typing import Any from typing import Any
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from django.template.defaultfilters import pluralize, slugify from django.template.defaultfilters import pluralize, slugify
from django.urls import reverse from django.urls import reverse
@@ -9,16 +8,17 @@ from django.utils.timesince import timesince
from jinja2 import Environment from jinja2 import Environment
from two_factor.templatetags.two_factor import device_action 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 from zerver.templatetags.app_filters import display_list, render_markdown_path, webpack_entry
def environment(**options: Any) -> Environment: def environment(**options: Any) -> Environment:
env = Environment(**options) env = Environment(**options)
env.globals.update( env.globals.update(
default_page_params={ # default_page_params is provided here for responses where
"debug_mode": settings.DEBUG, # zulip_default_context is not run, including the 404.html and
"webpack_public_path": staticfiles_storage.url(settings.WEBPACK_BUNDLES), # 500.html error pages.
}, default_page_params=DEFAULT_PAGE_PARAMS,
static=staticfiles_storage.url, static=staticfiles_storage.url,
url=reverse, url=reverse,
render_markdown_path=render_markdown_path, render_markdown_path=render_markdown_path,