Commits

Alexandre Macabies committed a725aa1

Fixing translation for large texts

Comments (0)

Files changed (3)

 
 LANGUAGES_FROM = (('af', 'afrikaans'), ('sq', 'albanais'), ('de', 'allemand'), ('en', 'anglais'), ('ar', 'arabe'), ('hy', 'arménien'), ('az', 'azéri'), ('eu', 'basque'), ('bn', 'bengali'), ('be', 'biélorusse'), ('bg', 'bulgare'), ('ca', 'catalan'), ('zh-CN', 'chinois'), ('ko', 'coréen'), ('ht', 'créole haïtien'), ('hr', 'croate'), ('da', 'danois'), ('es', 'espagnol'), ('eo', 'espéranto'), ('et', 'estonien'), ('fi', 'finnois'), ('fr', 'français'), ('gl', 'galicien'), ('cy', 'gallois'), ('ka', 'géorgien'), ('el', 'grec'), ('gu', 'gujarati'), ('iw', 'hébreu'), ('hi', 'hindi'), ('hu', 'hongrois'), ('id', 'indonésien'), ('ga', 'irlandais'), ('is', 'islandais'), ('it', 'italien'), ('ja', 'japonais'), ('kn', 'kannada'), ('la', 'latin'), ('lv', 'letton'), ('lt', 'lituanien'), ('mk', 'macédonien'), ('ms', 'malaisien'), ('mt', 'maltais'), ('nl', 'néerlandais'), ('no', 'norvégien'), ('fa', 'persan'), ('pl', 'polonais'), ('pt', 'portugais'), ('ro', 'roumain'), ('ru', 'russe'), ('sr', 'serbe'), ('sk', 'slovaque'), ('sl', 'slovène'), ('sv', 'suédois'), ('sw', 'swahili'), ('tl', 'tagalog'), ('ta', 'tamul'), ('cs', 'tchèque'), ('te', 'telugu'), ('th', 'thaï'), ('tr', 'turc'), ('uk', 'ukrainien'), ('ur', 'urdu'), ('vi', 'vietnamien'), ('yi', 'yiddish'))
 LANGUAGES_TO = (('af', 'afrikaans'), ('sq', 'albanais'), ('de', 'allemand'), ('en', 'anglais'), ('ar', 'arabe'), ('hy', 'arménien'), ('az', 'azéri'), ('eu', 'basque'), ('bn', 'bengali'), ('be', 'biélorusse'), ('bg', 'bulgare'), ('ca', 'catalan'), ('zh-CN', 'chinois (simplifié)'), ('zh-TW', 'chinois (traditionnel)'), ('ko', 'coréen'), ('ht', 'créole haïtien'), ('hr', 'croate'), ('da', 'danois'), ('es', 'espagnol'), ('eo', 'espéranto'), ('et', 'estonien'), ('fi', 'finnois'), ('fr', 'français'), ('gl', 'galicien'), ('cy', 'gallois'), ('ka', 'géorgien'), ('el', 'grec'), ('gu', 'gujarati'), ('iw', 'hébreu'), ('hi', 'hindi'), ('hu', 'hongrois'), ('id', 'indonésien'), ('ga', 'irlandais'), ('is', 'islandais'), ('it', 'italien'), ('ja', 'japonais'), ('kn', 'kannada'), ('la', 'latin'), ('lv', 'letton'), ('lt', 'lituanien'), ('mk', 'macédonien'), ('ms', 'malaisien'), ('mt', 'maltais'), ('nl', 'néerlandais'), ('no', 'norvégien'), ('fa', 'persan'), ('pl', 'polonais'), ('pt', 'portugais'), ('ro', 'roumain'), ('ru', 'russe'), ('sr', 'serbe'), ('sk', 'slovaque'), ('sl', 'slovène'), ('sv', 'suédois'), ('sw', 'swahili'), ('tl', 'tagalog'), ('ta', 'tamul'), ('cs', 'tchèque'), ('te', 'telugu'), ('th', 'thaï'), ('tr', 'turc'), ('uk', 'ukrainien'), ('ur', 'urdu'), ('vi', 'vietnamien'), ('yi', 'yiddish'))
