Node.js Plugin for Utilizing DeepL Translation API in GoldenDict/GoldenDict-ng
- Published on
- Authors
- Name
- Jerry
I'm excited to introduce to you a Node.js plugin I recently developed that enables seamless integration of DeepL's translation service into GoldenDict/GoldenDict-ng, a cross-platform open-source dictionary application. GoldenDict/GoldenDict-ng can be used on Windows, macOS, and Linux systems, providing a versatile solution for translation needs.
This plugin harnesses the power of DeepL's API, a highly acclaimed translation service provider. Since the API for free users comes with monthly usage limits, I've added a feature in the plugin that automatically checks the remaining free usage after each translation request, ensuring you're always aware of your usage.
In developing this plugin, I employed various technical approaches to enhance user experience. Firstly, I used regular expressions to determine if the input content is in Chinese. If it is, the content is translated into English; otherwise, it's translated into Chinese. This makes it effortless to translate Chinese into other languages or vice versa.
Furthermore, to protect sensitive information, I stored DeepL's AUTH_KEY in a separate .env file and imported it into my code as an environment variable using the dotenv package, ensuring security and maintainability.
To use this plugin in GoldenDict/GoldenDict-ng, you need to install Node.js, git clone or download the files and unzip. Then under the project folder, npm ci to install the dependency packages.
In GoldenDict/GoldenDict-ng, press F3 or open Dictionaries settings. Add the following settings in the programs panel, the path will be different depends on where you store the script.
If you're interested in this plugin, feel free to check out the source code on my GitHub repository and provide valuable feedback. I hope this plugin brings you a more convenient and intelligent translation experience to your dictionary application!
const axios = require('axios')
require('dotenv').config({ path: __dirname + '/.env' })
// Get command line arguments
const arg = process.argv.slice(2)[0] // The real command line arguments start from the third element
// Use regular expression to match Chinese characters
const isChinese = /[\u4e00-\u9fa5]/.test(arg)
const input = arg.trim()
if (isChinese || input.split(' ').length > 1) {
const target_lang = isChinese ? 'EN-US' : 'ZH'
const data = JSON.stringify({
text: [input],
target_lang: target_lang,
})
const translate_req = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api-free.deepl.com/v2/translate',
headers: {
Authorization: `DeepL-Auth-Key ${process.env.DEEPL_AUTH_KEY}`,
'Content-Type': 'application/json',
},
data: data,
}
const usage_req = {
method: 'get',
maxBodyLength: Infinity,
url: 'https://api-free.deepl.com/v2/usage',
headers: {
Authorization: `DeepL-Auth-Key ${process.env.DEEPL_AUTH_KEY}`,
},
}
Promise.all([axios.request(translate_req), axios.request(usage_req)])
.then(([translate_res, usage_res]) => {
const translate_data = translate_res.data.translations[0]
const detected_source_language = translate_data.detected_source_language
const text = translate_data.text
const character_count = usage_res.data.character_count
const character_limit = usage_res.data.character_limit
console.log(`[${detected_source_language}] ${input} <hr> [${target_lang}] ${text}`)
console.log(`
<div style="text-align: end; padding: 5px 5px 0;">Usage: ${character_count}/${character_limit}</div>
`)
})
.catch((error) => {
console.log(error)
})
}