2016-06-23 11:32:45 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import ujson
|
2016-07-30 05:03:57 +02:00
|
|
|
|
2016-12-08 05:06:51 +01:00
|
|
|
from typing import Any, Dict, List, Text
|
2016-06-23 11:32:45 +02:00
|
|
|
|
|
|
|
from django.core.management.commands import compilemessages
|
|
|
|
from django.conf import settings
|
|
|
|
|
2016-07-26 14:34:18 +02:00
|
|
|
import polib
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2017-09-13 07:04:22 +02:00
|
|
|
from zerver.lib.i18n import with_language
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
class Command(compilemessages.Command):
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-07-30 05:03:57 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2017-02-04 02:06:58 +01:00
|
|
|
if settings.PRODUCTION:
|
|
|
|
# HACK: When using upgrade-zulip-from-git, we're in a
|
|
|
|
# production environment where STATIC_ROOT will include
|
|
|
|
# past versions; this ensures we only process the current
|
|
|
|
# version
|
|
|
|
settings.STATIC_ROOT = os.path.join(settings.DEPLOY_ROOT, "static")
|
|
|
|
settings.LOCALE_PATHS = (os.path.join(settings.DEPLOY_ROOT, 'static/locale'),)
|
2016-06-23 11:32:45 +02:00
|
|
|
super(Command, self).handle(*args, **options)
|
|
|
|
self.extract_language_options()
|
2017-09-13 07:04:22 +02:00
|
|
|
self.create_language_name_map()
|
|
|
|
|
|
|
|
def create_language_name_map(self):
|
|
|
|
# type: () -> None
|
|
|
|
join = os.path.join
|
|
|
|
static_root = settings.STATIC_ROOT
|
|
|
|
path = join(static_root, 'locale', 'language_options.json')
|
|
|
|
output_path = join(static_root, 'locale', 'language_name_map.json')
|
|
|
|
|
|
|
|
with open(path, 'r') as reader:
|
|
|
|
languages = ujson.load(reader)
|
|
|
|
lang_list = []
|
|
|
|
for lang_info in languages['languages']:
|
|
|
|
name = lang_info['name']
|
|
|
|
lang_info['name'] = with_language(name, lang_info['code'])
|
|
|
|
lang_list.append(lang_info)
|
|
|
|
|
|
|
|
lang_list.sort(key=lambda lang: lang['name'])
|
|
|
|
|
|
|
|
with open(output_path, 'w') as output_file:
|
|
|
|
ujson.dump({'name_map': lang_list}, output_file, indent=4)
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2016-07-26 14:34:18 +02:00
|
|
|
def get_po_filename(self, locale_path, locale):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: (Text, Text) -> Text
|
2016-07-26 14:34:18 +02:00
|
|
|
po_template = '{}/{}/LC_MESSAGES/django.po'
|
|
|
|
return po_template.format(locale_path, locale)
|
|
|
|
|
|
|
|
def get_json_filename(self, locale_path, locale):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: (Text, Text) -> Text
|
2016-07-30 05:03:57 +02:00
|
|
|
return "{}/{}/translations.json".format(locale_path, locale)
|
2016-07-26 14:34:18 +02:00
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
def extract_language_options(self):
|
2016-07-30 05:03:57 +02:00
|
|
|
# type: () -> None
|
|
|
|
locale_path = u"{}/locale".format(settings.STATIC_ROOT)
|
|
|
|
output_path = u"{}/language_options.json".format(locale_path)
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2016-08-02 14:25:32 +02:00
|
|
|
data = {'languages': []} # type: Dict[str, List[Dict[str, Any]]]
|
2016-06-23 11:32:45 +02:00
|
|
|
lang_name_re = re.compile('"Language-Team: (.*?) \(')
|
|
|
|
|
2016-07-05 09:25:23 +02:00
|
|
|
locales = os.listdir(locale_path)
|
2016-07-30 05:03:57 +02:00
|
|
|
locales.append(u'en')
|
2016-07-05 09:25:23 +02:00
|
|
|
locales = list(set(locales))
|
|
|
|
|
|
|
|
for locale in locales:
|
2016-08-02 14:25:32 +02:00
|
|
|
info = {} # type: Dict[str, Any]
|
2016-07-30 05:03:57 +02:00
|
|
|
if locale == u'en':
|
2016-07-26 14:34:18 +02:00
|
|
|
data['languages'].append({
|
2016-07-30 05:03:57 +02:00
|
|
|
'code': u'en',
|
|
|
|
'name': u'English',
|
2016-07-26 14:34:18 +02:00
|
|
|
})
|
2016-06-23 11:32:45 +02:00
|
|
|
continue
|
2017-02-03 23:26:10 +01:00
|
|
|
filename = self.get_po_filename(locale_path, locale)
|
|
|
|
if not os.path.exists(filename):
|
2016-07-19 00:02:30 +02:00
|
|
|
continue
|
2017-02-03 23:26:10 +01:00
|
|
|
|
|
|
|
with open(filename, 'r') as reader:
|
|
|
|
result = lang_name_re.search(reader.read())
|
|
|
|
if result:
|
|
|
|
try:
|
|
|
|
name = result.group(1)
|
|
|
|
except Exception:
|
|
|
|
print("Problem in parsing {}".format(filename))
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
raise Exception("Unknown language %s" % (locale,))
|
2016-06-23 11:32:45 +02:00
|
|
|
|
2016-07-26 14:34:18 +02:00
|
|
|
percentage = self.get_translation_percentage(locale_path, locale)
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
info['name'] = name
|
|
|
|
info['code'] = locale
|
2016-07-26 14:34:18 +02:00
|
|
|
info['percent_translated'] = percentage
|
2016-06-23 11:32:45 +02:00
|
|
|
|
|
|
|
if info:
|
|
|
|
data['languages'].append(info)
|
|
|
|
|
|
|
|
with open(output_path, 'w') as writer:
|
2016-07-14 12:00:26 +02:00
|
|
|
ujson.dump(data, writer, indent=2)
|
2016-07-26 14:34:18 +02:00
|
|
|
|
|
|
|
def get_translation_percentage(self, locale_path, locale):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: (Text, Text) -> int
|
2016-07-30 05:03:57 +02:00
|
|
|
|
2016-07-26 14:34:18 +02:00
|
|
|
# backend stats
|
|
|
|
po = polib.pofile(self.get_po_filename(locale_path, locale))
|
|
|
|
not_translated = len(po.untranslated_entries())
|
|
|
|
total = len(po.translated_entries()) + not_translated
|
|
|
|
|
|
|
|
# frontend stats
|
|
|
|
with open(self.get_json_filename(locale_path, locale)) as reader:
|
|
|
|
for key, value in ujson.load(reader).items():
|
|
|
|
total += 1
|
2017-10-04 10:09:24 +02:00
|
|
|
if value == '':
|
2016-07-26 14:34:18 +02:00
|
|
|
not_translated += 1
|
|
|
|
|
2016-08-02 14:25:32 +02:00
|
|
|
return (total - not_translated) * 100 // total
|