mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-25 09:21:04 +00:00
Merge pull request #7 from worldworm/main
Add frontend language default parameter, favicon, move charLimit api, swagger response model
This commit is contained in:
commit
00273d2544
5 changed files with 141 additions and 76 deletions
|
@ -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` |
|
| --ga-id | Enable Google Analytics on the API client page by providing an ID | `No tracking` |
|
||||||
| --debug | Enable debug environment | `False` |
|
| --debug | Enable debug environment | `False` |
|
||||||
| --ssl | Whether to enable SSL | `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
|
## Roadmap
|
||||||
|
|
160
app/app.py
160
app/app.py
|
@ -10,7 +10,7 @@ def get_remote_address():
|
||||||
|
|
||||||
return ip
|
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
|
from app.init import boot
|
||||||
boot()
|
boot()
|
||||||
|
|
||||||
|
@ -20,6 +20,15 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False):
|
||||||
if debug:
|
if debug:
|
||||||
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
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:
|
if req_limit > 0:
|
||||||
from flask_limiter import Limiter
|
from flask_limiter import Limiter
|
||||||
limiter = Limiter(
|
limiter = Limiter(
|
||||||
|
@ -54,34 +63,29 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False):
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: List of languages
|
description: List of languages
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: languages
|
||||||
schema:
|
type: array
|
||||||
type: array
|
items:
|
||||||
items:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
code:
|
||||||
code:
|
type: string
|
||||||
type: string
|
description: Language code
|
||||||
description: Language code
|
name:
|
||||||
name:
|
type: string
|
||||||
type: string
|
description: Human-readable language name (in English)
|
||||||
description: Human-readable language name (in English)
|
|
||||||
charLimit:
|
|
||||||
type: string
|
|
||||||
description: Character input limit for this language (-1 indicates no limit)
|
|
||||||
429:
|
429:
|
||||||
description: Slow down
|
description: Slow down
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: error-slow-down
|
||||||
schema:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
error:
|
||||||
error:
|
type: string
|
||||||
type: string
|
description: Reason for slow down
|
||||||
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
|
# Add cors
|
||||||
@app.after_request
|
@app.after_request
|
||||||
|
@ -127,44 +131,40 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False):
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
description: Translated text
|
description: Translated text
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: translate
|
||||||
schema:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
translatedText:
|
||||||
translatedText:
|
type: string
|
||||||
type: string
|
description: Translated text
|
||||||
description: Translated text
|
|
||||||
400:
|
400:
|
||||||
description: Invalid request
|
description: Invalid request
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: error-response
|
||||||
schema:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
error:
|
||||||
error:
|
type: string
|
||||||
type: string
|
description: Error message
|
||||||
description: Error message
|
|
||||||
500:
|
500:
|
||||||
description: Translation error
|
description: Translation error
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: error-response
|
||||||
schema:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
error:
|
||||||
error:
|
type: string
|
||||||
type: string
|
description: Error message
|
||||||
description: Error message
|
|
||||||
429:
|
429:
|
||||||
description: Slow down
|
description: Slow down
|
||||||
content:
|
schema:
|
||||||
application/json:
|
id: error-slow-down
|
||||||
schema:
|
type: object
|
||||||
type: object
|
properties:
|
||||||
properties:
|
error:
|
||||||
error:
|
type: string
|
||||||
type: string
|
description: Reason for slow down
|
||||||
description: Reason for slow down
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if request.is_json:
|
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:
|
except Exception as e:
|
||||||
abort(500, description="Cannot translate text: %s" % str(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 = swagger(app)
|
||||||
swag['info']['version'] = "1.0"
|
swag['info']['version'] = "1.0"
|
||||||
|
|
BIN
app/static/favicon.ico
Normal file
BIN
app/static/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
|
@ -4,6 +4,7 @@
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>LibreTranslate - Free and Open Source Machine Translation API</title>
|
<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="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">
|
<meta name="keywords" content="translation,api">
|
||||||
|
|
||||||
|
@ -256,6 +257,7 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
loading: true,
|
loading: true,
|
||||||
error: "",
|
error: "",
|
||||||
langs: [],
|
langs: [],
|
||||||
|
settings: {},
|
||||||
sourceLang: "",
|
sourceLang: "",
|
||||||
targetLang: "",
|
targetLang: "",
|
||||||
|
|
||||||
|
@ -267,10 +269,33 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
},
|
},
|
||||||
mounted: function(){
|
mounted: function(){
|
||||||
var self = this;
|
var self = this;
|
||||||
var request = new XMLHttpRequest();
|
var requestSettings = new XMLHttpRequest();
|
||||||
request.open('GET', BaseUrl + '/languages', true);
|
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) {
|
if (this.status >= 200 && this.status < 400) {
|
||||||
// Success!
|
// Success!
|
||||||
self.langs = JSON.parse(this.response);
|
self.langs = JSON.parse(this.response);
|
||||||
|
@ -280,18 +305,6 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
return;
|
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;
|
self.loading = false;
|
||||||
} else {
|
} else {
|
||||||
self.error = "Cannot load /languages";
|
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.error = "Error while calling /languages";
|
||||||
self.loading = false;
|
self.loading = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
request.send();
|
requestLanguages.send();
|
||||||
},
|
},
|
||||||
updated: function(){
|
updated: function(){
|
||||||
M.FormSelect.init(this.$refs.sourceLangDropdown);
|
M.FormSelect.init(this.$refs.sourceLangDropdown);
|
||||||
|
|
8
main.py
8
main.py
|
@ -16,6 +16,10 @@ parser.add_argument('--debug', default=False, action="store_true",
|
||||||
help="Enable debug environment")
|
help="Enable debug environment")
|
||||||
parser.add_argument('--ssl', default=None, action="store_true",
|
parser.add_argument('--ssl', default=None, action="store_true",
|
||||||
help="Whether to enable SSL")
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
@ -24,7 +28,9 @@ if __name__ == "__main__":
|
||||||
app = create_app(char_limit=args.char_limit,
|
app = create_app(char_limit=args.char_limit,
|
||||||
req_limit=args.req_limit,
|
req_limit=args.req_limit,
|
||||||
ga_id=args.ga_id,
|
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:
|
if args.debug:
|
||||||
app.run(host=args.host, port=args.port)
|
app.run(host=args.host, port=args.port)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in a new issue