From 9ce186861b1899d2fc87df72747367dee6977c52 Mon Sep 17 00:00:00 2001 From: frianasoa Date: Tue, 19 Dec 2023 22:49:46 +0900 Subject: [PATCH] Added DeepL translation --- content/notes/menus.js | 88 +++++++++++++++++++++++---------- content/settings/ai.xhtml | 4 +- content/settings/preferences.js | 1 + content/ui/ai-ui.js | 2 +- core/ai.js | 31 ++++++++++++ install.rdf | 2 +- manifest.json | 2 +- zenote-update.json | 6 +-- zenote-update.rdf | 6 +-- 9 files changed, 106 insertions(+), 36 deletions(-) diff --git a/content/notes/menus.js b/content/notes/menus.js index d228780..e9718a8 100644 --- a/content/notes/menus.js +++ b/content/notes/menus.js @@ -61,7 +61,17 @@ Menus = { } } - items_ai["translate-google-en"] = {name: "Translate to "+tl+" (Google)", icon: "fa-google"}; + items_ai["zn-translation"] = { + name: "Translate to "+tl, + icon: "fa-language", + items: { + "translate-google": {name: "Google", icon: "fa-google"} + } + }; + if(Zotero.ZeNotes.Prefs.getb("deepl-api-key")!="") + { + items_ai["zn-translation"]["items"]["translate-deepl"] = {name: "DeepL", icon: "fa-d"}; + } items_ai["sep-ai-02"] = "---------"; if(Zotero.ZeNotes.Prefs.getb("bard-api-key")!="") @@ -330,51 +340,77 @@ Menus = { } - else if(key=="translate-google-en") + else if(key.includes("translate-")) { - var mode = "api-key"; - if(!annotationkey) { alert("Annotation not found!"); return; } - if(Zotero.ZeNotes.Prefs.getb("google-translate-key")=="") + var annotation = Zotero.Items.get(annotationid); + + if(!annotation) { - mode="free-0"; + alert("Annotation text not found!"); + return; } - var annotation = Zotero.Items.get(annotationid); var currentcomment = annotation.annotationComment; if(currentcomment==null) { currentcomment = ""; } - if(!annotation) - { - alert("Annotation text not found!"+annotationkey); - return; - } - var tl = Zotero.ZeNotes.Prefs.get("target-language"); - Zotero.ZeNotes.Ai.Google.translate(annotation["annotationText"], tl, mode).then(r=>{ - var table = AiUi.createdialog(annotation, currentcomment, r, "g-translate"); - Dialog.open(table, function(){}, "Choose translation [Google]", "close"); - }).catch(r=>{ - var html = ""; - if(Array.isArray(r)) - { - html = r.join("
"); - } - else + + if(key.includes("-google")) + { + var mode = "api-key"; + if(Zotero.ZeNotes.Prefs.getb("google-translate-key")=="") { - html="-"+r; + mode="free-0"; } - Dialog.open(html, function(){ + Zotero.ZeNotes.Ai.Google.translate(annotation["annotationText"], tl, mode).then(r=>{ + var table = AiUi.createdialog(annotation, currentcomment, r, "g-translate"); + Dialog.open(table, function(){}, "Choose translation [Google]", "close"); + }).catch(r=>{ + var html = ""; + if(Array.isArray(r)) + { + html = r.join("
"); + } + else + { + html="-"+r; + } + Dialog.open(html, function(){ + }); }); - }); + } + else if(key.includes("-deepl")) + { + Zotero.ZeNotes.Ai.DeepL.translate(annotation["annotationText"], tl).then(r=>{ + var table = AiUi.createdialog(annotation, currentcomment, r, "deepl-translate"); + Dialog.open(table, function(){}, "Choose translation [DeepL]", "close"); + }).catch(r=>{ + var html = ""; + if(Array.isArray(r)) + { + html = r.join("
"); + } + else + { + html="-"+r; + } + Dialog.open(html, function(){ + }); + }); + } + + + + } else if(key=="summarize-row-annotations") diff --git a/content/settings/ai.xhtml b/content/settings/ai.xhtml index 91f6549..995c2e4 100644 --- a/content/settings/ai.xhtml +++ b/content/settings/ai.xhtml @@ -1,5 +1,7 @@
+ + - +
DeepL API key
Google translate API key
Bard API key Model
Cell custom prompt
Row summary prompt
Table summary prompt
Google translate API key
diff --git a/content/settings/preferences.js b/content/settings/preferences.js index 0720cd5..d0d91e6 100644 --- a/content/settings/preferences.js +++ b/content/settings/preferences.js @@ -316,6 +316,7 @@ Zotero_Preferences.ZeNotes = { Zotero_Preferences.ZeNotes.loadpreference("bard-api-key", "zn-bard-api-key", "encrypt"); Zotero_Preferences.ZeNotes.loadpreference("google-translate-key", "zn-google-translate-key", "encrypt"); + Zotero_Preferences.ZeNotes.loadpreference("deepl-api-key", "zn-deepl-api-key", "encrypt"); Zotero_Preferences.ZeNotes.loadpreference("bard-model", "zn-bard-model"); Zotero_Preferences.ZeNotes.loadpreference("cell-custom-prompt", "zn-cell-custom-prompt"); diff --git a/content/ui/ai-ui.js b/content/ui/ai-ui.js index 0677f62..7a294b2 100644 --- a/content/ui/ai-ui.js +++ b/content/ui/ai-ui.js @@ -24,7 +24,7 @@ AiUi = { var comment = c.innerText; annotation.annotationComment = currentcomment+"\n\n[Paraphrase]\n"+comment+"\n"; - if(mode=="g-translate") + if(["g-translate", "deepl-translate"].includes(mode)) { annotation.annotationComment = currentcomment+"\n\n[Translation]\n"+comment+"\n"; } diff --git a/core/ai.js b/core/ai.js index 12a17c4..3aa894f 100644 --- a/core/ai.js +++ b/core/ai.js @@ -70,6 +70,18 @@ Ai={ return Promise.resolve(["Error: "+e, JSON.stringify(data)]); } } + + else if(mode=="deepl-translate") + { + try { + return Promise.resolve(data.translations.map(function(e){return e.text})); + } + catch(e) + { + return Promise.resolve(["Error: "+e, JSON.stringify(data)]); + } + } + else if(mode=="bing") { try { @@ -233,4 +245,23 @@ Ai.Google = { return Ai.request(url, options, "g-translate-free-1"); } }, +} + +Ai.DeepL = { + translate(sentence, language){ + var apikey = Zotero.ZeNotes.Prefs.getb("deepl-api-key"); + var url = "https://api-free.deepl.com/v2/translate" + + var payload = {text: [sentence], target_lang: language.toUpperCase()}; + + var options = { + method: 'POST', + headers: { + "Authorization": "DeepL-Auth-Key "+apikey, + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + } + return Ai.request(url, options, "deepl-translate"); + }, } \ No newline at end of file diff --git a/install.rdf b/install.rdf index ee118d9..b22ebed 100644 --- a/install.rdf +++ b/install.rdf @@ -5,7 +5,7 @@ zenotes@alefa.net ZeNotes - 0.6.7 + 0.6.8 true https://raw.githubusercontent.com/frianasoa/zenotes/main/zenote-update.json https://github.com/frianasoa/zenotes diff --git a/manifest.json b/manifest.json index 9478a5d..f14cd3f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "Ze Notes", - "version": "0.6.7", + "version": "0.6.8", "description": "Advanced notes manager", "homepage_url": "https://github.com/frianasoa/zenotes", "author": "Fanantenana Rianasoa Andriariniaina", diff --git a/zenote-update.json b/zenote-update.json index d02d46d..8af28f9 100644 --- a/zenote-update.json +++ b/zenote-update.json @@ -3,9 +3,9 @@ "zenotes@alefa.net": { "updates": [ { - "version": "0.6.7", - "update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.7/zenotes-v0.6.7.xpi", - "update_hash": "sha256:049f73820b48db38f1fe56cbb61d747f755e875d81bc674ef9c9f65211853299", + "version": "0.6.8", + "update_link": "https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.8/zenotes-v0.6.8.xpi", + "update_hash": "sha256:baa8d6abadd563420521cc87c0a34469324aab265b0ed615dcbb3a73337526da", "applications": { "gecko": { "strict_min_version": "60.0" diff --git a/zenote-update.rdf b/zenote-update.rdf index 46749fa..f4cc39c 100644 --- a/zenote-update.rdf +++ b/zenote-update.rdf @@ -5,13 +5,13 @@ - 0.6.7 + 0.6.8 zotero@chnm.gmu.edu 5.0.0 6.* - https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.7/zenotes-v0.6.7.xpi + https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.8/zenotes-v0.6.8.xpi @@ -20,7 +20,7 @@ juris-m@juris-m.github.io 4.999 6.* - https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.7/zenotes-v0.6.7.xpi + https://github.com/frianasoa/Ze-Notes/releases/download/v0.6.8/zenotes-v0.6.8.xpi