diff --git a/src/controller/mastodon/messages.py b/src/controller/mastodon/messages.py index d4303645..fca90832 100644 --- a/src/controller/mastodon/messages.py +++ b/src/controller/mastodon/messages.py @@ -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) diff --git a/src/wxUI/dialogs/mastodon/postDialogs.py b/src/wxUI/dialogs/mastodon/postDialogs.py index dd4695a9..646872f9 100644 --- a/src/wxUI/dialogs/mastodon/postDialogs.py +++ b/src/wxUI/dialogs/mastodon/postDialogs.py @@ -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):