diff --git a/doc/changelog.md b/doc/changelog.md
index f299071e..3dc37288 100644
--- a/doc/changelog.md
+++ b/doc/changelog.md
@@ -12,6 +12,7 @@
* TWBlue can upload images in Tweets and replies again. ([#240,](https://github.com/manuelcortez/TWBlue/issues/240))
* Fixed the way we use to count characters in Twitter. The new methods in TWBlue take into account special characters and URLS as documented in Twitter. ([#199,](https://github.com/manuelcortez/TWBlue/issues/199) [#315](https://github.com/manuelcortez/TWBlue/issues/315))
* Proxy support now works as expected.
+* Changed translation service from yandex.translate to Google Translator. ([#355,](https://github.com/manuelcortez/TWBlue/issues/355))
* And more. ([#352,](https://github.com/manuelcortez/TWBlue/issues/352))
## Changes in version 0.95
diff --git a/requirements.txt b/requirements.txt
index 6bf36fff..b95337d7 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -16,7 +16,9 @@ futures
winpaths
PySocks
win_inet_pton
-yandex.translate
+# Install the latest RC of this lib
+# see https://github.com/ssut/py-googletrans/issues/234
+googletrans==4.0.0-rc1
idna<3,>=2.5
chardet
urllib3
diff --git a/src/controller/messages.py b/src/controller/messages.py
index 880ec4a2..62d4a5fa 100644
--- a/src/controller/messages.py
+++ b/src/controller/messages.py
@@ -45,8 +45,12 @@ class basicTweet(object):
dlg = translator.gui.translateDialog()
if dlg.get_response() == widgetUtils.OK:
text_to_translate = self.message.get_text()
- dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")]
- msg = translator.translator.translate(text=text_to_translate, target=dest)
+ language_dict = translator.translator.available_languages()
+ print(dlg.dest_lang.GetStringSelection())
+ for k in language_dict:
+ if language_dict[k] == dlg.dest_lang.GetStringSelection():
+ dst = k
+ msg = translator.translator.translate(text=text_to_translate, target=dst)
self.message.set_text(msg)
self.text_processor()
self.message.text_focus()
diff --git a/src/extra/translator/translator.py b/src/extra/translator/translator.py
index f390fbf2..ae08a4e8 100644
--- a/src/extra/translator/translator.py
+++ b/src/extra/translator/translator.py
@@ -1,15 +1,23 @@
# -*- coding: utf-8 -*-
-from __future__ import unicode_literals
-from builtins import zip
-from yandex_translate import YandexTranslate
+import logging
+from googletrans import Translator, LANGUAGES
+
+log = logging.getLogger("extras.translator")
+
+# create a single translator instance
+# see https://github.com/ssut/py-googletrans/issues/234
+t = None
def translate(text="", target="en"):
- t = YandexTranslate("trnsl.1.1.20161012T134532Z.d01b9c75fc39aa74.7d1be75a5166a80583eeb020e10f584168da6bf7")
- vars = dict(text=text, lang=target)
- return t.translate(**vars)["text"][0]
+ global t
+ log.debug("Received translation request for language %s, text=%s" % (target, text))
+ if t == None:
+ t = Translator()
+ vars = dict(text=text, dest=target)
+ return t.translate(**vars).text
supported_langs = None
-d = None
+
languages = {
"af": _(u"Afrikaans"),
"sq": _(u"Albanian"),
@@ -105,11 +113,4 @@ languages = {
}
def available_languages():
- global supported_langs, d
- if supported_langs == None and d == None:
- t = YandexTranslate("trnsl.1.1.20161012T134532Z.d01b9c75fc39aa74.7d1be75a5166a80583eeb020e10f584168da6bf7")
- supported_langs = t.langs
- d = []
- for i in supported_langs:
- d.append(languages[i])
- return sorted(zip(supported_langs, d))
+ return dict(sorted(languages.items(), key=lambda x: x[1]))
diff --git a/src/extra/translator/wx_ui.py b/src/extra/translator/wx_ui.py
index 14d2a88e..8b9672e3 100644
--- a/src/extra/translator/wx_ui.py
+++ b/src/extra/translator/wx_ui.py
@@ -16,37 +16,21 @@
# along with this program. If not, see .
#
############################################################
-from __future__ import absolute_import
-from __future__ import unicode_literals
-# -*- coding: utf-8 -*-
-############################################################
-# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo
-#
-# 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 2 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 .
-#
-############################################################
from . import translator
import wx
from wxUI.dialogs import baseDialog
class translateDialog(baseDialog.BaseWXDialog):
def __init__(self):
+ languages = []
+ language_dict = translator.available_languages()
+ for k in language_dict:
+ languages.append(language_dict[k])
super(translateDialog, self).__init__(None, -1, title=_(u"Translate message"))
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
staticDest = wx.StaticText(panel, -1, _(u"Target language"))
- self.dest_lang = wx.ComboBox(panel, -1, choices=[x[1] for x in translator.available_languages()], style = wx.CB_READONLY)
+ self.dest_lang = wx.ComboBox(panel, -1, choices=languages, style = wx.CB_READONLY)
self.dest_lang.SetFocus()
self.dest_lang.SetSelection(0)
listSizer = wx.BoxSizer(wx.HORIZONTAL)