-
-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
62f343a
commit 0714466
Showing
4 changed files
with
54 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
import { SERVICES } from "./utils/config"; | ||
import { getPref } from "./utils/prefs"; | ||
import { TranslateTask } from "./utils/task"; | ||
import { version } from "../package.json"; | ||
|
||
/** | ||
* To plugin developers: Please use this API to translate your custom text. | ||
* | ||
* @param raw raw text for translation. | ||
* @param service service id. See src/utils/config.ts > SERVICES | ||
* If not provided, the default service will be used. | ||
* If you want to use multiple services, please provide an array of service ids. | ||
* The first service in the array will be used as the default service. | ||
* Others will be used as fallback services. | ||
* @returns TranslateTask object. | ||
*/ | ||
async function translate(raw: string, service?: string) { | ||
async function translate(raw: string, service?: string | string[]) { | ||
let currentService: string; | ||
let candidateServices: string[] = []; | ||
if (typeof service === "string") { | ||
currentService = service; | ||
} else if (Array.isArray(service)) { | ||
currentService = service[0]; | ||
candidateServices = service.slice(1); | ||
} else { | ||
currentService = getPref("translateSource") as string; | ||
} | ||
|
||
const data: TranslateTask = { | ||
id: `${Zotero.Utilities.randomString()}-${new Date().getTime()}`, | ||
type: "custom", | ||
raw, | ||
result: "", | ||
audio: [], | ||
service: service || (getPref("translateSource") as string), | ||
candidateServices: [], | ||
service: currentService, | ||
candidateServices, | ||
itemId: -1, | ||
status: "waiting", | ||
extraTasks: [], | ||
silent: true, | ||
}; | ||
await addon.data.translate.services.runTranslationTask(data, { | ||
noDisplay: true, | ||
}); | ||
return data; | ||
} | ||
|
||
export default { translate }; | ||
/** | ||
* Get all available services. | ||
* @returns Array of services. | ||
*/ | ||
function getServices() { | ||
return SERVICES.map((service) => Object.assign({}, service)); | ||
} | ||
|
||
/** | ||
* Get version of the plugin. | ||
* @returns Version of the plugin. | ||
*/ | ||
function getVersion() { | ||
return version; | ||
} | ||
|
||
export default { | ||
translate, | ||
getServices, | ||
getVersion, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters