81 success (boolean) |
81 success (boolean) |
82 """ |
82 """ |
83 params = QByteArray( |
83 params = QByteArray( |
84 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format( |
84 "client=gtx&sl={0}&tl={1}&dt=t&dt=bd&ie=utf-8&oe=utf-8&q=".format( |
85 originalLanguage, translationLanguage).encode("utf-8")) |
85 originalLanguage, translationLanguage).encode("utf-8")) |
86 encodedText = QByteArray(Utilities.html_encode(text).encode("utf-8"))\ |
86 encodedText = ( |
|
87 QByteArray(Utilities.html_encode(text).encode("utf-8")) |
87 .toPercentEncoding() |
88 .toPercentEncoding() |
|
89 ) |
88 request = params + encodedText |
90 request = params + encodedText |
89 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
91 response, ok = requestObject.post(QUrl(self.TranslatorUrl), request) |
90 if ok: |
92 if ok: |
91 try: |
93 try: |
92 # clean up the response |
94 # clean up the response |
99 sentences = responseDict["sentences"] |
101 sentences = responseDict["sentences"] |
100 result = "" |
102 result = "" |
101 for sentence in sentences: |
103 for sentence in sentences: |
102 result += sentence["trans"].replace("\n", "<br/>") |
104 result += sentence["trans"].replace("\n", "<br/>") |
103 |
105 |
104 if self.plugin.getPreferences("GoogleEnableDictionary") and \ |
106 if ( |
105 "dict" in responseDict: |
107 self.plugin.getPreferences("GoogleEnableDictionary") and |
|
108 "dict" in responseDict |
|
109 ): |
106 dictionary = responseDict["dict"] |
110 dictionary = responseDict["dict"] |
107 for value in dictionary: |
111 for value in dictionary: |
108 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format( |
112 result += "<hr/><u><b>{0}</b> - {1}</u><br/>".format( |
109 text, value["pos"]) |
113 text, value["pos"]) |
110 for entry in value["entry"]: |
114 for entry in value["entry"]: |
118 previous, word, ", ".join(reverse)) |
122 previous, word, ", ".join(reverse)) |
119 if value != dictionary[-1]: |
123 if value != dictionary[-1]: |
120 result += "<br/>" |
124 result += "<br/>" |
121 elif isinstance(responseDict, list): |
125 elif isinstance(responseDict, list): |
122 sentences = responseDict[0] |
126 sentences = responseDict[0] |
123 result = "".join([s[0] for s in sentences])\ |
127 result = ( |
124 .replace("\n", "<br/>") |
128 "".join([s[0] for s in sentences]).replace("\n", "<br/>") |
125 if self.plugin.getPreferences("GoogleEnableDictionary") and \ |
129 ) |
126 len(responseDict) > 2: |
130 if ( |
|
131 self.plugin.getPreferences("GoogleEnableDictionary") and |
|
132 len(responseDict) > 2 |
|
133 ): |
127 if not responseDict[1]: |
134 if not responseDict[1]: |
128 result = self.tr("No translation found.") |
135 result = self.tr("No translation found.") |
129 ok = False |
136 ok = False |
130 else: |
137 else: |
131 for wordTypeList in responseDict[1]: |
138 for wordTypeList in responseDict[1]: |