mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-29 03:11:01 +00:00
add download link & auto download translated file
This commit is contained in:
parent
b97134ac07
commit
6a304df2e8
3 changed files with 39 additions and 9 deletions
10
app/app.py
10
app/app.py
|
@ -507,11 +507,9 @@ def create_app(args):
|
||||||
id: translate
|
id: translate
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
translatedText:
|
translatedFileUrl:
|
||||||
oneOf:
|
type: string
|
||||||
- type: string
|
description: Translated file url
|
||||||
- type: array
|
|
||||||
description: Translated text(s)
|
|
||||||
400:
|
400:
|
||||||
description: Invalid request
|
description: Invalid request
|
||||||
schema:
|
schema:
|
||||||
|
@ -588,7 +586,7 @@ def create_app(args):
|
||||||
translated_filename = os.path.basename(translated_file_path)
|
translated_filename = os.path.basename(translated_file_path)
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"translatedFileUrl": url_for('download_file', filename=translated_filename)
|
"translatedFileUrl": url_for('download_file', filename=translated_filename, _external=True)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -35,6 +35,7 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
translationType: "text",
|
translationType: "text",
|
||||||
inputFile: false,
|
inputFile: false,
|
||||||
loadingFileTranslation: false,
|
loadingFileTranslation: false,
|
||||||
|
translatedFileUrl: "",
|
||||||
},
|
},
|
||||||
mounted: function(){
|
mounted: function(){
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -317,10 +318,13 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
removeFile: function(e) {
|
removeFile: function(e) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
this.inputFile = false;
|
this.inputFile = false;
|
||||||
|
this.translatedFileUrl = "";
|
||||||
|
this.loadingFileTranslation = false;
|
||||||
},
|
},
|
||||||
translateFile: function(e) {
|
translateFile: function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
let self = this;
|
||||||
let translateFileRequest = new XMLHttpRequest();
|
let translateFileRequest = new XMLHttpRequest();
|
||||||
|
|
||||||
translateFileRequest.open("POST", BaseUrl + "/translate_file", true);
|
translateFileRequest.open("POST", BaseUrl + "/translate_file", true);
|
||||||
|
@ -333,12 +337,39 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
|
|
||||||
this.loadingFileTranslation = true
|
this.loadingFileTranslation = true
|
||||||
|
|
||||||
translateFileRequest.onload = () => {
|
translateFileRequest.onload = function() {
|
||||||
if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) {
|
if (translateFileRequest.readyState === 4 && translateFileRequest.status === 200) {
|
||||||
this.loadingFileTranslation = false
|
try{
|
||||||
|
self.loadingFileTranslation = false;
|
||||||
|
|
||||||
|
let res = JSON.parse(this.response);
|
||||||
|
if (res.translatedFileUrl){
|
||||||
|
self.translatedFileUrl = res.translatedFileUrl;
|
||||||
|
|
||||||
|
let link = document.createElement("a");
|
||||||
|
link.target = "_blank";
|
||||||
|
link.href = self.translatedFileUrl;
|
||||||
|
link.click();
|
||||||
|
}else{
|
||||||
|
throw new Error(res.error || "Unknown error");
|
||||||
|
}
|
||||||
|
|
||||||
|
}catch(e){
|
||||||
|
self.error = e.message;
|
||||||
|
self.loadingFileTranslation = false;
|
||||||
|
self.inputFile = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
translateFileRequest.onerror = function() {
|
||||||
|
self.error = "Error while calling /translate_file";
|
||||||
|
self.loadingFileTranslation = false;
|
||||||
|
self.inputFile = false;
|
||||||
|
};
|
||||||
|
|
||||||
translateFileRequest.send(data);
|
translateFileRequest.send(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,7 +203,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button @click="translateFile" v-if="loadingFileTranslation === false" class="btn">Translate</button>
|
<button @click="translateFile" v-if="translatedFileUrl === '' && loadingFileTranslation === false" class="btn">Translate</button>
|
||||||
|
<a v-if="translatedFileUrl !== ''" :href="translatedFileUrl" class="btn">Download</a>
|
||||||
<div class="progress" v-if="loadingFileTranslation">
|
<div class="progress" v-if="loadingFileTranslation">
|
||||||
<div class="indeterminate"></div>
|
<div class="indeterminate"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in a new issue