microblog/app/translate.py

20 lines
790 B
Python
Raw Normal View History

2017-10-05 22:34:15 +00:00
import json
import requests
from flask import current_app
2017-10-05 22:34:15 +00:00
from flask_babel import _
def translate(text, source_language, dest_language):
if 'MS_TRANSLATOR_KEY' not in current_app.config or \
not current_app.config['MS_TRANSLATOR_KEY']:
2017-10-05 22:34:15 +00:00
return _('Error: the translation service is not configured.')
auth = {
'Ocp-Apim-Subscription-Key': current_app.config['MS_TRANSLATOR_KEY']}
2017-10-05 22:34:15 +00:00
r = requests.get('https://api.microsofttranslator.com/v2/Ajax.svc'
'/Translate?text={}&from={}&to={}'.format(
text, source_language, dest_language),
headers=auth)
if r.status_code != 200:
return _('Error: the translation service failed.')
return json.loads(r.content.decode('utf-8-sig'))