Enable toggling translation

This commit is contained in:
Ivan Habunek 2022-12-11 22:51:32 +01:00
parent be5948bac8
commit c3bf0f3bb0
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
4 changed files with 34 additions and 26 deletions

View File

@ -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():

View File

@ -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()

View File

@ -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)"))

View File

@ -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"])),