2020-03-21 04:11:51 +00:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import random
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
|
|
import googletrans
|
|
|
|
|
import requests
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
import typograph_ru
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
translator = googletrans.Translator()
|
2020-03-30 08:25:29 +00:00
|
|
|
|
default_target_language = os.environ.get('TARGET_LANGUAGE', 'ru')
|
2020-03-21 04:11:51 +00:00
|
|
|
|
|
|
|
|
|
is_yandex = os.environ.get('YANDEX') is not None
|
|
|
|
|
|
|
|
|
|
|
2020-03-30 08:25:29 +00:00
|
|
|
|
def translate(text, target_language=None):
|
|
|
|
|
target_language = target_language or default_target_language
|
2020-03-21 04:11:51 +00:00
|
|
|
|
if target_language == 'en':
|
|
|
|
|
return text
|
|
|
|
|
elif target_language == 'typograph_ru':
|
|
|
|
|
return typograph_ru.typograph(text)
|
|
|
|
|
elif is_yandex:
|
2020-03-21 09:17:06 +00:00
|
|
|
|
text = text.replace('‘', '\'')
|
|
|
|
|
text = text.replace('’', '\'')
|
2020-03-30 08:25:29 +00:00
|
|
|
|
has_alpha = any([char.isalpha() for char in text])
|
|
|
|
|
if text.isascii() and has_alpha and not text.isupper():
|
2020-03-21 09:17:06 +00:00
|
|
|
|
text = urllib.parse.quote(text)
|
|
|
|
|
url = f'http://translate.yandex.net/api/v1/tr.json/translate?srv=docs&lang=en-{target_language}&text={text}'
|
|
|
|
|
result = requests.get(url).json()
|
|
|
|
|
if result.get('code') == 200:
|
|
|
|
|
return result['text'][0]
|
|
|
|
|
else:
|
2020-03-30 08:25:29 +00:00
|
|
|
|
result = str(result)
|
|
|
|
|
print(f'Failed to translate "{text}": {result}', file=sys.stderr)
|
2020-03-21 09:17:06 +00:00
|
|
|
|
sys.exit(1)
|
2020-03-21 04:11:51 +00:00
|
|
|
|
else:
|
2020-03-21 09:17:06 +00:00
|
|
|
|
return text
|
2020-03-21 04:11:51 +00:00
|
|
|
|
else:
|
|
|
|
|
time.sleep(random.random())
|
|
|
|
|
return translator.translate(text, target_language).text
|
|
|
|
|
|
|
|
|
|
|
2020-03-30 12:48:55 +00:00
|
|
|
|
def translate_toc(root, lang):
|
|
|
|
|
global is_yandex
|
|
|
|
|
is_yandex = True
|
2020-03-21 04:11:51 +00:00
|
|
|
|
if isinstance(root, dict):
|
|
|
|
|
result = []
|
|
|
|
|
for key, value in root.items():
|
2020-03-30 12:48:55 +00:00
|
|
|
|
key = translate(key, lang) if key != 'hidden' and not key.isupper() else key
|
|
|
|
|
result.append((key, translate_toc(value, lang),))
|
2020-03-21 04:11:51 +00:00
|
|
|
|
return dict(result)
|
|
|
|
|
elif isinstance(root, list):
|
2020-03-30 12:48:55 +00:00
|
|
|
|
return [translate_toc(item, lang) for item in root]
|
2020-03-21 04:11:51 +00:00
|
|
|
|
elif isinstance(root, str):
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
|
|
|
2020-03-30 08:25:29 +00:00
|
|
|
|
def translate_po():
|
|
|
|
|
import babel.messages.pofile
|
|
|
|
|
base_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'website', 'locale')
|
2020-03-30 12:48:55 +00:00
|
|
|
|
for lang in ['en', 'zh', 'es', 'fr', 'ru', 'ja', 'fa']:
|
2020-03-30 08:25:29 +00:00
|
|
|
|
po_path = os.path.join(base_dir, lang, 'LC_MESSAGES', 'messages.po')
|
|
|
|
|
with open(po_path, 'r') as f:
|
|
|
|
|
po_file = babel.messages.pofile.read_po(f, locale=lang, domain='messages')
|
|
|
|
|
for item in po_file:
|
|
|
|
|
if not item.string:
|
|
|
|
|
global is_yandex
|
|
|
|
|
is_yandex = True
|
|
|
|
|
item.string = translate(item.id, lang)
|
|
|
|
|
with open(po_path, 'wb') as f:
|
|
|
|
|
babel.messages.pofile.write_po(f, po_file)
|
|
|
|
|
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
target_language = sys.argv[1]
|
2020-03-30 08:25:29 +00:00
|
|
|
|
if target_language == 'po':
|
|
|
|
|
translate_po()
|
|
|
|
|
else:
|
2020-03-30 12:48:55 +00:00
|
|
|
|
result = translate_toc(yaml.full_load(sys.stdin.read())['nav'], sys.argv[1])
|
2020-03-30 08:25:29 +00:00
|
|
|
|
print(yaml.dump({'nav': result}))
|