From 72b624db4cff41cfe5c8a333b5548ba8fe882ecb Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Sun, 10 Jan 2021 08:07:56 +0000 Subject: [PATCH 1/5] add frontend language default parameter #6 --- app/app.py | 51 +++++++++++++++++++++++++++++++++++++++- app/templates/index.html | 44 ++++++++++++++++++++++------------ main.py | 8 ++++++- 3 files changed, 86 insertions(+), 17 deletions(-) diff --git a/app/app.py b/app/app.py index 9dc5fc4..4c3bfa6 100644 --- a/app/app.py +++ b/app/app.py @@ -10,7 +10,7 @@ def get_remote_address(): return ip -def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False): +def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="es"): from app.init import boot boot() @@ -20,6 +20,15 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False): if debug: app.config['TEMPLATES_AUTO_RELOAD'] = True + # Map userdefined frontend languages to argos language object. + frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source]), None) + frontend_argos_language_target = next(iter([l for l in languages if l.code == frontend_language_target]), None) + # Raise AttributeError to prevent app startup if user input is not valid. + if frontend_argos_language_source is None: + raise AttributeError(f"{frontend_language_source} as frontend source language is not supported.") + if frontend_argos_language_target is None: + raise AttributeError(f"{frontend_language_target} as frontend target language is not supported.") + if req_limit > 0: from flask_limiter import Limiter limiter = Limiter( @@ -201,6 +210,46 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False): except Exception as e: abort(500, description="Cannot translate text: %s" % str(e)) + @app.route("/frontend/settings") + def frontend_settings(): + """ + Retrieve frontend specific settings + --- + tags: + - frontend + responses: + 200: + description: frontend settings + schema: + id: frontend-settings + type: object + properties: + language: + type: object + properties: + source: + type: object + properties: + code: + type: string + description: Language code + name: + type: string + description: Human-readable language name (in English) + target: + type: object + properties: + code: + type: string + description: Language code + name: + type: string + description: Human-readable language name (in English) + """ + return jsonify({'language': { + 'source': {'code': frontend_argos_language_source.code, 'name': frontend_argos_language_source.name}, + 'target': {'code': frontend_argos_language_target.code, 'name': frontend_argos_language_target.name}} + }) swag = swagger(app) swag['info']['version'] = "1.0" diff --git a/app/templates/index.html b/app/templates/index.html index 7567e9a..0ecf45a 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -256,6 +256,7 @@ document.addEventListener('DOMContentLoaded', function(){ loading: true, error: "", langs: [], + settings: {}, sourceLang: "", targetLang: "", @@ -267,10 +268,33 @@ document.addEventListener('DOMContentLoaded', function(){ }, mounted: function(){ var self = this; - var request = new XMLHttpRequest(); - request.open('GET', BaseUrl + '/languages', true); + var requestSettings = new XMLHttpRequest(); + requestSettings.open('GET', BaseUrl + '/frontend/settings', true); - request.onload = function() { + requestSettings.onload = function() { + if (this.status >= 200 && this.status < 400) { + // Success! + self.settings = JSON.parse(this.response); + self.sourceLang = self.settings.language.source.code; + self.targetLang = self.settings.language.target.code; + + }else { + self.error = "Cannot load /frontend/settings"; + self.loading = false; + } + }; + + requestSettings.onerror = function() { + self.error = "Error while calling /frontend/settings"; + self.loading = false; + }; + + requestSettings.send(); + + var requestLanguages = new XMLHttpRequest(); + requestLanguages.open('GET', BaseUrl + '/languages', true); + + requestLanguages.onload = function() { if (this.status >= 200 && this.status < 400) { // Success! self.langs = JSON.parse(this.response); @@ -280,17 +304,7 @@ document.addEventListener('DOMContentLoaded', function(){ return; } - self.sourceLang = self.langs[0].code; - self.targetLang = self.langs[1].code; self.charactersLimit = self.langs[0].charLimit; - // TODO: update this when switching languages - - // Set Spanish - for (var i = 1; i < self.langs.length; i++){ - if (self.langs[i].code === "es"){ - self.targetLang = "es"; - } - } self.loading = false; } else { @@ -299,12 +313,12 @@ document.addEventListener('DOMContentLoaded', function(){ } }; - request.onerror = function() { + requestLanguages.onerror = function() { self.error = "Error while calling /languages"; self.loading = false; }; - request.send(); + requestLanguages.send(); }, updated: function(){ M.FormSelect.init(this.$refs.sourceLangDropdown); diff --git a/main.py b/main.py index 065717a..36e4ef6 100644 --- a/main.py +++ b/main.py @@ -16,6 +16,10 @@ parser.add_argument('--debug', default=False, action="store_true", help="Enable debug environment") parser.add_argument('--ssl', default=None, action="store_true", help="Whether to enable SSL") +parser.add_argument('--frontend-language-source', type=str, default="en", metavar="", + help='Set frontend default language - source (%(default)s)') +parser.add_argument('--frontend-language-target', type=str, default="es", metavar="", + help='Set frontend default language - target (%(default)s)') args = parser.parse_args() @@ -24,7 +28,9 @@ if __name__ == "__main__": app = create_app(char_limit=args.char_limit, req_limit=args.req_limit, ga_id=args.ga_id, - debug=args.debug) + debug=args.debug, + frontend_language_source=args.frontend_language_source, + frontend_language_target=args.frontend_language_target) if args.debug: app.run(host=args.host, port=args.port) else: From 5c82f9e11e1cb886bbc3d55ef4e2c76642b11d81 Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Sun, 10 Jan 2021 09:15:55 +0100 Subject: [PATCH 2/5] update README.md frontend arguments #6 --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 819243c..ad8b0df 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,8 @@ docker-compose up -d --build | --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` | | --debug | Enable debug environment | `False` | | --ssl | Whether to enable SSL | `False` | +| --frontend-language-source | Set frontend default language - source | `en` | +| --frontend-language-target | Set frontend default language - target | `es` | ## Roadmap From eb832cc538a7b959a67f405784e1d2db229f9817 Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Sun, 10 Jan 2021 08:38:11 +0000 Subject: [PATCH 3/5] fix swagger doc response model --- app/app.py | 106 +++++++++++++++++++++++++---------------------------- 1 file changed, 50 insertions(+), 56 deletions(-) diff --git a/app/app.py b/app/app.py index 4c3bfa6..3b40c63 100644 --- a/app/app.py +++ b/app/app.py @@ -63,32 +63,30 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la responses: 200: description: List of languages - content: - application/json: - schema: - type: array - items: - type: object - properties: - code: - type: string - description: Language code - name: - type: string - description: Human-readable language name (in English) - charLimit: - type: string - description: Character input limit for this language (-1 indicates no limit) + schema: + id: languages + type: array + items: + type: object + properties: + code: + type: string + description: Language code + name: + type: string + description: Human-readable language name (in English) + charLimit: + type: string + description: Character input limit for this language (-1 indicates no limit) 429: description: Slow down - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Reason for slow down + schema: + id: error-slow-down + type: object + properties: + error: + type: string + description: Reason for slow down """ return jsonify([{'code': l.code, 'name': l.name, 'charLimit': char_limit } for l in languages]) @@ -136,44 +134,40 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la responses: 200: description: Translated text - content: - application/json: - schema: - type: object - properties: - translatedText: - type: string - description: Translated text + schema: + id: translate + type: object + properties: + translatedText: + type: string + description: Translated text 400: description: Invalid request - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Error message + schema: + id: error-response + type: object + properties: + error: + type: string + description: Error message 500: description: Translation error - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Error message + schema: + id: error-response + type: object + properties: + error: + type: string + description: Error message 429: description: Slow down - content: - application/json: - schema: - type: object - properties: - error: - type: string - description: Reason for slow down + schema: + id: error-slow-down + type: object + properties: + error: + type: string + description: Reason for slow down """ if request.is_json: From b0645cd5d7d636cdfa7801a99b244d0519e6b75c Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Sun, 10 Jan 2021 08:59:07 +0000 Subject: [PATCH 4/5] add favicon --- app/static/favicon.ico | Bin 0 -> 1150 bytes app/templates/index.html | 1 + 2 files changed, 1 insertion(+) create mode 100644 app/static/favicon.ico diff --git a/app/static/favicon.ico b/app/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..1b8ff0c34a94785f6d375c0c20f85ebceff25e4c GIT binary patch literal 1150 zcmbW#%PT}-7{~Ev+>%RVA-N7wY-EKUvanGiN-irKg@uKG09m^J0hVm76s1{8qLhUa zL$OgBb`~z*-^_bBoz56}rq6lb=XuUKbIyBSvk1S^QsWr4=^V3iGiwGVDycey8NIT? zlw_Xy5aE=pG^eR2zeNGfMGOT_Y9!x4YoH#MZc#i0PyQVX%+65hD*B&EG`QS9bdx+{ z54%pBaT#7zGdaZp>bxRj1$56C9-!+_@QhCQql4rUx<{?E&p3s7=y~;N)}i;&GY6xc z!Uc{T&D+p|Ys}yrvq%?xG+yxn%@w)Wf!5&)O$duTiux}4U)h9eic!2{8`%hpVT!lt zac&p~q0n|zUt2RQw9n?Cchg*ZYZY3nVx)^A8s2(Gnb)|yim@N}7=gaAe-Du+V~nHL zR?q6uJIEd0@C5Y+5Dcy5I3^tJ!(&Htf3KcAQ|P_52BP_YLf8H2+7EVdQoh+5SF9k0 WIA1ui#MldVQLKwQmi$P3(#IDB@J$&2 literal 0 HcmV?d00001 diff --git a/app/templates/index.html b/app/templates/index.html index 0ecf45a..b0a3b60 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -4,6 +4,7 @@ LibreTranslate - Free and Open Source Machine Translation API + From f5d249c880e9dbb03f93f3acd0cecc1ebf53ff33 Mon Sep 17 00:00:00 2001 From: worldworm <13227454+worldworm@users.noreply.github.com> Date: Sun, 10 Jan 2021 09:24:42 +0000 Subject: [PATCH 5/5] move charLimit to settings api --- app/app.py | 11 ++++++----- app/templates/index.html | 4 +--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/app/app.py b/app/app.py index 3b40c63..0f098bc 100644 --- a/app/app.py +++ b/app/app.py @@ -75,9 +75,6 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la name: type: string description: Human-readable language name (in English) - charLimit: - type: string - description: Character input limit for this language (-1 indicates no limit) 429: description: Slow down schema: @@ -88,7 +85,7 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la type: string description: Reason for slow down """ - return jsonify([{'code': l.code, 'name': l.name, 'charLimit': char_limit } for l in languages]) + return jsonify([{'code': l.code, 'name': l.name} for l in languages]) # Add cors @app.after_request @@ -218,6 +215,9 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la id: frontend-settings type: object properties: + charLimit: + type: integer + description: Character input limit for this language (-1 indicates no limit) language: type: object properties: @@ -240,7 +240,8 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la type: string description: Human-readable language name (in English) """ - return jsonify({'language': { + return jsonify({'charLimit': char_limit, + 'language': { 'source': {'code': frontend_argos_language_source.code, 'name': frontend_argos_language_source.name}, 'target': {'code': frontend_argos_language_target.code, 'name': frontend_argos_language_target.name}} }) diff --git a/app/templates/index.html b/app/templates/index.html index b0a3b60..6d430ce 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -278,7 +278,7 @@ document.addEventListener('DOMContentLoaded', function(){ self.settings = JSON.parse(this.response); self.sourceLang = self.settings.language.source.code; self.targetLang = self.settings.language.target.code; - + self.charactersLimit = self.settings.charLimit; }else { self.error = "Cannot load /frontend/settings"; self.loading = false; @@ -305,8 +305,6 @@ document.addEventListener('DOMContentLoaded', function(){ return; } - self.charactersLimit = self.langs[0].charLimit; - self.loading = false; } else { self.error = "Cannot load /languages";