Files
zulip/zerver/lib/i18n.py
Anders Kaseorg bbb56df6b0 i18n: Move static/locale back to locale.
As of commit cff40c557b (#9300), these
files are no longer served directly to the browser.  Disentangle them
from the static asset pipeline so we can refactor it without worrying
about them.

This has the side effect of eliminating the accidental duplication of
translation data via hash-naming in our release tarballs.

This reverts commit b546391f0b (#1148).

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2019-07-02 14:57:55 -07:00

98 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
import operator
from django.conf import settings
from django.utils import translation
from django.utils.translation import ugettext as _
from django.utils.lru_cache import lru_cache
from itertools import zip_longest
from typing import Any, List, Dict
import os
import ujson
import logging
def with_language(string: str, language: str) -> str:
"""
This is an expensive function. If you are using it in a loop, it will
make your code slow.
"""
old_language = translation.get_language()
translation.activate(language)
result = _(string)
translation.activate(old_language)
return result
@lru_cache()
def get_language_list() -> List[Dict[str, Any]]:
path = os.path.join(settings.DEPLOY_ROOT, 'locale', 'language_name_map.json')
with open(path, 'r') as reader:
languages = ujson.load(reader)
return languages['name_map']
def get_language_list_for_templates(default_language: str) -> List[Dict[str, Dict[str, str]]]:
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 = "{} ({}%)".format(name, lang['percent_translated'])
selected = False
if default_language in (lang['code'], lang['locale']):
selected = True
item[position] = {
'name': name,
'code': lang['code'],
'percent': percent,
'selected': selected
}
formatted_list.append(item)
return formatted_list
def get_language_name(code: str) -> str:
for lang in get_language_list():
if code in (lang['code'], lang['locale']):
return lang['name']
# Log problem, but still return a name
logging.error("Unknown language code '%s'" % (code,))
return "Unknown"
def get_available_language_codes() -> List[str]:
language_list = get_language_list()
codes = [language['code'] for language in language_list]
return codes
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.DEPLOY_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 {}