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]))