From 304d91e8b7506d0a4494aaa5c3607bf3a8b6fcfb Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Fri, 22 Jan 2021 17:55:15 -0600 Subject: [PATCH 1/2] Started replacing yandex.translate with googletrans --- src/controller/messages.py | 8 ++++++-- src/extra/translator/translator.py | 21 ++++++--------------- src/extra/translator/wx_ui.py | 26 +++++--------------------- 3 files changed, 17 insertions(+), 38 deletions(-) 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..e1a24091 100644 --- a/src/extra/translator/translator.py +++ b/src/extra/translator/translator.py @@ -1,15 +1,13 @@ # -*- coding: utf-8 -*- -from __future__ import unicode_literals -from builtins import zip -from yandex_translate import YandexTranslate +from googletrans import Translator, LANGUAGES 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] + t = Translator() + vars = dict(text=text, dst=target) + return t.translate(**vars).text supported_langs = None -d = None + languages = { "af": _(u"Afrikaans"), "sq": _(u"Albanian"), @@ -105,11 +103,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 languages 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) From 351f7009272086cf516eaf887ab3d11afd6fed1f Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 26 Jan 2021 16:01:26 -0600 Subject: [PATCH 2/2] Replaced translation services. Fixes #355 --- doc/changelog.md | 1 + requirements.txt | 4 +++- src/extra/translator/translator.py | 16 +++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) 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/extra/translator/translator.py b/src/extra/translator/translator.py index e1a24091..ae08a4e8 100644 --- a/src/extra/translator/translator.py +++ b/src/extra/translator/translator.py @@ -1,9 +1,19 @@ # -*- coding: utf-8 -*- +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 = Translator() - vars = dict(text=text, dst=target) + 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 @@ -103,4 +113,4 @@ languages = { } def available_languages(): - return languages + return dict(sorted(languages.items(), key=lambda x: x[1]))