5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DeepL API の使用方法

Last updated at Posted at 2021-10-12

auth_key と text, source_lang, target_lang を指定して GET or POST するだけ
参考: https://www.deepl.com/docs-api/introduction/

シェルスクリプト

deepl.sh
#!/bin/sh
DEEPL_API_URL="https://api-free.deepl.com/v2/translate"
YOUR_API_KEY="__DEEPL_API_KEY_HERE__"
SOURCE_TEXT="
I'm a lumberjack and I'm OK.
I sleep at night, I work during the day.

He's a lumberjack and he's OK.
He sleeps all night and he works all day.

I cut down trees, I eat my lunch.
I go to the lavatory.
On Wednesdays I go shopping and have buttered scones for tea.
"

curl -s ${DEEPL_API_URL} \
  -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "auth_key=${YOUR_API_KEY}" \
  -d "text=${SOURCE_TEXT}" \
  -d "source_lang=EN" \
  -d "target_lang=JA" \
| jq .translations[].text

Python

deepl.py
import requests

DEEPL_API_URL = 'https://api-free.deepl.com/v2/translate'
YOUR_API_KEY = '__DEEPL_API_KEY_HERE__'
SOURCE_TEXT = """
I'm a lumberjack and I'm OK.
I sleep at night, I work during the day.

He's a lumberjack and he's OK.
He sleeps all night and he works all day.

I cut down trees, I eat my lunch.
I go to the lavatory.
On Wednesdays I go shopping and have buttered scones for tea.
"""

params = {
    "auth_key": YOUR_API_KEY,
    "text": SOURCE_TEXT,
    "source_lang": 'EN',
    "target_lang": 'JA'
}
request = requests.post(DEEPL_API_URL, data=params)
result = request.json()
print(result["translations"][0]["text"])

Node.js

deepl.js
const request = require('request');

const deepl_api_url = 'https://api-free.deepl.com/v2/translate'
const your_api_key = '__DEEPL_API_KEY_HERE__';
const source_text = `
I'm a lumberjack and I'm OK.
I sleep at night, I work during the day.

He's a lumberjack and he's OK.
He sleeps all night and he works all day.

I cut down trees, I eat my lunch.
I go to the lavatory.
On Wednesdays I go shopping and have buttered scones for tea.
`;

const params = {
    url: deepl_api_url,
    method: 'POST',
    headers: {
        'content-type': 'application/x-www-form-urlencoded'
    },
    form: {
        auth_key: your_api_key,
        text: source_text,
        source_lang: 'EN',
        target_lang: 'JA'
    },
    json: true
}
request.post(params, function(error, response, result){
    console.log(result.translations[0].text);
});

PHP

deepl.php
<?php
$deepl_api_url = 'https://api-free.deepl.com/v2/translate';
$your_api_key = '__DEEPL_API_KEY_HERE__';
$source_text = "
I'm a lumberjack and I'm OK.
I sleep at night, I work during the day.

He's a lumberjack and he's OK.
He sleeps all night and he works all day.

I cut down trees, I eat my lunch.
I go to the lavatory.
On Wednesdays I go shopping and have buttered scones for tea.
";

$header = [
    'Content-Type: application/x-www-form-urlencoded',
];
$content = [
    'auth_key'    => $your_api_key,
    'text'        => $source_text,
    'source_lang' => 'EN',
    'target_lang' => 'JA',
];

$params = [
    'http' => [
        'method'  => 'POST',
        'header'  => implode("\r\n", $header),
        'content' => http_build_query($content, '', '&'),
    ]
];

$request = file_get_contents(
    $deepl_api_url,
    false,
    stream_context_create($params)
);
$result = json_decode($request);

echo $result->translations[0]->text;

実行結果

私は木こりですが、大丈夫ですよ。
夜は寝て、昼は働く。

彼は木こりであり、彼はOKだ。
彼は夜も寝るし、昼も働く。

木を切って、お昼を食べて。
お手洗いにも行く。
水曜日には買い物に行き、紅茶にバター入りのスコーンを食べる。
5
8
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
5
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?