commit/instantTranslate: ABuffEr: Fixed language detection and auto switching, disabled old script to open settings, added message when translation fails instead of raising exception; changed version template and compatibility flag

  • From: commits-noreply@xxxxxxxxxxxxx
  • To: nvda-addons-commits@xxxxxxxxxxxxx
  • Date: Fri, 08 Feb 2019 21:02:06 +0000 (UTC)

1 new commit in instantTranslate:

https://bitbucket.org/nvdaaddonteam/instanttranslate/commits/85ed663fd6f0/
Changeset:   85ed663fd6f0
Branch:      master
User:        ABuffEr
Date:        2019-02-08 21:01:39+00:00
Summary:     Fixed language detection and auto switching, disabled old script 
to open settings, added message when translation fails instead of raising 
exception; changed version template and compatibility flag

Affected #:  3 files

diff --git a/addon/globalPlugins/instantTranslate/__init__.py 
b/addon/globalPlugins/instantTranslate/__init__.py
index c332218..8fb939d 100644
--- a/addon/globalPlugins/instantTranslate/__init__.py
+++ b/addon/globalPlugins/instantTranslate/__init__.py
@@ -7,29 +7,29 @@
 #This file is covered by the GNU General Public License.
 #See the file COPYING for more details.
 
-import urllib
-import json
-import gui
-from gui import NVDASettingsDialog
-import wx
-import api
-import textInfos
-import tones
-import scriptHandler
-import globalPluginHandler
 from functools import wraps
-import queueHandler
-import ui
-import config
+from interface import InstantTranslateSettingsPanel
+from langslist import g
 from locale import getdefaultlocale
-import globalVars
-from interface import *
-from translator import Translator
-from tones import beep
 from time import sleep
-import threading
+from tones import beep
+from translator import Translator
 import addonHandler
-from langslist import g
+import api
+import config
+import globalPluginHandler
+import globalVars
+import gui
+import json
+import os
+import queueHandler
+import scriptHandler
+import textInfos
+import threading
+import tones
+import ui
+import urllib
+import wx
 
 _addonDir = os.path.join(os.path.dirname(__file__), "..", "..").decode("mbcs")
 _curAddon = addonHandler.Addon(_addonDir)
@@ -69,10 +69,10 @@ def finally_(func, final):
                return new
        return wrap(final)
 
-def detect_language(text):
-       
response=urllib.urlopen("https://translate.yandex.net/api/v1.5/tr.json/detect?key=trnsl.1.1.20150410T053856Z.1c57628dc3007498.d36b0117d8315e9cab26f8e0302f6055af8132d7&"+urllib.urlencode({"text":text.encode('utf-8')})).read()
-       response=json.loads(response)
-       return response['lang']
+#def detect_language(text):
+#      
response=urllib.urlopen("https://translate.yandex.net/api/v1.5/tr.json/detect?key=trnsl.1.1.20150410T053856Z.1c57628dc3007498.d36b0117d8315e9cab26f8e0302f6055af8132d7&"+urllib.urlencode({"text":text.encode('utf-8')})).read()
+#      response=json.loads(response)
+#      return response['lang']
 
 class GlobalPlugin(globalPluginHandler.GlobalPlugin):
        scriptCategory = unicode(_addonSummary)
@@ -81,11 +81,11 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
                super(GlobalPlugin, self).__init__(*args, **kwargs)
                if globalVars.appArgs.secure:
                        return
-               
NVDASettingsDialog.categoryClasses.append(InstantTranslateSettingsPanel)
                self.getUpdatedGlobalVars()
                self.toggling = False
                self.maxCachedResults = 5
                self.cachedResults = []
+               
gui.settingsDialogs.NVDASettingsDialog.categoryClasses.append(InstantTranslateSettingsPanel)
 
        def getUpdatedGlobalVars(self):
                global lang_from, lang_to, lang_swap, copyTranslation, 
