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 @@ ...@@ -21,6 +21,8 @@
Base code for machine translation services. Base code for machine translation services.
''' '''
from __future__ import unicode_literals
import sys import sys
import json import json
...@@ -75,9 +77,9 @@ class MachineTranslation(object): ...@@ -75,9 +77,9 @@ class MachineTranslation(object):
''' '''
# Encode params # Encode params
if len(kwargs) > 0: if len(kwargs) > 0:
params = urlencode(kwargs) params = urlencode(kwargs).encode('utf-8')
else: else:
params = '' params = b''
# Store for exception handling # Store for exception handling
self.request_url = url self.request_url = url
...@@ -104,8 +106,10 @@ class MachineTranslation(object): ...@@ -104,8 +106,10 @@ class MachineTranslation(object):
# Read and possibly convert response # Read and possibly convert response
text = handle.read() text = handle.read()
# Needed for Microsoft # Needed for Microsoft
if text.startswith('\xef\xbb\xbf'): if text[:3] == b'\xef\xbb\xbf':
text = text.decode('UTF-8-sig') text = text.decode('UTF-8-sig')
else:
text = text.decode('utf-8')
# Replace literal \t # Replace literal \t
text = text.strip().replace( text = text.strip().replace(
'\t', '\\t' '\t', '\\t'
......
...@@ -70,7 +70,7 @@ class TMServerTranslation(MachineTranslation): ...@@ -70,7 +70,7 @@ class TMServerTranslation(MachineTranslation):
self.url, self.url,
quote(appsettings.SOURCE_LANGUAGE), quote(appsettings.SOURCE_LANGUAGE),
quote(language), quote(language),
quote(text[:500].encode('utf-8').replace('\r', ' ')), quote(text[:500].replace('\r', ' ').encode('utf-8')),
) )
response = self.json_req(url) response = self.json_req(url)
......
...@@ -195,18 +195,18 @@ class MachineTranslationTest(TestCase): ...@@ -195,18 +195,18 @@ class MachineTranslationTest(TestCase):
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
'https://www.googleapis.com/language/translate/v2/languages', 'https://www.googleapis.com/language/translate/v2/languages',
body='{"data": {"languages": [ { "language": "cs" }]}}' body=b'{"data": {"languages": [ { "language": "cs" }]}}'
) )
httpretty.register_uri( httpretty.register_uri(
httpretty.GET, httpretty.GET,
'https://www.googleapis.com/language/translate/v2/', 'https://www.googleapis.com/language/translate/v2/',
body='{"data":{"translations":[{"translatedText":"svet"}]}}' body=b'{"data":{"translations":[{"translatedText":"svet"}]}}'
) )
machine = GoogleTranslation() machine = GoogleTranslation()
self.assertTranslate(machine) self.assertTranslate(machine)
@httpretty.activate @httpretty.activate
def test_google_invalid(self): def test_google_invalidx(self):
"""Test handling of server failure.""" """Test handling of server failure."""
cache.delete('%s-languages' % GoogleTranslation().mtid) cache.delete('%s-languages' % GoogleTranslation().mtid)
httpretty.register_uri( 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