mastodon: Adds language selector on post dialog. Language for posts will be set via account's default language, post language or via TWBlue's user language

This commit is contained in:
Manuel Cortez 2024-12-31 18:48:27 -06:00
parent 8ed80da82c
commit 0322939cba
2 changed files with 36 additions and 4 deletions

View File

@ -5,6 +5,7 @@ import wx
import widgetUtils
import config
import output
import languageHandler
from twitter_text import parse_tweet, config
from mastodon import MastodonError
from controller import messages
@ -32,9 +33,12 @@ class post(messages.basicMessage):
self.max = session.char_limit
self.title = title
self.session = session
self.message = postDialogs.Post(caption=caption, text=text, *args, **kwargs)
langs = self.session.supported_languages
display_langs = [l.name for l in langs]
self.message = postDialogs.Post(caption=caption, text=text, languages=display_langs, *args, **kwargs)
self.message.SetTitle(title)
self.message.text.SetInsertionPoint(len(self.message.text.GetValue()))
self.set_language(self.session.default_language)
widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
widgetUtils.connect_event(self.message.text, widgetUtils.ENTERED_TEXT, self.text_processor)
widgetUtils.connect_event(self.message.spoiler, widgetUtils.ENTERED_TEXT, self.text_processor)
@ -70,7 +74,21 @@ class post(messages.basicMessage):
self.add_post(event=None, update_gui=False)
return self.thread
def set_post_data(self, visibility, data):
def set_language(self, language=None):
""" Attempt to set the default language for a post. """
# language can be provided in a post (replying or recovering from errors).
# Also it can be provided in user preferences (retrieved in the session).
# If no language is provided, let's fallback to TWBlue's user language.
if language != None:
language_code = language
else:
# Let's cut langcode_VARIANT to ISO-639 two letter code only.
language_code = languageHandler.curLang[:2]
for lang in self.session.supported_languages:
if lang.code == language_code:
self.message.language.SetStringSelection(lang.name)
def set_post_data(self, visibility, data, language):
if len(data) == 0:
return
if len(data) > 1:
@ -87,6 +105,7 @@ class post(messages.basicMessage):
self.message.on_sensitivity_changed()
for attachment in self.attachments:
self.message.add_item(item=[attachment["file"], attachment["type"], attachment["description"]])
self.set_language(language)
self.text_processor()
def text_processor(self, *args, **kwargs):
@ -231,6 +250,14 @@ class post(messages.basicMessage):
visibility_settings = ["public", "unlisted", "private", "direct"]
return visibility_settings[self.message.visibility.GetSelection()]
def get_language(self):
langs = self.session.supported_languages
lang = self.message.language.GetSelection()
if lang >= 0:
print(langs[lang].code)
return langs[lang].code
return None
def set_visibility(self, setting):
visibility_settings = ["public", "unlisted", "private", "direct"]
visibility_setting = visibility_settings.index(setting)

View File

@ -2,7 +2,7 @@
import wx
class Post(wx.Dialog):
def __init__(self, caption=_("Post"), text="", *args, **kwds):
def __init__(self, caption=_("Post"), text="", languages=[], *args, **kwds):
super(Post, self).__init__(parent=None, id=wx.ID_ANY, *args, **kwds)
main_sizer = wx.BoxSizer(wx.VERTICAL)
post_sizer = wx.WrapSizer(wx.VERTICAL)
@ -49,6 +49,12 @@ class Post(wx.Dialog):
self.visibility = wx.ComboBox(self, wx.ID_ANY, choices=[_("Public"), _("Not listed"), _("Followers only"), _("Direct")], style=wx.CB_DROPDOWN | wx.CB_READONLY | wx.CB_SIMPLE)
self.visibility.SetSelection(0)
visibility_sizer.Add(self.visibility, 0, 0, 0)
language_sizer = wx.BoxSizer(wx.HORIZONTAL)
post_actions_sizer.Add(language_sizer, 0, wx.RIGHT, 20)
lang_label = wx.StaticText(self, wx.ID_ANY, _("Language"))
language_sizer.Add(lang_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 5)
self.language = wx.ComboBox(self, wx.ID_ANY, choices=languages, style=wx.CB_DROPDOWN | wx.CB_READONLY)
language_sizer.Add(self.language, 0, wx.ALIGN_CENTER_VERTICAL, 0)
self.add = wx.Button(self, wx.ID_ANY, _("A&dd"))
self.sensitive = wx.CheckBox(self, wx.ID_ANY, _("S&ensitive content"))
self.sensitive.SetValue(False)
@ -165,7 +171,6 @@ class Post(wx.Dialog):
def unable_to_attach_poll(self, *args, **kwargs):
return wx.MessageDialog(self, _("You can add a poll or media files. In order to add your poll, please remove other attachments first."), _("Error adding poll"), wx.ICON_ERROR).ShowModal()
import wx
class viewPost(wx.Dialog):
def set_title(self, length):