2016-06-23 11:32:45 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import ujson
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
class Command(compilemessages.Command):
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
super(Command, self).handle(*args, **options)
|
|
|
|
self.extract_language_options()
|
|
|
|
|
2016-07-26 14:34:18 +02:00
|
|
|
def get_po_filename(self, locale_path, locale):
|
|
|
|
po_template = '{}/{}/LC_MESSAGES/django.po'
|
|
|
|
return po_template.format(locale_path, locale)
|
|
|
|
|
|
|
|
def get_json_filename(self, locale_path, locale):
|
|
|
|
return "{}/{}/translations.json".format(locale_path, locale)
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
def extract_language_options(self):
|
2016-07-14 12:00:26 +02:00
|
|
|
locale_path = "{}/locale".format(settings.STATIC_ROOT)
|
|
|
|
output_path = "{}/language_options.json".format(locale_path)
|
2016-06-23 11:32:45 +02:00
|
|
|
|
|
|
|
data = {'languages': []} # type: Dict[str, List[Dict[str, str]]]
|
|
|
|
lang_name_re = re.compile('"Language-Team: (.*?) \(')
|
|
|
|
|
2016-07-05 09:25:23 +02:00
|
|
|
locales = os.listdir(locale_path)
|
|
|
|
locales.append('en')
|
|
|
|
locales = list(set(locales))
|
|
|
|
|
|
|
|
for locale in locales:
|
2016-06-23 11:32:45 +02:00
|
|
|
info = {}
|
|
|
|
if locale == 'en':
|
2016-07-26 14:34:18 +02:00
|
|
|
data['languages'].append({
|
|
|
|
'code': 'en',
|
|
|
|
'name': 'English',
|
|
|
|
})
|
2016-06-23 11:32:45 +02:00
|
|
|
continue
|
2016-07-19 00:02:30 +02:00
|
|
|
if locale == 'zh-CN':
|
|
|
|
continue
|
2016-06-23 11:32:45 +02:00
|
|
|
if locale == 'zh_CN':
|
|
|
|
name = 'Simplified Chinese'
|
|
|
|
else:
|
2016-07-26 14:34:18 +02:00
|
|
|
filename = self.get_po_filename(locale_path, locale)
|
2016-07-14 12:00:26 +02:00
|
|
|
if not os.path.exists(filename):
|
|
|
|
continue
|
|
|
|
|
2016-06-23 11:32:45 +02: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:
|
2016-07-19 00:02:07 +02:00
|
|
|
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):
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# There is a difference between frontend and backend files for Chinese
|
|
|
|
if locale == 'zh_CN':
|
|
|
|
locale = 'zh-CN'
|
|
|
|
|
|
|
|
# frontend stats
|
|
|
|
with open(self.get_json_filename(locale_path, locale)) as reader:
|
|
|
|
for key, value in ujson.load(reader).items():
|
|
|
|
total += 1
|
|
|
|
if key == value:
|
|
|
|
not_translated += 1
|
|
|
|
|
|
|
|
return "{}%".format((total - not_translated) * 100 // total)
|