67 @return tuple of translated text (string) and flag indicating |
67 @return tuple of translated text (string) and flag indicating |
68 success (boolean) |
68 success (boolean) |
69 """ |
69 """ |
70 apiKey = self.plugin.getPreferences("GoogleV2Key") |
70 apiKey = self.plugin.getPreferences("GoogleV2Key") |
71 if not apiKey: |
71 if not apiKey: |
72 return self.tr("A valid Google Translate key is required."), False |
72 return self.tr("Google V2: A valid Google Translate key is" |
|
73 " required."), False |
73 |
74 |
74 params = "?key={3}&source={0}&target={1}&q={2}".format( |
75 params = "?key={3}&source={0}&target={1}&q={2}".format( |
75 originalLanguage, translationLanguage, text, apiKey) |
76 originalLanguage, translationLanguage, text, apiKey) |
76 url = QUrl(self.TranslatorUrl + params) |
77 url = QUrl(self.TranslatorUrl + params) |
77 response, ok = requestObject.get(url) |
78 response, ok = requestObject.get(url) |
78 if ok: |
79 if ok: |
79 response = str(response, "utf-8", "replace") |
80 response = str(response, "utf-8", "replace") |
80 try: |
81 try: |
81 responseDict = json.loads(response) |
82 responseDict = json.loads(response) |
82 except ValueError: |
83 except ValueError: |
83 return self.tr("Invalid response received"), False |
84 return self.tr("Google V2: Invalid response received"), False |
84 |
85 |
85 if ( |
86 if ( |
86 "data" not in responseDict or |
87 "data" not in responseDict or |
87 "translations" not in responseDict["data"] |
88 "translations" not in responseDict["data"] |
88 ): |
89 ): |
89 return self.tr("No translation available."), False |
90 return self.tr("Google V2: No translation available."), False |
90 |
91 |
91 result = "" |
92 result = "" |
92 translations = responseDict["data"]["translations"] |
93 translations = responseDict["data"]["translations"] |
93 for translation in translations: |
94 for translation in translations: |
94 result += translation["translatedText"] |
95 result += translation["translatedText"] |