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
|
|
|
|
from itertools import zip_longest
|
|
|
|
from typing import Any, Dict, List
|
2016-08-02 14:29:03 +02:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import ujson
|
2016-08-05 22:28:25 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import translation
|
2017-09-13 07:04:22 +02:00
|
|
|
from django.utils.lru_cache import lru_cache
|
2020-06-11 00:54:34 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2016-08-05 22:28:25 +02:00
|
|
|
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def with_language(string: str, language: str) -> str:
|
2017-09-13 07:04:22 +02:00
|
|
|
"""
|
|
|
|
This is an expensive function. If you are using it in a loop, it will
|
|
|
|
make your code slow.
|
|
|
|
"""
|
2016-08-05 22:28:25 +02:00
|
|
|
old_language = translation.get_language()
|
|
|
|
translation.activate(language)
|
|
|
|
result = _(string)
|
|
|
|
translation.activate(old_language)
|
|
|
|
return result
|
|
|
|
|
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-04-09 21:51:58 +02:00
|
|
|
with open(path) as reader:
|
2016-08-05 22:28:25 +02:00
|
|
|
languages = ujson.load(reader)
|
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]:
|
|
|
|
if language == 'zh-hans':
|
|
|
|
language = 'zh_Hans'
|
|
|
|
elif language == 'zh-hant':
|
|
|
|
language = 'zh_Hant'
|
|
|
|
elif language == 'id-id':
|
|
|
|
language = 'id_ID'
|
2019-07-02 22:38:09 +02:00
|
|
|
path = os.path.join(settings.DEPLOY_ROOT, 'locale', language, 'translations.json')
|
2018-05-03 11:08:50 +02:00
|
|
|
try:
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(path) as reader:
|
2018-05-03 11:08:50 +02:00
|
|
|
return ujson.load(reader)
|
|
|
|
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 {}
|