2020-09-18 20:43:19 +02:00
|
|
|
# See https://zulip.readthedocs.io/en/latest/translating/internationalization.html
|
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import logging
|
|
|
|
import os
|
2020-08-14 09:49:33 +02:00
|
|
|
from functools import lru_cache
|
2024-07-12 02:30:23 +02:00
|
|
|
from typing import Any
|
2016-08-02 14:29:03 +02:00
|
|
|
|
2020-08-07 01:09:47 +02:00
|
|
|
import orjson
|
2016-08-05 22:28:25 +02:00
|
|
|
from django.conf import settings
|
2020-10-02 14:23:45 +02:00
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.utils import translation
|
2022-01-20 15:49:21 +01:00
|
|
|
from django.utils.translation.trans_real import parse_accept_lang_header
|
2016-08-05 22:28:25 +02:00
|
|
|
|
2021-08-21 19:24:20 +02:00
|
|
|
from zerver.lib.request import RequestNotes
|
2021-04-09 23:09:56 +02:00
|
|
|
from zerver.models import Realm
|
2021-07-09 15:17:33 +02:00
|
|
|
|
2016-08-05 22:28:25 +02:00
|
|
|
|
2022-04-27 02:13:11 +02:00
|
|
|
@lru_cache(None)
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_language_list() -> list[dict[str, Any]]:
|
2021-02-12 08:20:45 +01:00
|
|
|
path = os.path.join(settings.DEPLOY_ROOT, "locale", "language_name_map.json")
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(path, "rb") as reader:
|
|
|
|
languages = orjson.loads(reader.read())
|
2021-02-12 08:20:45 +01:00
|
|
|
return languages["name_map"]
|
2016-08-05 22:28:25 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2019-03-20 18:07:37 +01:00
|
|
|
def get_language_name(code: str) -> str:
|
2016-08-02 14:33:13 +02:00
|
|
|
for lang in get_language_list():
|
2021-02-12 08:20:45 +01:00
|
|
|
if code in (lang["code"], lang["locale"]):
|
|
|
|
return lang["name"]
|
2019-03-20 18:07:37 +01:00
|
|
|
# Log problem, but still return a name
|
2020-05-02 08:44:14 +02:00
|
|
|
logging.error("Unknown language code '%s'", code)
|
2019-03-20 18:07:37 +01:00
|
|
|
return "Unknown"
|
2016-08-02 14:33:13 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_available_language_codes() -> list[str]:
|
2016-08-05 22:28:25 +02:00
|
|
|
language_list = get_language_list()
|
2021-02-12 08:20:45 +01:00
|
|
|
codes = [language["code"] for language in language_list]
|
2016-08-05 22:28:25 +02:00
|
|
|
return codes
|
2018-05-03 11:08:50 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
def get_language_translation_data(language: str) -> dict[str, str]:
|
2021-02-12 08:20:45 +01:00
|
|
|
if language == "en":
|
2020-09-24 13:04:54 +02:00
|
|
|
return {}
|
2020-10-23 00:42:03 +02:00
|
|
|
locale = translation.to_locale(language)
|
2021-02-12 08:20:45 +01:00
|
|
|
path = os.path.join(settings.DEPLOY_ROOT, "locale", locale, "translations.json")
|
2018-05-03 11:08:50 +02:00
|
|
|
try:
|
2020-08-07 01:09:47 +02:00
|
|
|
with open(path, "rb") as reader:
|
|
|
|
return orjson.loads(reader.read())
|
2018-05-03 11:08:50 +02:00
|
|
|
except FileNotFoundError:
|
2021-02-12 08:20:45 +01:00
|
|
|
print(f"Translation for {language} not found at {path}")
|
2018-05-03 11:08:50 +02:00
|
|
|
return {}
|
2020-10-02 14:23:45 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-10-02 14:23:45 +02:00
|
|
|
def get_and_set_request_language(
|
2024-07-12 02:30:23 +02:00
|
|
|
request: HttpRequest, user_configured_language: str, testing_url_language: str | None = None
|
2020-10-02 14:23:45 +02:00
|
|
|
) -> str:
|
|
|
|
# We pick a language for the user as follows:
|
|
|
|
# * First priority is the language in the URL, for debugging.
|
|
|
|
# * If not in the URL, we use the language from the user's settings.
|
|
|
|
request_language = testing_url_language
|
|
|
|
if request_language is None:
|
|
|
|
request_language = user_configured_language
|
|
|
|
translation.activate(request_language)
|
|
|
|
|
2020-12-23 13:45:24 +01:00
|
|
|
# We also want to save the language to the user's cookies, so that
|
2020-10-02 14:23:45 +02:00
|
|
|
# something reasonable will happen in logged-in portico pages.
|
2020-12-23 13:45:24 +01:00
|
|
|
# We accomplish that by setting a flag on the request which signals
|
|
|
|
# to LocaleMiddleware to set the cookie on the response.
|
2021-08-21 19:24:20 +02:00
|
|
|
RequestNotes.get_notes(request).set_language = translation.get_language()
|
2020-10-02 14:23:45 +02:00
|
|
|
|
|
|
|
return request_language
|
2022-01-20 15:49:21 +01:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_browser_language_code(request: HttpRequest) -> str | None:
|
2022-05-12 06:54:12 +02:00
|
|
|
accept_lang_header = request.headers.get("Accept-Language")
|
2022-01-20 15:49:21 +01:00
|
|
|
if accept_lang_header is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
available_language_codes = get_available_language_codes()
|
2022-05-12 06:54:12 +02:00
|
|
|
for accept_lang, priority in parse_accept_lang_header(accept_lang_header):
|
2022-01-20 15:49:21 +01:00
|
|
|
if accept_lang == "*":
|
|
|
|
return None
|
|
|
|
if accept_lang in available_language_codes:
|
|
|
|
return accept_lang
|
|
|
|
return None
|
2021-04-09 23:09:56 +02:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def get_default_language_for_new_user(realm: Realm, *, request: HttpRequest | None) -> str:
|
2023-09-29 19:09:45 +02:00
|
|
|
if request is None:
|
|
|
|
# Users created via the API or LDAP will not have a
|
|
|
|
# browser/request associated with them, and should just use
|
|
|
|
# the realm's default language.
|
|
|
|
return realm.default_language
|
|
|
|
|
2021-04-09 23:09:56 +02:00
|
|
|
browser_language_code = get_browser_language_code(request)
|
|
|
|
if browser_language_code is not None:
|
|
|
|
return browser_language_code
|
|
|
|
return realm.default_language
|