+
+MAX_LENGTH_FOR_GET = 1300
 from const import LANGUAGES_FROM, LANGUAGES_TO
 from widgets import LoadingSpinner, TicksSpinner
 from translate import TranslateService
+from operator import itemgetter
 import logging
 import resrc
 
     def translationResult(self, data, reply, _):
         log.debug('Translation arrived')
         translated = ""
-        was_n_before = True
-        for piece in data[4]:
-            translated += (' ' if piece[2] == 1 and not was_n_before else '') + piece[0]
-            was_n_before = piece[0].endswith('\n')
+
+        if data[4] is None: # from POST... no other suggestions
+            translated = ''.join(map(itemgetter(0), data[0]))
+        else:
+            was_n_before = True
+            for piece in data[4]:
+                translated += (' ' if piece[2] == 1 and not was_n_before else '') + piece[0]
+                was_n_before = piece[0].endswith('\n')
 
         self.ui.t_text.setPlainText(translated.strip())
         self.load_spinner.fadeOut()
 import time
 import random
 from utils import parse_eval
+from const import MAX_LENGTH_FOR_GET
 
 import logging
 log = logging.getLogger('copytrans')
         request.setRawHeader("User-Agent", USER_AGENT)
         request.setAttribute(QtNetwork.QNetworkRequest.CacheLoadControlAttribute, QtNetwork.QNetworkRequest.PreferCache)
         if data is not None:
-            request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json" if is_json else "application/x-www-form-urlencoded")
+#            we shall have a posting_json param cause query can be www-form and response can be json
+#            request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/json" if is_json else "application/x-www-form-urlencoded")
+            request.setHeader(QtNetwork.QNetworkRequest.ContentTypeHeader, "application/x-www-form-urlencoded")
         send = self._request_methods[method]
         reply = send(request, data) if data is not None else send(request)
         key = (host, port)
         return leftUrl.port(80) == rightUrl.port(80) and \
             leftUrl.toString(QUrl.FormattingOption(QUrl.RemovePort)) == rightUrl.toString(QUrl.FormattingOption(QUrl.RemovePort))
 
+    @staticmethod
+    def encode_data(datadict):
+        out = []
+        for key, value in datadict.items():
+            value = str(QUrl.toPercentEncoding(value))
+            out.append('%s=%s' % (str(key), value))
+        return '&'.join(out)
+
     def _process_reply(self, reply):
         try:
             request, handler, is_json = self._active_requests.pop(reply)
             elif is_json:
                 data = str(reply.readAll())
                 try:
-                    handler(json.loads(data), reply, error)
+                    parsed = json.loads(data)
                 except ValueError:
-                    handler(parse_eval(data), reply, error)
+                    parsed = parse_eval(data)
+                print(parsed)
+                handler(parsed, reply, error)
             else:
                 handler(reply.readAll(), reply, error)
         reply.close()
 
     # real methods
     def get_translation(self, lng_from, lng_to, original, callback):
-        qs = QUrl('/translate_a/t')
-        qs.setQueryItems(list({
+        data = {
             'client': 't',
             'hl': 'fr',
             'multires': '1',
-            'otf': '2' if random.random() < 0.91 else '1', # automatic translation (key down...)
-            'ssel': '6',
-            'tsel': '3',
-            'sc': '1',
+            'ssel': '0',
+            'tsel': '0',
+#            'sc': '1',
             'sl': lng_from,
             'tl': lng_to,
             'text': original,
-        }.items()))
+        }
+        qs = QUrl('/translate_a/t')
         self.csi_translation(lng_from, lng_to, len(original))
-        return self.get('translate.google.fr', 80, qs.toString(), is_json=True, priority=True, important=True, handler=callback)
+        if len(original) <= MAX_LENGTH_FOR_GET:
+            data['otf'] = '1' if random.random() < 0.91 else '2' # automatic translation (key down...)
+            qs.setQueryItems(list(data.items()))
+            return self.get('translate.google.fr', 80, qs.toString(), is_json=True, priority=True, important=True, handler=callback)
+        else:
+            data = TranslateService.encode_data(data)
+            return self.post('translate.google.fr', 80, qs.toString(), data, is_json=True, priority=True, important=True, handler=callback)
 
     def csi_load(self):
         return self.get(**build_csi({
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.