DeepL API の使用方法

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

## シェルスクリプト

```bash: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

```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

```javascript: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

```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だ。
彼は夜も寝るし、昼も働く。

木を切って、お昼を食べて。
お手洗いにも行く。
水曜日には買い物に行き、紅茶にバター入りのスコーンを食べる。
```