Commit 64470a17 authored by Michal Čihař's avatar Michal Čihař

Make machine translations compatible with Python 3

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 84e9ffcb
......@@ -21,6 +21,8 @@
Base code for machine translation services.
'''
from __future__ import unicode_literals
import sys
import json
......@@ -75,9 +77,9 @@ class MachineTranslation(object):
'''
# Encode params
if len(kwargs) > 0:
params = urlencode(kwargs)
params = urlencode(kwargs).encode('utf-8')
else:
params = ''
params = b''
# Store for exception handling
self.request_url = url
......@@ -104,8 +106,10 @@ class MachineTranslation(object):
# Read and possibly convert response
text = handle.read()
# Needed for Microsoft
if text.startswith('\xef\xbb\xbf'):
if text[:3] == b'\xef\xbb\xbf':
text = text.decode('UTF-8-sig')
else:
text = text.decode('utf-8')
# Replace literal \t
text = text.strip().replace(
'\t', '\\t'
......
......@@ -70,7 +70,7 @@ class TMServerTranslation(MachineTranslation):
self.url,
quote(appsettings.SOURCE_LANGUAGE),
quote(language),
quote(text[:500].encode('utf-8').replace('\r', ' ')),
quote(text[:500].replace('\r', ' ').encode('utf-8')),
)
response = self.json_req(url)
......
......@@ -195,18 +195,18 @@ class MachineTranslationTest(TestCase):
httpretty.register_uri(
httpretty.GET,
'https://www.googleapis.com/language/translate/v2/languages',
body='{"data": {"languages": [ { "language": "cs" }]}}'
body=b'{"data": {"languages": [ { "language": "cs" }]}}'
)
httpretty.register_uri(
httpretty.GET,
'https://www.googleapis.com/language/translate/v2/',
body='{"data":{"translations":[{"translatedText":"svet"}]}}'
body=b'{"data":{"translations":[{"translatedText":"svet"}]}}'
)
machine = GoogleTranslation()
self.assertTranslate(machine)
@httpretty.activate
def test_google_invalid(self):
def test_google_invalidx(self):
"""Test handling of server failure."""
cache.delete('%s-languages' % GoogleTranslation().mtid)
httpretty.register_uri(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment