1
1

友人の@y-vectorfieldさんがDeepL API1を紹介してくれたので、TypeScriptでやってみました。

はじめにプロジェクトのセットアップをしていきます。npm, npxを使ってプロジェクトを立ち上げます。

$ mkdir <project> && cd <project>
$ npm init -y
$ npm install typescript --save-dev
$ npm install deepl-node --save-dev
$ npx tsc --init --rootDir src --outDir dist --esModuleInterop \
  --resolveJsonModule --lib es6,dom --module commonjs

package.jsonのmain, scriptsを修正します。

package.json
  "main": "dist/deepl.js",
  "scripts": {
    "start": "npx tsc && node dist/deepl.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

src/deepl.tsを作成します。

src/deepl.ts
import * as deepl from 'deepl-node';

const authKey = "f63c02c5-f056-..."; // Replace with your key
const translator = new deepl.Translator(authKey);

(async () => {
    const result = await translator.translateText('Hello, world!', null, 'ja');
    console.log(result.text);
})();

npm startでコンパイル・実行します。

$ npm start
:
こんにちは、世界よ!

できました👏
いつも有益な情報をくれるy-vectorfieldさんには感謝しています。

npm: 10.2.4

  1. https://developers.deepl.com/docs/v/ja

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1