From c3bf0f3bb0a63f0402dc8ad60d39f1df8753bf4e Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sun, 11 Dec 2022 22:51:32 +0100 Subject: [PATCH] Enable toggling translation --- toot/tui/app.py | 48 +++++++++++++++++++++++--------------------- toot/tui/entities.py | 5 +++++ toot/tui/overlays.py | 2 +- toot/tui/timeline.py | 5 +++-- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/toot/tui/app.py b/toot/tui/app.py index f11ef0a..037506e 100644 --- a/toot/tui/app.py +++ b/toot/tui/app.py @@ -317,17 +317,17 @@ class TUI(urwid.Frame): def _done(instance): if "max_toot_chars" in instance: self.max_toot_chars = instance["max_toot_chars"] + if "translation" in instance: # instance is advertising translation service self.can_translate = instance["translation"]["enabled"] - else: - if "version" in instance: - # fallback check: - # get the major version number of the server - # this works for Mastodon and Pleroma version strings - # Mastodon versions < 4 do not have translation service - # Revisit this logic if Pleroma implements translation - self.can_translate = int(instance["version"][0]) > 3 + elif "version" in instance: + # fallback check: + # get the major version number of the server + # this works for Mastodon and Pleroma version strings + # Mastodon versions < 4 do not have translation service + # Revisit this logic if Pleroma implements translation + self.can_translate = int(instance["version"][0]) > 3 return self.run_in_thread(_load_instance, done_callback=_done) @@ -509,29 +509,31 @@ class TUI(urwid.Frame): try: response = api.translate(self.app, self.user, status.id) + if response["content"]: + self.footer.set_message("Status translated") + else: + self.footer.set_error_message("Server returned empty translation") + response = None except: response = None - finally: - self.footer.clear_message() + self.footer.set_error_message("Translate server error") + self.loop.set_alarm_in(3, lambda *args: self.footer.clear_message()) return response def _done(response): if response is not None: - # Create a new Status that is translated - new_data = status.data - new_data["content"] = response["content"] - new_data["detected_source_language"] = response["detected_source_language"] - new_status = self.make_status(new_data) + status.translation = response["content"] + status.translated_from = response["detected_source_language"] + status.show_translation = True + timeline.update_status(status) - timeline.update_status(new_status) - self.footer.set_message(f"Translated status {status.id} from {response['detected_source_language']}") - else: - self.footer.set_error_message("Translate server error") - - self.loop.set_alarm_in(5, lambda *args: self.footer.clear_message()) - - self.run_in_thread(_translate, done_callback=_done ) + # If already translated, toggle showing translation + if status.translation: + status.show_translation = not status.show_translation + timeline.update_status(status) + else: + self.run_in_thread(_translate, done_callback=_done) def async_delete_status(self, timeline, status): def _delete(): diff --git a/toot/tui/entities.py b/toot/tui/entities.py index 3cf2122..5722b64 100644 --- a/toot/tui/entities.py +++ b/toot/tui/entities.py @@ -44,6 +44,11 @@ class Status: # This can be toggled by the user self.show_sensitive = False + # Set when status is translated + self.show_translation = False + self.translation = None + self.translated_from = None + # TODO: clean up self.id = self.data["id"] self.account = self._get_account() diff --git a/toot/tui/overlays.py b/toot/tui/overlays.py index 37aa73c..51ad22f 100644 --- a/toot/tui/overlays.py +++ b/toot/tui/overlays.py @@ -164,7 +164,7 @@ class Help(urwid.Padding): yield urwid.Text(h(" [B] - Boost/unboost status")) yield urwid.Text(h(" [C] - Compose new status")) yield urwid.Text(h(" [F] - Favourite/unfavourite status")) - yield urwid.Text(h(" [N] - Translate status, if possible")) + yield urwid.Text(h(" [N] - Translate status if possible (toggle)")) yield urwid.Text(h(" [R] - Reply to current status")) yield urwid.Text(h(" [S] - Show text marked as sensitive")) yield urwid.Text(h(" [T] - Show status thread (replies)")) diff --git a/toot/tui/timeline.py b/toot/tui/timeline.py index 7bab54c..63e2d5a 100644 --- a/toot/tui/timeline.py +++ b/toot/tui/timeline.py @@ -272,7 +272,8 @@ class StatusDetails(urwid.Pile): if status.data["spoiler_text"] and not status.show_sensitive: yield ("pack", urwid.Text(("content_warning", "Marked as sensitive. Press S to view."))) else: - for line in format_content(status.data["content"]): + content = status.translation if status.show_translation else status.data["content"] + for line in format_content(content): yield ("pack", urwid.Text(highlight_hashtags(line))) media = status.data["media_attachments"] @@ -299,7 +300,7 @@ class StatusDetails(urwid.Pile): yield ("pack", urwid.AttrWrap(urwid.Divider("-"), "gray")) - translated = status.data.get("detected_source_language") + translated = status.show_translation and status.translated_from yield ("pack", urwid.Text([ ("gray", "⤶ {} ".format(status.data["replies_count"])),