mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-24 08:02:18 +02:00
add frontend language default parameter #6
This commit is contained in:
parent
9fb974b7b1
commit
72b624db4c
51
app/app.py
51
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"
|
||||
|
@ -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);
|
||||
|
8
main.py
8
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="<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:
|
||||
|
Loading…
Reference in New Issue
Block a user