[Share] Auto-Translate the New Item's Title #107
Replies: 6 comments 8 replies
-
// Translate item title and abstract automatically
// Wait for abstract to be filled before translating abstract
// author windingwind
// link https://github.com/windingwind/zotero-tag/discussions/107
// usage Event=Create Item
const Zotero = require("Zotero");
if (!Zotero.PDFTranslate) {
return "[Action:Translate Title and Abstract] Translate for Zotero is not installed or disabled.";
}
if (!item) {
return "[Action:Translate Title and Abstract] Target item is empty";
}
const title = item.getField("title");
const abstract = item.getField("abstractNote");
const titleResult = (await Zotero.PDFTranslate.api.translate(title)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "titleTranslation", titleResult);
let abstractResult = "";
if (abstract) {
abstractResult = (await Zotero.PDFTranslate.api.translate(abstract)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "abstractTranslation", abstractResult);
} else {
const observerID = Zotero.Notifier.registerObserver({
notify: async (event, type, ids, extraData) => {
if (event === 'modify' && type === 'item' && ids.includes(item.id)) {
const newAbstract = item.getField('abstractNote');
if (newAbstract) {
abstractResult = (await Zotero.PDFTranslate.api.translate(newAbstract)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "abstractTranslation", abstractResult);
Zotero.Notifier.unregisterObserver(observerID);
}
}
}
}, ['item']);
}
return `[Action:Translate Title and Abstract] successfully translate title ${title} to ${titleResult} and abstract ${abstract} to ${abstractResult}.`; |
Beta Was this translation helpful? Give feedback.
-
返回错误:Script Error: missing ] after element list 最后一行有误,应修改为:
|
Beta Was this translation helpful? Give feedback.
-
PhoenixHwang 版本对,存在一个bug,翻译出来的信息,在其他字段中~ |
Beta Was this translation helpful? Give feedback.
-
跳过中文条目: // Translate item title and abstract automatically
// Wait for abstract to be filled before translating abstract
// author windingwind, PhoenixHwang, northword
// link https://github.com/windingwind/zotero-tag/discussions/107
// usage Event=Create Item
const Zotero = require("Zotero");
if (!Zotero.PDFTranslate) {
return "[Action:Translate Title and Abstract] Translate for Zotero is not installed or disabled.";
}
if (!item) {
return "[Action:Translate Title and Abstract] Target item is empty";
}
const skipLang = ["", "zh", "zh-CN"];
const lang = item.getField("language");
if (skipLang.includes(lang)) {
return "[Action:Translate Title and Abstract] Skip due to language"
}
const title = item.getField("title");
const abstract = item.getField("abstractNote");
const titleResult = (await Zotero.PDFTranslate.api.translate(title)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "titleTranslation", titleResult);
let abstractResult = "";
if (abstract) {
abstractResult = (await Zotero.PDFTranslate.api.translate(abstract)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "abstractTranslation", abstractResult);
} else {
const observerID = Zotero.Notifier.registerObserver({
notify: async (event, type, ids, extraData) => {
if (event === 'modify' && type === 'item' && ids.includes(item.id)) {
const newAbstract = item.getField('abstractNote');
if (newAbstract) {
abstractResult = (await Zotero.PDFTranslate.api.translate(newAbstract)).result;
Zotero.PDFTranslate.data.ztoolkit.ExtraField.setExtraField(item, "abstractTranslation", abstractResult);
Zotero.Notifier.unregisterObserver(observerID);
}
}
}
}, ['item']);
}
return `[Action:Translate Title and Abstract] successfully translate title ${title} to ${titleResult} and abstract.`; |
Beta Was this translation helpful? Give feedback.
-
大佬,这个貌似不起作用了。无法进行自动翻译标题和摘要。麻烦大佬看看 |
Beta Was this translation helpful? Give feedback.
-
好像不能自动翻译了,有没有大佬解释以下为什么 |
Beta Was this translation helpful? Give feedback.
-
Description
Automatically translate the new item' title.
Action Settings
Event: Create Item
Operation: Script
Data:
Beta Was this translation helpful? Give feedback.
All reactions