示例#1
0
def translate_text(request):
    language_from = request.GET.get('from', None)
    language_to = request.GET.get('to', None)
    text = request.GET.get('text', None)

    if language_from == language_to:
        data = {'success': True, 'translation': text}
    else:
        # run the translation:
        AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
        AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)

        translator = Translator(AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)

        try:
            translated_text = translator.translate(text, language_to)
            data = {'success': True, 'translation': translated_text}
        except TranslateApiException as e:
            data = {'success': False, 'error': "Translation API Exception: {0}".format(e.message)}

    return HttpResponse(json.dumps(data), mimetype='application/json')
示例#2
0
def translate_text(request):
    language_from = request.GET.get('from', None)
    language_to = request.GET.get('to', None)
    text = request.GET.get('text', None)

    if language_from == language_to:
        data = {'success': True, 'translation': text}
    else:
        # run the translation:
        AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
        AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)

        translator = Translator(AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)

        try:
            translated_text = translator.translate(text, language_to)
            data = {'success': True, 'translation': translated_text}
        except TranslateApiException as e:
            data = {'success': False, 'error': "Translation API Exception: {0}".format(e.message)}

    return HttpResponse(json.dumps(data), mimetype='application/json')
示例#3
0
 def test_invalid_client_id(self):
     client = Translator("foo", "bar")
     with self.assertRaises(TranslateApiException):
         client.translate("hello", "pt")
示例#4
0
 def test_translate(self):
     client = Translator(client_id, client_secret, debug=True)
     self.assertEqual(client.translate("hello", "pt"), u'Ol\xe1')
示例#5
0
 def test_invalid_client_id(self):
     client = Translator("foo", "bar")
     with self.assertRaises(TranslateApiException):
         client.translate("hello", "pt")
示例#6
0
 def test_translate(self):
     client = Translator(client_id, client_secret, debug=True)
     self.assertEqual(client.translate("hello", "pt"), u'Ol\xe1')