autoSwap, isAutoSwapped
@@ -130,7 +130,7 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
        script_ITLayer.__doc__=_("Instant Translate layer commands. t 
translates selected text, shift+t translates clipboard text, a announces 
current swap configuration, s swaps source and target languages, c copies last 
result to clipboard, i identify the language of selected text.")
 
        def terminate(self):
-               
NVDASettingsDialog.categoryClasses.remove(InstantTranslateSettingsPanel)
+               
gui.settingsDialogs.NVDASettingsDialog.categoryClasses.remove(InstantTranslateSettingsPanel)
 
        def script_translateClipboardText(self, gesture):
                try:
@@ -165,8 +165,9 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
        def translate(self, text):
                self.getUpdatedGlobalVars()
                global lang_from
-               if lang_from == "auto":
-                       lang_from = detect_language(text)
+               # useful for yandex, that doesn't support auto option
+#              if lang_from == "auto":
+#                      lang_from = detect_language(text)
                translation = None
                if (text, lang_to, lang_from) in [(x[0],x[1],x[2]) for x in 
self.cachedResults]:
                        translation = filter(lambda f: f[0] == text and f[1] == 
lang_to and f[2] == lang_from, self.cachedResults)[0][3]
@@ -278,7 +279,7 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
                                        beep(500, 100)
                                        i = 0
                        myTranslator.join()
-                       language = myTranslator.lang_translated
+                       language = myTranslator.lang_detected
                        queueHandler.queueFunction(queueHandler.eventQueue, 
ui.message, g(language))
        # Translators: Presented in input help mode.
        script_identifyLanguage.__doc__ = _("It identifies the language of 
selected text")
@@ -290,7 +291,7 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
                # code from RapidSettings
                def run():
                        gui.mainFrame.prePopup()
-                       d = InstantTranslateSettingsDialog(None)
+                       d = InstantTranslateSettingsPanel(None)
                        if d is not None:
                                d.Show()
                        gui.mainFrame.postPopup()
@@ -303,7 +304,7 @@ class GlobalPlugin(globalPluginHandler.GlobalPlugin):
                "kb:a":"announceLanguages",
                "kb:c":"copyLastResult",
                "kb:i":"identifyLanguage",
-               "kb:o":"showSettings",
+#              "kb:o":"showSettings",
                "kb:h":"displayHelp",
        }
 

diff --git a/addon/globalPlugins/instantTranslate/translator.py 
b/addon/globalPlugins/instantTranslate/translator.py
index 2b097c0..3c189e2 100644
--- a/addon/globalPlugins/instantTranslate/translator.py
+++ b/addon/globalPlugins/instantTranslate/translator.py
@@ -12,6 +12,8 @@ import threading
 from time import sleep
 from random import randint
 from logHandler import log
+import ui
+import queueHandler
 
 impPath = os.path.abspath(os.path.dirname(__file__))
 sys.path.append(impPath)
@@ -54,6 +56,7 @@ class Translator(threading.Thread):
                self.lang_from = lang_from
                self.lang_swap = lang_swap
                self.translation = ''
+               self.lang_detected = ''
                self.opener = urllib2.build_opener()
                self.opener.addheaders = [('User-agent', 'Mozilla/5.0')]
                self.firstChunk = True
@@ -71,13 +74,20 @@ class Translator(threading.Thread):
                        url = urlTemplate.format(lang_from=self.lang_from, 
lang_to=self.lang_to, text=urllib2.quote(chunk.encode('utf-8')))
                        try:
                                response = json.load(self.opener.open(url))
-                               if self.firstChunk and self.lang_from == "auto" 
and response["src"] == self.lang_to and self.lang_swap is not None:
+                               temp = response[-1][-1][-1]
+                               self.lang_detected = temp if 
isinstance(temp,unicode) else unicode()
+                               if not self.lang_detected:
+                                       self.lang_detected = _("unavailable")
+#                              log.info("firstChunk=%s, lang_from=%s, 
lang_detected=%s, lang_to=%s, lang_swap=%s"%(self.firstChunk, self.lang_from, 
self.lang_detected, self.lang_to, self.lang_swap))
+                               if self.firstChunk and self.lang_from == "auto" 
and self.lang_detected == self.lang_to and self.lang_swap is not None:
                                        self.lang_to = self.lang_swap
                                        self.firstChunk = False
                                        url = 
urlTemplate.format(lang_from=self.lang_from, lang_to=self.lang_to, 
text=urllib2.quote(chunk.encode('utf-8')))
                                        response = 
json.load(self.opener.open(url))
                        except Exception as e:
-                               log.exception("Instant translate: Can not 
translate text '%s'" %chunk)
                                # We have probably been blocked, so stop trying 
to translate.
-                               raise e
+#                              log.exception("Instant translate: Can not 
translate text '%s'" %chunk)
+#                              raise e
+                               
queueHandler.queueFunction(queueHandler.eventQueue, ui.message, _("Translation 
failed"))
+                               return
                        self.translation += "".join(r[0] for r in response[0])

diff --git a/buildVars.py b/buildVars.py
index 2223aa0..cfdc37f 100644
--- a/buildVars.py
+++ b/buildVars.py
@@ -2,10 +2,8 @@
 
 # Build customizations
 # Change this file instead of sconstruct or manifest files, whenever possible.
-from datetime import datetime
 # Full getext (please don't change)
 _ = lambda x : x
-now = datetime.now()
 # Add-on information variables
 addon_info = {
        # for previously unpublished addons, please follow the community 
guidelines at:
@@ -19,7 +17,7 @@ addon_info = {
        # Translators: Long description to be shown for this add-on on add-on 
information from add-ons manager
        "addon_description" : _("This addon translates selected or clipboard 
text using the Yandex Translate service and presents it."),
        # version
-       "addon_version" : "4.3-dev",
+       "addon_version" : "4.3-20190208-dev",
        # Author(s)
        "addon_author" : "Alexy Sadovoy aka Lex <lex@xxxxxxxxxx>, ruslan 
<ru2020slan@xxxxxxxxx>, beqa <beqaprogger@xxxxxxxxx>, Mesar Hameed 
<mhameed@xxxxxxxxxxxxx>, Alberto Buffolino <a.buffolino@xxxxxxxxx>, and other 
NVDA contributors",
        # URL for the add-on documentation support
@@ -29,7 +27,7 @@ addon_info = {
        # Minimum NVDA version supported
        "addon_minimumNVDAVersion" : "2018.2.0",
        # Last NVDA version supported/tested
-       "addon_lastTestedNVDAVersion" : 
"{major}.{minor}.0".format(major=now.year, minor=(now.month-1)//3+1)
+       "addon_lastTestedNVDAVersion" : "2019.1.0"
 }
 
 import os.path

Repository URL: https://bitbucket.org/nvdaaddonteam/instanttranslate/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.

Other related posts:

  • » commit/instantTranslate: ABuffEr: Fixed language detection and auto switching, disabled old script to open settings, added message when translation fails instead of raising exception; changed version template and compatibility flag - commits-noreply