change: Improved translation services' settings. Closes #632

This commit is contained in:
Manuel Cortez 2024-05-16 08:58:41 -06:00
parent ee4f254825
commit a5cd118b99
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790
6 changed files with 53 additions and 18 deletions

View File

@ -2,6 +2,9 @@ TWBlue Changelog
## changes in this version ## changes in this version
* Core:
* Added Initial Support to GoToSocial.
* The translation module has been rewritten. Now, instead of offering translations with Google Translator, the user can choose between [LibreTranslate,](https://github.com/LibreTranslate/LibreTranslate) which requires no configuration thanks to the [instance of the NVDA Spanish community;](https://translate.nvda.es) or translate using [DeepL,](https://deepl.com) for which it is necessary to create an account on DeepL and [subscribe to a DeepL API Free plan](https://support.deepl.com/hc/en-us/articles/360021200939-DeepL-API-Free) to obtain the API key which can be used to translate up to 500000 characters every month. The API key can be entered in the global options dialog, under a new tab called translation services. When translating a text, the translation engine can be changed. When changing the translation engine, the target language must be selected again before translation takes place.
* Mastodon: * Mastodon:
* Fixed an error that caused TWBlue to be unable to properly display the user action dialog from the followers or following buffer. ([#575](https://github.com/mcv-software/twblue/issues/575)) * Fixed an error that caused TWBlue to be unable to properly display the user action dialog from the followers or following buffer. ([#575](https://github.com/mcv-software/twblue/issues/575))

View File

@ -31,6 +31,7 @@ user = string(default="")
password = string(default="") password = string(default="")
[translator] [translator]
engine=string(default="deepl") engine=string(default="LibreTranslate")
translator_api_url=string(default="https://translate.nvda.es") lt_api_url=string(default="https://translate.nvda.es")
translator_api_key=string(default="") lt_api_key=string(default="")
deepl_api_key = string(default="")

View File

@ -73,7 +73,10 @@ class globalSettingsController(object):
self.dialog.set_value("proxy", "port", config.app["proxy"]["port"]) self.dialog.set_value("proxy", "port", config.app["proxy"]["port"])
self.dialog.set_value("proxy", "user", config.app["proxy"]["user"]) self.dialog.set_value("proxy", "user", config.app["proxy"]["user"])
self.dialog.set_value("proxy", "password", config.app["proxy"]["password"]) self.dialog.set_value("proxy", "password", config.app["proxy"]["password"])
self.dialog.create_translator_panel()
self.dialog.set_value("translator_panel", "libre_api_url", config.app["translator"]["lt_api_url"])
self.dialog.set_value("translator_panel", "libre_api_key", config.app["translator"]["lt_api_key"])
self.dialog.set_value("translator_panel", "deepL_api_key", config.app["translator"]["deepl_api_key"])
self.dialog.realize() self.dialog.realize()
self.response = self.dialog.get_response() self.response = self.dialog.get_response()
@ -120,4 +123,7 @@ class globalSettingsController(object):
config.app["proxy"]["port"] = self.dialog.get_value("proxy", "port") config.app["proxy"]["port"] = self.dialog.get_value("proxy", "port")
config.app["proxy"]["user"] = self.dialog.get_value("proxy", "user") config.app["proxy"]["user"] = self.dialog.get_value("proxy", "user")
config.app["proxy"]["password"] = self.dialog.get_value("proxy", "password") config.app["proxy"]["password"] = self.dialog.get_value("proxy", "password")
config.app["translator"]["lt_api_url"] = self.dialog.get_value("translator_panel", "libre_api_url")
config.app["translator"]["lt_api_key"] = self.dialog.get_value("translator_panel", "libre_api_key")
config.app["translator"]["deepl_api_key"] = self.dialog.get_value("translator_panel", "deepL_api_key")
config.app.write() config.app.write()

View File

@ -3,12 +3,12 @@ import config
from deepl import Translator from deepl import Translator
def translate(text: str, target_language: str) -> str: def translate(text: str, target_language: str) -> str:
key = config.app["translator"]["translator_api_key"] key = config.app["translator"]["deepl_api_key"]
t = Translator(key) t = Translator(key)
return t.translate_text(text, target_lang=target_language).text return t.translate_text(text, target_lang=target_language).text
def languages(): def languages():
key = config.app["translator"]["translator_api_key"] key = config.app["translator"]["deepl_api_key"]
t = Translator(key) t = Translator(key)
langs = t.get_target_languages() langs = t.get_target_languages()
return langs return langs

View File

@ -17,9 +17,9 @@ class TranslatorController(object):
self.response = False self.response = False
self.dialog = translateDialog() self.dialog = translateDialog()
pub.subscribe(self.on_engine_changed, "translator.engine_changed") pub.subscribe(self.on_engine_changed, "translator.engine_changed")
if config.app["translator"]["engine"] == "libretranslate": if config.app["translator"]["engine"] == "LibreTranslate":
self.dialog.engine_select.SetSelection(0) self.dialog.engine_select.SetSelection(0)
elif config.app["translator"]["engine"] == "deepl": elif config.app["translator"]["engine"] == "DeepL":
self.dialog.engine_select.SetSelection(1) self.dialog.engine_select.SetSelection(1)
threading.Thread(target=self.load_languages).start() threading.Thread(target=self.load_languages).start()
if self.dialog.ShowModal() == wx.ID_OK: if self.dialog.ShowModal() == wx.ID_OK:
@ -35,27 +35,24 @@ class TranslatorController(object):
self.dialog.set_languages(self.languages) self.dialog.set_languages(self.languages)
def on_engine_changed(self, engine): def on_engine_changed(self, engine):
if engine == "LibreTranslate": config.app["translator"]["engine"] = engine
config.app["translator"]["engine"] = engine.lower()
elif engine == "DeepL":
config.app["translator"]["engine"] = engine.lower()
config.app.write() config.app.write()
threading.Thread(target=self.load_languages).start() threading.Thread(target=self.load_languages).start()
def translate(self): def translate(self):
log.debug("Received translation request for language %s, text=%s" % (self.target_language, self.text)) log.debug("Received translation request for language %s, text=%s" % (self.target_language, self.text))
if config.app["translator"].get("engine") == "libretranslate": if config.app["translator"].get("engine") == "LibreTranslate":
translator = libre_translate.CustomLibreTranslateAPI(config.app["translator"]["translator_api_url"]) translator = libre_translate.CustomLibreTranslateAPI(config.app["translator"]["lt_api_url"], config.app["translator"]["lt_api_key"])
vars = dict(q=self.text, target=self.target_language) vars = dict(q=self.text, target=self.target_language)
return translator.translate(**vars) return translator.translate(**vars)
elif config.app["translator"]["engine"] == "deepl" and config.app["translator"]["translator_api_key"] != "": elif config.app["translator"]["engine"] == "DeepL" and config.app["translator"]["deepl_api_key"] != "":
return deep_l.translate(text=self.text, target_language=self.target_language) return deep_l.translate(text=self.text, target_language=self.target_language)
def get_languages(self): def get_languages(self):
languages = {} languages = {}
if config.app["translator"].get("engine") == "libretranslate": if config.app["translator"].get("engine") == "LibreTranslate":
translator = libre_translate.CustomLibreTranslateAPI(config.app["translator"]["translator_api_url"]) translator = libre_translate.CustomLibreTranslateAPI(config.app["translator"]["lt_api_url"], config.app["translator"]["lt_api_key"])
languages = {l.get("code"): l.get("name") for l in translator.languages()} languages = {l.get("code"): l.get("name") for l in translator.languages()}
elif config.app["translator"]["engine"] == "deepl" and config.app["translator"]["translator_api_key"] != "": elif config.app["translator"]["engine"] == "DeepL" and config.app["translator"]["deepl_api_key"] != "":
languages = {language.code: language.name for language in deep_l.languages()} languages = {language.code: language.name for language in deep_l.languages()}
return dict(sorted(languages.items(), key=lambda x: x[1])) return dict(sorted(languages.items(), key=lambda x: x[1]))

View File

@ -201,6 +201,30 @@ class other_buffers(wx.Panel):
buffers_list.append(self.buffers.get_text_column(i, 0)) buffers_list.append(self.buffers.get_text_column(i, 0))
return buffers_list return buffers_list
class TranslatorPanel(wx.Panel, baseDialog.BaseWXDialog):
def __init__(self, parent):
super(TranslatorPanel, self).__init__(parent)
sizer = wx.BoxSizer(wx.VERTICAL)
lbl_libre_url = wx.StaticText(self, wx.ID_ANY, _(u"LibreTranslate API URL: "))
self.libre_api_url = wx.TextCtrl(self, wx.ID_ANY)
libreUrlBox = wx.BoxSizer(wx.HORIZONTAL)
libreUrlBox.Add(lbl_libre_url, 0, wx.ALL, 5)
libreUrlBox.Add(self.libre_api_url, 1, wx.ALL | wx.EXPAND, 5)
sizer.Add(libreUrlBox, 0, wx.ALL | wx.EXPAND, 5)
lbl_libre_api_key = wx.StaticText(self, wx.ID_ANY, _(u"LibreTranslate API Key (optional): "))
self.libre_api_key = wx.TextCtrl(self, wx.ID_ANY)
libreApiKeyBox = wx.BoxSizer(wx.HORIZONTAL)
libreApiKeyBox.Add(lbl_libre_api_key, 0, wx.ALL, 5)
libreApiKeyBox.Add(self.libre_api_key, 1, wx.ALL | wx.EXPAND, 5)
sizer.Add(libreApiKeyBox, 0, wx.ALL | wx.EXPAND, 5)
lbl_deepL_api_key = wx.StaticText(self, wx.ID_ANY, _(u"DeepL API Key: "))
self.deepL_api_key = wx.TextCtrl(self, wx.ID_ANY)
deepLApiKeyBox = wx.BoxSizer(wx.HORIZONTAL)
deepLApiKeyBox.Add(lbl_deepL_api_key, 0, wx.ALL, 5)
deepLApiKeyBox.Add(self.deepL_api_key, 1, wx.ALL | wx.EXPAND, 5)
sizer.Add(deepLApiKeyBox, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(sizer)
class configurationDialog(baseDialog.BaseWXDialog): class configurationDialog(baseDialog.BaseWXDialog):
def set_title(self, title): def set_title(self, title):
self.SetTitle(title) self.SetTitle(title)
@ -221,6 +245,10 @@ class configurationDialog(baseDialog.BaseWXDialog):
self.proxy = proxy(self.notebook, proxyTypes) self.proxy = proxy(self.notebook, proxyTypes)
self.notebook.AddPage(self.proxy, _(u"Proxy")) self.notebook.AddPage(self.proxy, _(u"Proxy"))
def create_translator_panel(self):
self.translator_panel= TranslatorPanel(self.notebook)
self.notebook.AddPage(self.translator_panel, _("Translation services"))
def realize(self): def realize(self):
self.sizer.Add(self.notebook, 0, wx.ALL, 5) self.sizer.Add(self.notebook, 0, wx.ALL, 5)
ok_cancel_box = wx.BoxSizer(wx.HORIZONTAL) ok_cancel_box = wx.BoxSizer(wx.HORIZONTAL)