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]]:
|
2019-07-02 22:38:09 +02: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())
|
2017-09-13 07:04:22 +02:00
|
|
|
return languages['name_map']
|
2016-08-05 22:28:25 +02:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def get_language_list_for_templates(default_language: str) -> List[Dict[str, Dict[str, str]]]:
|
2020-06-03 06:02:53 +02:00
|
|
|
language_list = [lang for lang in get_language_list()
|
|
|
|
if 'percent_translated' not in lang or
|
|
|
|
lang['percent_translated'] >= 5.]
|
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 = {}
|
|
|
|
for position, ind in zip(['first', 'second'], row):
|
|
|
|
if ind is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
lang = language_list[ind]
|
|
|
|
percent = name = lang['name']
|
|
|
|
if 'percent_translated' in lang:
|
2017-11-03 03:12:25 +01:00
|
|
|
percent = "{} ({}%)".format(name, lang['percent_translated'])
|
2016-08-02 14:34:12 +02:00
|
|
|
|
2017-10-20 08:16:18 +02:00
|
|
|
selected = False
|
|
|
|
if default_language in (lang['code'], lang['locale']):
|
|
|
|
selected = True
|
|
|
|
|
2016-08-02 14:34:12 +02:00
|
|
|
item[position] = {
|
|
|
|
'name': name,
|
|
|
|
'code': lang['code'],
|
|
|
|
'percent': percent,
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
'selected': selected,
|
2016-08-02 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatted_list.append(item)
|
|
|
|
|
|
|
|
return formatted_list
|
|
|
|
|
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():
|
2017-10-20 08:16:18 +02:00
|
|
|
if code in (lang['code'], lang['locale']):
|
2016-08-02 14:33:13 +02:00
|
|
|
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
|
|
|
|
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()
|
|
|
|
codes = [language['code'] for language in language_list]
|
|
|
|
return codes
|
2018-05-03 11:08:50 +02:00
|
|
|
|
|
|
|
def get_language_translation_data(language: str) -> Dict[str, str]:
|
2020-09-24 13:04:54 +02:00
|
|
|
if language == 'en':
|
|
|
|
return {}
|
2020-10-23 00:42:03 +02:00
|
|
|
locale = translation.to_locale(language)
|
|
|
|
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:
|
2020-06-09 00:25:09 +02: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
|
|
|
|
|
|
|
def get_and_set_request_language(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_configured_language: str,
|
|
|
|
testing_url_language: Optional[str]=None
|
|
|
|
) -> 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
|