Merge pull request #460 from SethFalco/fix-305

fix: request and push notification on file translations
This commit is contained in:
Piero Toffanin 2023-07-08 14:25:31 +02:00 committed by GitHub
commit 99ce267c91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 6 deletions

View file

@ -436,7 +436,7 @@ code[class*="language-"], pre[class*="language-"] {
background-color: var(--pri-bg-color); background-color: var(--pri-bg-color);
} }
.card-stacked { .card-stacked, .card-stacked .card-content {
background-color: var(--sec-bg-color) background-color: var(--sec-bg-color)
} }

View file

@ -402,9 +402,10 @@ document.addEventListener('DOMContentLoaded', function(){
self.translatedFileUrl = res.translatedFileUrl; self.translatedFileUrl = res.translatedFileUrl;
let link = document.createElement("a"); let link = document.createElement("a");
link.target = "_blank";
link.href = self.translatedFileUrl; link.href = self.translatedFileUrl;
link.download = "";
link.click(); link.click();
handleNotification("Translation Complete", "Finished translating " + self.inputFile.name + ".");
}else{ }else{
throw new Error(res.error || {{ _e("Unknown error") }}); throw new Error(res.error || {{ _e("Unknown error") }});
} }
@ -413,22 +414,27 @@ document.addEventListener('DOMContentLoaded', function(){
self.error = e.message; self.error = e.message;
self.loadingFileTranslation = false; self.loadingFileTranslation = false;
self.inputFile = false; self.inputFile = false;
handleNotification("Translation Failed", e.message);
} }
}else{ }else{
let res = JSON.parse(this.response); let res = JSON.parse(this.response);
self.error = res.error || {{ _e("Unknown error") }}; self.error = res.error || {{ _e("Unknown error") }};
self.loadingFileTranslation = false; self.loadingFileTranslation = false;
self.inputFile = false; self.inputFile = false;
handleNotification("Translation Failed", self.error);
} }
} }
translateFileRequest.onerror = function() { translateFileRequest.onerror = function() {
self.error = {{ _e("Cannot load %(url)s", url="/translate_file") }}; const message = {{ _e("Cannot load %(url)s", url="/translate_file") }};
self.error = message;
self.loadingFileTranslation = false; self.loadingFileTranslation = false;
self.inputFile = false; self.inputFile = false;
handleNotification("Translation Failed", message);
}; };
translateFileRequest.send(data); translateFileRequest.send(data);
Notification.requestPermission();
} }
} }
}); });
@ -474,6 +480,17 @@ function handleLangsResponse(self, response) {
self.loading = false; self.loading = false;
} }
/**
* @param {string} title
* @param {string} body
*/
function handleNotification(title, body) {
new Notification(title, {
body,
tag: 'libretranslate'
});
}
/** /**
* @param {object} langDropdown * @param {object} langDropdown
* @param {string} lang * @param {string} lang