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
|
2016-08-02 14:34:12 +02:00
|
|
|
import operator
|
2020-06-11 00:54:34 +02:00
|
|
|
import os
|
2020-08-14 09:49:33 +02:00
|
|
|
from functools import lru_cache
|
2020-06-11 00:54:34 +02:00
|
|
|
from itertools import zip_longest
|
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
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def get_language_list_for_templates(default_language: str) -> List[Dict[str, Dict[str, str]]]:
|
2021-02-12 08:19:30 +01:00
|
|
|
language_list = [
|
|
|
|
lang
|
|
|
|
for lang in get_language_list()
|
2021-02-12 08:20:45 +01:00
|
|
|
if "percent_translated" not in lang or lang["percent_translated"] >= 5.0
|
2021-02-12 08:19:30 +01:00
|
|
|
]
|
2016-08-02 14:34:12 +02:00
|
|
|
|
|
|
|
formatted_list = []
|
|
|
|
lang_len = len(language_list)
|
|
|
|
firsts_end = (lang_len // 2) + operator.mod(lang_len, 2)
|
|
|
|
firsts = list(range(0, firsts_end))
|
|
|
|
seconds = list(range(firsts_end, lang_len))
|
|
|
|
assert len(firsts) + len(seconds) == lang_len
|
|
|
|
for row in zip_longest(firsts, seconds):
|
|
|
|
item = {}
|
2021-02-12 08:20:45 +01:00
|
|
|
for position, ind in zip(["first", "second"], row):
|
2016-08-02 14:34:12 +02:00
|
|
|
if ind is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
lang = language_list[ind]
|
2021-02-12 08:20:45 +01:00
|
|
|
percent = name = lang["name"]
|
|
|
|
if "percent_translated" in lang:
|
|
|
|
percent = "{} ({}%)".format(name, lang["percent_translated"])
|
2016-08-02 14:34:12 +02:00
|
|
|
|
2017-10-20 08:16:18 +02:00
|
|
|
selected = False
|
2021-02-12 08:20:45 +01:00
|
|
|
if default_language in (lang["code"], lang["locale"]):
|
2017-10-20 08:16:18 +02:00
|
|
|
selected = True
|
|
|
|
|
2016-08-02 14:34:12 +02:00
|
|
|
item[position] = {
|
2021-02-12 08:20:45 +01:00
|
|
|
"name": name,
|
|
|
|
"code": lang["code"],
|
|
|
|
"percent": percent,
|
|
|
|
"selected": selected,
|
2016-08-02 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatted_list.append(item)
|
|
|
|
|
|
|
|
return formatted_list
|
|
|
|
|
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
|