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
|
|
|
|
2016-08-02 14:34:12 +02:00
|
|
|
from six.moves import urllib, zip_longest, zip, range
|
2016-12-21 13:17:53 +01:00
|
|
|
from typing import Any, List, Dict, Optional, Text
|
2016-08-05 22:28:25 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import ujson
|
|
|
|
|
|
|
|
def with_language(string, language):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text, Text) -> Text
|
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()
|
2016-08-05 22:28:25 +02:00
|
|
|
def get_language_list():
|
|
|
|
# type: () -> 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
|
|
|
|
2016-08-02 14:34:12 +02:00
|
|
|
def get_language_list_for_templates(default_language):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (Text) -> 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:
|
|
|
|
percent = u"{} ({}%)".format(name, lang['percent_translated'])
|
|
|
|
|
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
|
|
|
|
|
2016-08-02 14:33:13 +02:00
|
|
|
def get_language_name(code):
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: (str) -> Optional[Text]
|
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
|
|
|
|
2016-08-05 22:28:25 +02:00
|
|
|
def get_available_language_codes():
|
2016-12-21 13:17:53 +01:00
|
|
|
# type: () -> List[Text]
|
2016-08-05 22:28:25 +02:00
|
|
|
language_list = get_language_list()
|
|
|
|
codes = [language['code'] for language in language_list]
|
|
|
|
return codes
|