Merge pull request #7 from worldworm/main

Add frontend language default parameter, favicon, move charLimit api, swagger response model
This commit is contained in:
Piero Toffanin 2021-01-10 09:43:52 -05:00 committed by GitHub
commit 00273d2544
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 141 additions and 76 deletions

View file

@ -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

View file

@ -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"

BIN
app/static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -4,6 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LibreTranslate - Free and Open Source Machine Translation API</title>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<meta name="description" content="Free and Open Source Machine Translation API. 100% self-hosted, no limits, no ties to proprietary services. Run your own API server in just a few minutes.">
<meta name="keywords" content="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);

View file

@ -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="<language code>",
help='Set frontend default language - source (%(default)s)')
parser.add_argument('--frontend-language-target', type=str, default="es", metavar="<language code>",
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: