1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-24 08:02:18 +02:00

add download link & auto download translated file

This commit is contained in:
Sébastien Thuret 2021-10-25 10:50:55 +02:00
parent b97134ac07
commit 6a304df2e8
No known key found for this signature in database
GPG Key ID: 4742E2D66933BB08
3 changed files with 39 additions and 9 deletions

View File

@ -507,11 +507,9 @@ def create_app(args):
id: translate
type: object
properties:
translatedText:
oneOf:
- type: string
- type: array
description: Translated text(s)
translatedFileUrl:
type: string
description: Translated file url
400:
description: Invalid request
schema:
@ -588,7 +586,7 @@ def create_app(args):
translated_filename = os.path.basename(translated_file_path)
return jsonify(
{
"translatedFileUrl": url_for('download_file', filename=translated_filename)
"translatedFileUrl": url_for('download_file', filename=translated_filename, _external=True)
}
)
except Exception as e:

View File

@ -35,6 +35,7 @@ document.addEventListener('DOMContentLoaded', function(){
translationType: "text",
inputFile: false,
loadingFileTranslation: false,
translatedFileUrl: "",
},
mounted: function(){
var self = this;
@ -317,10 +318,13 @@ document.addEventListener('DOMContentLoaded', function(){
removeFile: function(e) {
e.preventDefault()
this.inputFile = false;
this.translatedFileUrl = "";
this.loadingFileTranslation = false;
},
translateFile: function(e) {
e.preventDefault();
let self = this;
let translateFileRequest = new XMLHttpRequest();
translateFileRequest.open("POST", BaseUrl + "/translate_file", true);
@ -333,12 +337,39 @@ document.addEventListener('DOMContentLoaded', function(){
this.loadingFileTranslation = true
translateFileRequest.onload = () => {
translateFileRequest.onload = function() {
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);
}
}

View File

@ -203,7 +203,8 @@
</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="indeterminate"></div>
</div>