(self, *_args)
| 1129 | @Gtk.Template.Callback() |
| 1130 | @background_task |
| 1131 | async def _on_translation(self, *_args): |
| 1132 | if not self.provider["trans"] or self._appeared_before(): |
| 1133 | # If it's like the last translation then it's useless to continue |
| 1134 | return |
| 1135 | |
| 1136 | # Run translation |
| 1137 | if self.next_translation: |
| 1138 | request = self.next_translation |
| 1139 | self.next_translation = None |
| 1140 | else: |
| 1141 | text = self.src_buffer.get_text(self.src_buffer.get_start_iter(), self.src_buffer.get_end_iter(), True) |
| 1142 | request = TranslationRequest(text, self.src_lang_selector.selected, self.dest_lang_selector.selected) |
| 1143 | |
| 1144 | if self.translation_loading: |
| 1145 | self.next_translation = request |
| 1146 | return |
| 1147 | |
| 1148 | # Show feedback for start of translation. |
| 1149 | self.trans_spinner.show() |
| 1150 | self.dest_box.props.sensitive = False |
| 1151 | self.langs_button_box.props.sensitive = False |
| 1152 | |
| 1153 | # If the two languages are the same, nothing is done |
| 1154 | if request.src != request.dest and request.text != "": |
| 1155 | self.translation_loading = True |
| 1156 | |
| 1157 | try: |
| 1158 | translation = await self.provider["trans"].translate(request) |
| 1159 | |
| 1160 | if translation.detected and self.src_lang_selector.selected == "auto": |
| 1161 | if Settings.get().src_auto: |
| 1162 | self.src_lang_selector.set_insight( |
| 1163 | self.provider["trans"].normalize_lang_code(translation.detected) |
| 1164 | ) |
| 1165 | else: |
| 1166 | self.src_lang_selector.selected = translation.detected |
| 1167 | |
| 1168 | self.dest_buffer.props.text = translation.text |
| 1169 | |
| 1170 | # Finally, translation is saved in history |
| 1171 | self.add_history_entry(translation) |
| 1172 | |
| 1173 | self._check_mistakes() |
| 1174 | self._check_pronunciation() |
| 1175 | |
| 1176 | # Translation failed |
| 1177 | except (RequestError, ProviderError) as exc: |
| 1178 | self.trans_warning.props.visible = True |
| 1179 | self.lookup_action("copy").props.enabled = False # type: ignore |
| 1180 | self.lookup_action("listen-src").props.enabled = False # type: ignore |
| 1181 | self.lookup_action("listen-dest").props.enabled = False # type: ignore |
| 1182 | |
| 1183 | if isinstance(exc, RequestError): |
| 1184 | self.send_notification( |
| 1185 | _("Translation failed, check for network issues"), |
| 1186 | action={ |
| 1187 | "label": _("Retry"), |
| 1188 | "name": "win.translation", |
no test coverage detected