Commit 7f9eb712 authored by Michal Čihař's avatar Michal Čihař

Implement Google translator support

This might be legally problematic, it's up to site admin whether to
enable this service.

Fixes issue #136
parent 12cfe195
......@@ -83,8 +83,12 @@ class MachineTranslation(object):
# Read and possibly convert response
text = handle.read()
# Needed for Microsoft
if text.startswith('\xef\xbb\xbf'):
text = text.decode('UTF-8-sig')
# Needed for Google
while ',,' in text:
text = text.replace(',,', ',null,')
# Parse JSON
response = json.loads(text)
......
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2013 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from trans.machine.base import MachineTranslation
class GoogleTranslation(MachineTranslation):
'''
Google machine translation support.
'''
name = 'Google'
def convert_language(self, language):
'''
Converts language to service specific code.
'''
return language.replace('_', '-').lower()
def is_supported(self, language):
'''
Any language is supported.
'''
return True
def download_translations(self, language, text):
'''
Downloads list of possible translations from a service.
'''
response = self.json_req(
'http://translate.google.com/translate_a/t',
client='t',
text=text,
sl='en',
tl=language,
)
return [(response[0][0][0], 100)]
......@@ -28,6 +28,7 @@ from trans.machine.apertium import ApertiumTranslation
from trans.machine.microsoft import (
MicrosoftTranslation, microsoft_translation_supported
)
from trans.machine.google import GoogleTranslation
class MachineTranslationTest(TestCase):
......@@ -71,3 +72,7 @@ class MachineTranslationTest(TestCase):
def test_microsoft(self):
machine = MicrosoftTranslation()
self.assertGreater(len(machine.translate('cs', 'world')), 0)
def test_google(self):
machine = GoogleTranslation()
self.assertGreater(len(machine.translate('cs', 'world')), 0)
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