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 diff --git a/app/app.py b/app/app.py index 9dc5fc4..0f098bc 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( @@ -54,34 +63,29 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False): 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) 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]) + return jsonify([{'code': l.code, 'name': l.name} for l in languages]) # Add cors @app.after_request @@ -127,44 +131,40 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False): 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: @@ -201,6 +201,50 @@ 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: + charLimit: + type: integer + description: Character input limit for this language (-1 indicates no limit) + 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({'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}} + }) swag = swagger(app) swag['info']['version'] = "1.0" diff --git a/app/static/favicon.ico b/app/static/favicon.ico new file mode 100644 index 0000000..1b8ff0c Binary files /dev/null and b/app/static/favicon.ico differ diff --git a/app/templates/index.html b/app/templates/index.html index 7567e9a..6d430ce 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -4,6 +4,7 @@ LibreTranslate - Free and Open Source Machine Translation API + @@ -256,6 +257,7 @@ document.addEventListener('DOMContentLoaded', function(){ loading: true, error: "", langs: [], + settings: {}, sourceLang: "", targetLang: "", @@ -267,10 +269,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; + self.charactersLimit = self.settings.charLimit; + }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,18 +305,6 @@ 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 { self.error = "Cannot load /languages"; @@ -299,12 +312,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: