DeepLのAPIとGoldenDictを用いた自動翻訳辞書の制作

IP Tips

テキストのマーキングでWeb辞書を引ける便利ソフト、GoldenDict
ここに AI翻訳で有名なDeepLのAPIをくっつけて、自動翻訳を叶えましたゾ!!
※1 Windows,Mac,Linux対応
※2 DeepL APIキーを用意しておくこと。筆者はAPI Proプランがオススメです

じゃあ早速Node JSで作ってみましょうか。

  1. node JSをインストール
  2. npm install -g deepl-node express cors
  3. const express = require('express');
    const deepl = require('deepl-node');
    const cors = require('cors');
    
    const app = express();
    const PORT = 3000; // 任意のポート番号
    const apiKey = '<DEEPL_API_KEY>';  // DeepL APIのキーを入力
    
    // DeepL APIクライアントを作成
    const translator = new deepl.Translator(apiKey);
    
    // CORS対応
    app.use(cors());
    
    // 翻訳APIラッパー
    app.get('/translate', async (req, res) => {
      const text = decodeURIComponent(req.query.text);
      if (!text) {
        return res.status(400).send('No text provided for translation.');
      }
    
      try {
        // DeepL APIを使って翻訳
        const result = await translator.translateText(text, null, 'ja');
        const translatedText = result.text;
    
        // HTMLレスポンスを返す
        res.send(`${translatedText}`);
      } catch (error) {
        console.error(error);
        res.status(500).send('Translation failed.');
      }
    });
    
    app.listen(PORT, () => {
      console.log(`Server is running on http://localhost:${PORT}`);
    });
    
    
  4. 3.で作成したnodeJSをスタートアップに登録+実行
    node /path/to/deepl_api.js
  5. GoldenDict→編集→辞書→Web辞書に下記を登録
    DeepL API: http://localhost:3000/translation?text=%GDWORD%
    有効のみにチェック
  6. 言葉の壁を払った世界へようこそ^^

翻訳はコチラの記事で試してみてネ!!

コメント

タイトルとURLをコピーしました