Skip to content

Commit

Permalink
Enable toggling translation
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Dec 11, 2022
1 parent be5948b commit c3bf0f3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
50 changes: 26 additions & 24 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,17 @@ def _load_instance():
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)

Expand Down Expand Up @@ -509,29 +509,31 @@ def _translate():

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)

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 )
status.translation = response["content"]
status.translated_from = response["detected_source_language"]
status.show_translation = True
timeline.update_status(status)

# 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():
Expand Down
5 changes: 5 additions & 0 deletions toot/tui/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def __init__(self, data, is_mine, default_instance):
# 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()
Expand Down
2 changes: 1 addition & 1 deletion toot/tui/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def link(text, url):
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)"))
Expand Down
5 changes: 3 additions & 2 deletions toot/tui/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def content_generator(self, status, reblogged_by):
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"]
Expand All @@ -299,7 +300,7 @@ def content_generator(self, status, reblogged_by):

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

0 comments on commit c3bf0f3

Please sign in to comment.