[gedit-plugins] Allow to change server address in gedit's translation plugin using gsettings
- From: Jordi Mas <jmas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit-plugins] Allow to change server address in gedit's translation plugin using gsettings
- Date: Sun, 18 Oct 2020 18:28:20 +0000 (UTC)
commit a63b023b43dd224be2afe80ab585154c3079e417
Author: Jordi Mas <jmas softcatala org>
Date: Sun Oct 18 20:28:15 2020 +0200
Allow to change server address in gedit's translation plugin using gsettings
.../org.gnome.gedit.plugins.translate.gschema.xml | 7 +++++
plugins/translate/translate/__init__.py | 33 ++++++++++++----------
plugins/translate/translate/services/apertium.py | 15 ++++++----
plugins/translate/translate/services/service.py | 3 ++
plugins/translate/translate/settings.py | 3 ++
5 files changed, 40 insertions(+), 21 deletions(-)
---
diff --git a/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml
b/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml
index 3de4824..d10c11f 100644
--- a/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml
+++ b/plugins/translate/org.gnome.gedit.plugins.translate.gschema.xml
@@ -19,6 +19,13 @@
Language pair used to translate from one language to another
</description>
</key>
+ <key name="apertium-server" type="s">
+ <default>'https://www.apertium.org/apy'</default>
+ <summary>Apertium server end point</summary>
+ <description>
+ Apertium server end point
+ </description>
+ </key>
<key type="s" name="api-key">
<default>''</default>
<summary>API key for remote web service</summary>
diff --git a/plugins/translate/translate/__init__.py b/plugins/translate/translate/__init__.py
index e7fe0ef..18e6f29 100644
--- a/plugins/translate/translate/__init__.py
+++ b/plugins/translate/translate/__init__.py
@@ -37,6 +37,22 @@ try:
except:
_ = lambda s: s
+def _get_translation_service_shared(service_id):
+ settings = Settings()
+ service = Services.get(service_id)
+ if service.has_api_key() is True:
+ key = settings.get_apikey()
+ service.set_api_key(key)
+
+ if service_id == Services.APERTIUM_ID:
+ server = settings.get_apertium_server()
+ service.set_server(server)
+
+ service.init()
+ return service
+
+
+
class TranslateAppActivatable(GObject.Object, Gedit.AppActivatable):
app = GObject.Property(type=Gedit.App)
@@ -93,14 +109,7 @@ class TranslateWindowActivatable(GObject.Object, Gedit.WindowActivatable, PeasGt
return Services.get_name(service_id)
def _get_translation_service(self, service_id):
- settings = Settings()
- service = Services.get(service_id)
- if service.has_api_key() is True:
- key = settings.get_apikey()
- service.set_api_key(key)
-
- service.init()
- return service
+ return _get_translation_service_shared(service_id)
def get_languages_names_codes(self, service_id):
service = self._get_translation_service(service_id)
@@ -180,13 +189,7 @@ class TranslateViewActivatable(GObject.Object, Gedit.ViewActivatable):
def _get_translation_service(self):
service_id = self._settings.get_service()
- service = Services.get(service_id)
- if service.has_api_key() is True:
- key = self._settings.get_apikey()
- service.set_api_key(key)
-
- service.init()
- return service
+ return _get_translation_service_shared(service_id)
def translate_text(self, document, start, end):
doc = self.view.get_buffer()
diff --git a/plugins/translate/translate/services/apertium.py
b/plugins/translate/translate/services/apertium.py
index 48d783e..1966967 100644
--- a/plugins/translate/translate/services/apertium.py
+++ b/plugins/translate/translate/services/apertium.py
@@ -39,7 +39,6 @@ class Apertium(Service):
"eng|cat",
]
- SERVER = "https://www.apertium.org/apy"
@staticmethod
def _clean_for_ut():
@@ -52,16 +51,19 @@ class Apertium(Service):
def has_api_key(self):
return False
-
+
def set_api_key(self, key):
pass
+ def set_server(self, server):
+ self.server = server
+
def get_api_hint(self):
pass
def init(self):
self._fetch_remote_language_names_and_pairs()
-
+
def get_language_names(self):
if len(Apertium.g_language_codes) > 0 and len(Apertium.g_language_names) > 0:
return Apertium.g_language_names
@@ -125,6 +127,7 @@ class Apertium(Service):
def _fetch_remote_language_names_and_pairs(self):
+
if len(Apertium.g_language_names) > 0:
return
@@ -158,7 +161,7 @@ class Apertium(Service):
def _get_remote_language_pairs(self):
- url = "{0}/listPairs".format(self.SERVER)
+ url = "{0}/listPairs".format(self.server)
response = urllib.request.urlopen(url)
data = json.loads(response.read().decode("utf-8"))
@@ -186,14 +189,14 @@ class Apertium(Service):
for locale_code in locales:
locales_string += locale_code + '+'
- url = "{0}/listLanguageNames?locale={1}&languages={2}".format(self.SERVER,
+ url = "{0}/listLanguageNames?locale={1}&languages={2}".format(self.server,
user_locale, locales_string)
response = urllib.request.urlopen(url)
return json.loads(response.read().decode("utf-8"))
def translate_text(self, text, language_pair):
- url = "{0}/translate?langpair={1}&markUnknown=no".format(self.SERVER, language_pair)
+ url = "{0}/translate?langpair={1}&markUnknown=no".format(self.server, language_pair)
url += "&q=" + urllib.parse.quote_plus(text.encode('utf-8'))
response = urllib.request.urlopen(url)
diff --git a/plugins/translate/translate/services/service.py b/plugins/translate/translate/services/service.py
index 5406c6e..60516fc 100644
--- a/plugins/translate/translate/services/service.py
+++ b/plugins/translate/translate/services/service.py
@@ -53,3 +53,6 @@ class Service(metaclass=ABCMeta):
def set_api_key(self, key):
pass
+ @abstractmethod
+ def set_server(self, server):
+ pass
diff --git a/plugins/translate/translate/settings.py b/plugins/translate/translate/settings.py
index e3f7cc2..dadcb91 100644
--- a/plugins/translate/translate/settings.py
+++ b/plugins/translate/translate/settings.py
@@ -27,6 +27,7 @@ class Settings():
LANGUAGE_PAIR = 'language-pair'
SERVICE = 'service'
API_KEY = 'api-key'
+ APERTIUM_SERVER_KEY = 'apertium-server'
def __init__(self):
self._settings = Gio.Settings.new(self.TRANSLATE_KEY_BASE)
@@ -55,3 +56,5 @@ class Settings():
def set_output_document(self, document):
self._settings.set_boolean(self.OUTPUT_TO_DOCUMENT, document)
+ def get_apertium_server(self):
+ return self._settings.get_string(self.APERTIUM_SERVER_KEY)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]