From a073a5caaa36d5e704f3254b19b0358d29f26bf3 Mon Sep 17 00:00:00 2001 From: =Manuel Cortez Date: Sun, 30 Jul 2017 09:58:39 -0500 Subject: [PATCH] Removed source language in translator. Also fixed #153 --- doc/changelog.md | 1 + src/controller/messages.py | 3 +-- src/extra/translator/translator.py | 23 ++++++++++++++--------- src/extra/translator/wx_ui.py | 8 ++------ 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index 76a52040..441b7b03 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -3,6 +3,7 @@ ## changes in this version * When authorising an account, you will see a dialogue with a cancel button, in case you want to abort the process. Also, NVDA will not be blocked when the process starts. ([#101](https://github.com/manuelcortez/TWBlue/issues/101)) +* In the translator module, the list of available languages is fetched automatically from the provider. That means all of these languages will work and there will not be inconsistencies. Also we've removed the first combo box, because the language is detected automatically by Yandex'S API. ([#153](https://github.com/manuelcortez/TWBlue/issues/153)) ## changes in version 0.91 and 0.92 diff --git a/src/controller/messages.py b/src/controller/messages.py index dde3a9ee..52df839b 100644 --- a/src/controller/messages.py +++ b/src/controller/messages.py @@ -40,9 +40,8 @@ class basicTweet(object): dlg = translator.gui.translateDialog() if dlg.get_response() == widgetUtils.OK: text_to_translate = self.message.get_text() - source = [x[0] for x in translator.translator.available_languages()][dlg.get("source_lang")] dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")] - msg = translator.translator.translate(text=text_to_translate, source=source, target=dest) + msg = translator.translator.translate(text=text_to_translate, target=dest) 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 9caa697d..7852741c 100644 --- a/src/extra/translator/translator.py +++ b/src/extra/translator/translator.py @@ -1,11 +1,13 @@ # -*- coding: utf-8 -*- from yandex_translate import YandexTranslate -def translate(text="", source="auto", target="en"): +def translate(text="", target="en"): t = YandexTranslate("trnsl.1.1.20161012T134532Z.d01b9c75fc39aa74.7d1be75a5166a80583eeb020e10f584168da6bf7") - return t.translate(text, target)["text"][0] - + vars = dict(text=text, lang=target) + return t.translate(**vars)["text"][0] +supported_langs = None +d = None languages = { "af": _(u"Afrikaans"), "sq": _(u"Albanian"), @@ -71,7 +73,7 @@ languages = { "ps": _(u"Pashto"), "fa": _(u"Persian"), "pl": _(u"Polish"), - "pt-PT": _(u"Portuguese"), + "pt": _(u"Portuguese"), "pa": _(u"Punjabi"), "ro": _(u"Romanian"), "ru": _(u"Russian"), @@ -101,8 +103,11 @@ languages = { } def available_languages(): - l = languages.keys() - d = languages.values() - l.insert(0, '') - d.insert(0, _(u"autodetect")) - return sorted(zip(l, d)) + 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)) diff --git a/src/extra/translator/wx_ui.py b/src/extra/translator/wx_ui.py index 24689421..a61c9e7e 100644 --- a/src/extra/translator/wx_ui.py +++ b/src/extra/translator/wx_ui.py @@ -25,15 +25,11 @@ class translateDialog(baseDialog.BaseWXDialog): super(translateDialog, self).__init__(None, -1, title=_(u"Translate message")) panel = wx.Panel(self) sizer = wx.BoxSizer(wx.VERTICAL) - staticSource = wx.StaticText(panel, -1, _(u"Source language")) - self.source_lang = wx.ComboBox(panel, -1, choices=[x[1] for x in translator.available_languages()], style = wx.CB_READONLY) - self.source_lang.SetFocus() staticDest = wx.StaticText(panel, -1, _(u"Target language")) - self.source_lang.SetSelection(0) self.dest_lang = wx.ComboBox(panel, -1, choices=[x[1] for x in translator.available_languages()], style = wx.CB_READONLY) + self.dest_lang.SetFocus() + self.dest_lang.SetSelection(0) listSizer = wx.BoxSizer(wx.HORIZONTAL) - listSizer.Add(staticSource) - listSizer.Add(self.source_lang) listSizer.Add(staticDest) listSizer.Add(self.dest_lang) ok = wx.Button(panel, wx.ID_OK)