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
|
2020-10-02 14:23:45 +02:00
|
|
|
from typing import Any, Dict, List, Optional
|
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
|
2016-08-05 22:28:25 +02:00
|
|
|
|
|
|
|
|
2017-09-13 07:04:22 +02:00
|
|
|
@lru_cache()
|
2017-11-05 11:15:10 +01: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
|
|
|
|
2018-05-11 01:40:23 +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
|
|
|
|
2018-05-03 11:08:50 +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(
|
2021-02-12 08:19:30 +01:00
|
|
|
request: HttpRequest, user_configured_language: str, testing_url_language: Optional[str] = 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.
|
|
|
|
request._set_language = translation.get_language()
|
2020-10-02 14:23:45 +02:00
|
|
|
|
|
|
|
return request_language
|