Enable toggling translation
This commit is contained in:
parent
be5948bac8
commit
c3bf0f3bb0
4 changed files with 34 additions and 26 deletions
|
@ -317,17 +317,17 @@ class TUI(urwid.Frame):
|
||||||
def _done(instance):
|
def _done(instance):
|
||||||
if "max_toot_chars" in instance:
|
if "max_toot_chars" in instance:
|
||||||
self.max_toot_chars = instance["max_toot_chars"]
|
self.max_toot_chars = instance["max_toot_chars"]
|
||||||
|
|
||||||
if "translation" in instance:
|
if "translation" in instance:
|
||||||
# instance is advertising translation service
|
# instance is advertising translation service
|
||||||
self.can_translate = instance["translation"]["enabled"]
|
self.can_translate = instance["translation"]["enabled"]
|
||||||
else:
|
elif "version" in instance:
|
||||||
if "version" in instance:
|
# fallback check:
|
||||||
# fallback check:
|
# get the major version number of the server
|
||||||
# get the major version number of the server
|
# this works for Mastodon and Pleroma version strings
|
||||||
# this works for Mastodon and Pleroma version strings
|
# Mastodon versions < 4 do not have translation service
|
||||||
# Mastodon versions < 4 do not have translation service
|
# Revisit this logic if Pleroma implements translation
|
||||||
# Revisit this logic if Pleroma implements translation
|
self.can_translate = int(instance["version"][0]) > 3
|
||||||
self.can_translate = int(instance["version"][0]) > 3
|
|
||||||
|
|
||||||
return self.run_in_thread(_load_instance, done_callback=_done)
|
return self.run_in_thread(_load_instance, done_callback=_done)
|
||||||
|
|
||||||
|
@ -509,29 +509,31 @@ class TUI(urwid.Frame):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = api.translate(self.app, self.user, status.id)
|
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:
|
except:
|
||||||
response = None
|
response = None
|
||||||
finally:
|
self.footer.set_error_message("Translate server error")
|
||||||
self.footer.clear_message()
|
|
||||||
|
|
||||||
|
self.loop.set_alarm_in(3, lambda *args: self.footer.clear_message())
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def _done(response):
|
def _done(response):
|
||||||
if response is not None:
|
if response is not None:
|
||||||
# Create a new Status that is translated
|
status.translation = response["content"]
|
||||||
new_data = status.data
|
status.translated_from = response["detected_source_language"]
|
||||||
new_data["content"] = response["content"]
|
status.show_translation = True
|
||||||
new_data["detected_source_language"] = response["detected_source_language"]
|
timeline.update_status(status)
|
||||||
new_status = self.make_status(new_data)
|
|
||||||
|
|
||||||
timeline.update_status(new_status)
|
# If already translated, toggle showing translation
|
||||||
self.footer.set_message(f"Translated status {status.id} from {response['detected_source_language']}")
|
if status.translation:
|
||||||
else:
|
status.show_translation = not status.show_translation
|
||||||
self.footer.set_error_message("Translate server error")
|
timeline.update_status(status)
|
||||||
|
else:
|
||||||
self.loop.set_alarm_in(5, lambda *args: self.footer.clear_message())
|
self.run_in_thread(_translate, done_callback=_done)
|
||||||
|
|
||||||
self.run_in_thread(_translate, done_callback=_done )
|
|
||||||
|
|
||||||
def async_delete_status(self, timeline, status):
|
def async_delete_status(self, timeline, status):
|
||||||
def _delete():
|
def _delete():
|
||||||
|
|
|
@ -44,6 +44,11 @@ class Status:
|
||||||
# This can be toggled by the user
|
# This can be toggled by the user
|
||||||
self.show_sensitive = False
|
self.show_sensitive = False
|
||||||
|
|
||||||
|
# Set when status is translated
|
||||||
|
self.show_translation = False
|
||||||
|
self.translation = None
|
||||||
|
self.translated_from = None
|
||||||
|
|
||||||
# TODO: clean up
|
# TODO: clean up
|
||||||
self.id = self.data["id"]
|
self.id = self.data["id"]
|
||||||
self.account = self._get_account()
|
self.account = self._get_account()
|
||||||
|
|
|
@ -164,7 +164,7 @@ class Help(urwid.Padding):
|
||||||
yield urwid.Text(h(" [B] - Boost/unboost status"))
|
yield urwid.Text(h(" [B] - Boost/unboost status"))
|
||||||
yield urwid.Text(h(" [C] - Compose new status"))
|
yield urwid.Text(h(" [C] - Compose new status"))
|
||||||
yield urwid.Text(h(" [F] - Favourite/unfavourite 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(" [R] - Reply to current status"))
|
||||||
yield urwid.Text(h(" [S] - Show text marked as sensitive"))
|
yield urwid.Text(h(" [S] - Show text marked as sensitive"))
|
||||||
yield urwid.Text(h(" [T] - Show status thread (replies)"))
|
yield urwid.Text(h(" [T] - Show status thread (replies)"))
|
||||||
|
|
|
@ -272,7 +272,8 @@ class StatusDetails(urwid.Pile):
|
||||||
if status.data["spoiler_text"] and not status.show_sensitive:
|
if status.data["spoiler_text"] and not status.show_sensitive:
|
||||||
yield ("pack", urwid.Text(("content_warning", "Marked as sensitive. Press S to view.")))
|
yield ("pack", urwid.Text(("content_warning", "Marked as sensitive. Press S to view.")))
|
||||||
else:
|
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)))
|
yield ("pack", urwid.Text(highlight_hashtags(line)))
|
||||||
|
|
||||||
media = status.data["media_attachments"]
|
media = status.data["media_attachments"]
|
||||||
|
@ -299,7 +300,7 @@ class StatusDetails(urwid.Pile):
|
||||||
|
|
||||||
yield ("pack", urwid.AttrWrap(urwid.Divider("-"), "gray"))
|
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([
|
yield ("pack", urwid.Text([
|
||||||
("gray", "⤶ {} ".format(status.data["replies_count"])),
|
("gray", "⤶ {} ".format(status.data["replies_count"])),
|
||||||
|
|
Loading…
Reference in a new issue