2016-08-05 22:28:25 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-02 14:34:12 +02:00
|
|
|
import operator
|
2016-08-02 14:29:03 +02:00
|
|
|
|
2016-08-05 22:28:25 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import translation
|
|
|
|
from django.utils.translation import ugettext as _
|
2017-09-13 07:04:22 +02:00
|
|
|
from django.utils.lru_cache import lru_cache
|
2016-08-05 22:28:25 +02:00
|
|
|
|
2017-11-05 06:39:22 +01:00
|
|
|
from itertools import zip_longest
|
2018-05-11 01:40:23 +02:00
|
|
|
from typing import Any, List, Dict, Optional
|
2016-08-05 22:28:25 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import ujson
|
|
|
|
|
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]]:
|
2017-09-13 07:04:22 +02:00
|
|
|
path = os.path.join(settings.STATIC_ROOT, 'locale', 'language_name_map.json')
|
2016-08-05 22:28:25 +02:00
|
|
|
with open(path, 'r') as reader:
|
|
|
|
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]]]:
|
2016-08-02 14:34:12 +02:00
|
|
|
language_list = [l for l in get_language_list()
|
|
|
|
if 'percent_translated' not in l or
|
|
|
|
l['percent_translated'] >= 5.]
|
|
|
|
|
|
|
|
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,
|
2017-10-20 08:16:18 +02:00
|
|
|
'selected': selected
|
2016-08-02 14:34:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatted_list.append(item)
|
|
|
|
|
|
|
|
return formatted_list
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def get_language_name(code: str) -> Optional[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']
|
2017-03-03 20:30:49 +01:00
|
|
|
return None
|
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'
|
|
|
|
path = os.path.join(settings.STATIC_ROOT, 'locale', language, 'translations.json')
|
|
|
|
try:
|
|
|
|
with open(path, 'r') as reader:
|
|
|
|
return ujson.load(reader)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print('Translation for {} not found at {}'.format(language, path))
|
|
|
|
return {}
|