93 try: |
93 try: |
94 # clean up the response |
94 # clean up the response |
95 response = re.sub(r',{2,}', ',', response) |
95 response = re.sub(r',{2,}', ',', response) |
96 responseDict = json.loads(response) |
96 responseDict = json.loads(response) |
97 except ValueError: |
97 except ValueError: |
98 return self.tr("Invalid response received"), False |
98 return self.tr("Google V1: Invalid response received"), False |
99 |
99 |
100 if isinstance(responseDict, dict): |
100 if isinstance(responseDict, dict): |
101 sentences = responseDict["sentences"] |
101 sentences = responseDict["sentences"] |
102 result = "" |
102 result = "" |
103 for sentence in sentences: |
103 for sentence in sentences: |
130 if ( |
130 if ( |
131 self.plugin.getPreferences("GoogleEnableDictionary") and |
131 self.plugin.getPreferences("GoogleEnableDictionary") and |
132 len(responseDict) > 2 |
132 len(responseDict) > 2 |
133 ): |
133 ): |
134 if not responseDict[1]: |
134 if not responseDict[1]: |
135 result = self.tr("No translation found.") |
135 result = self.tr("Google V1: No translation found.") |
136 ok = False |
136 ok = False |
137 else: |
137 else: |
138 for wordTypeList in responseDict[1]: |
138 for wordTypeList in responseDict[1]: |
139 result += "<hr/><u><b>{0}</b> - {1}</u>".format( |
139 result += "<hr/><u><b>{0}</b> - {1}</u>".format( |
140 wordTypeList[0], wordTypeList[-2]) |
140 wordTypeList[0], wordTypeList[-2]) |
160 @return tuple with pronounce data (QByteArray) or error string (string) |
160 @return tuple with pronounce data (QByteArray) or error string (string) |
161 and success flag (boolean) |
161 and success flag (boolean) |
162 """ |
162 """ |
163 text = text.split("\n\n", 1)[0] |
163 text = text.split("\n\n", 1)[0] |
164 if len(text) > self.TextToSpeechLimit: |
164 if len(text) > self.TextToSpeechLimit: |
165 return (self.tr("Only texts up to {0} characters are allowed.") |
165 return (self.tr("Google V1: Only texts up to {0} characters are" |
|
166 " allowed.") |
166 .format(self.TextToSpeechLimit), False) |
167 .format(self.TextToSpeechLimit), False) |
167 |
168 |
168 url = QUrl(self.TextToSpeechUrl + |
169 url = QUrl(self.TextToSpeechUrl + |
169 "?client=tw-ob&ie=utf-8&tl={0}&q={1}".format( |
170 "?client=tw-ob&ie=utf-8&tl={0}&q={1}".format( |
170 language, text)) |
171 language, text)) |