from flask import request, Blueprint, Response

from util import json_util
from service.translate import youdao,alibaba


translator = Blueprint('translator', __name__)


@translator.route('/translate', methods=['POST'])
def translate():
    from_lang = request.args.get("from_lang")
    to_lang = request.args.get("to_lang")
    json_body = request.json
    text = json_body["text"]
    if not from_lang or not to_lang or not text:
        raise ValueError("Invalid params")

    ts = alibaba.AlibabaTranslator()
    r = ts.translate(from_lang, to_lang, text)
    return Response(r, mimetype='application/json')