C:blanc
投稿日
2023年03月09日
更新日
2023年12月22日
Blog PHP

ブログに DeepL API を導入してみました。

  • このエントリーをはてなブックマークに追加

登録

今回は DeepL API Free を利用します。
1ヶ月に500,000文字まで翻訳できます。
用語集は1,000個作れるみたいですが、本ブログではほとんど使用しないと思います。

無料プランでもクレジットカードの登録が必要です。
JCBカードは使えませんでした。

https://www.deepl.com/pro-api?cta=header-pro-api

登録が完了したら、APIで使用する認証キーが発行されます。

実装

公式のライブラリーが公開されていました。
https://github.com/DeepLcom/deepl-php

Google Transelate API より簡単でした。
Terminal
composer require deeplcom/deepl-php
PHP
require_once __DIR__ . '/vendor/autoload.php';
$translator = new \DeepL\Translator(AUTH_KEY);

$options = [];
$options['tag_handling'] = 'html';
$options['split_sentences'] = '1';
// $options['glossary'] = $glossaryId;

$contents = [
'おはようございます',
'こんばんは',
'さようなら',
];

try {
	$results = $translator->translateText($contents, 'ja', 'en-US', $options);

	if (! empty($results)) {
		foreach ($results as $value) {
			echo $value->text , '<br>';
		}
	}
} catch (\DeepL\DeepLException $error) {
	echo ($error->getMessage() ?? 'unknown error');
}
「glossary」は用語集のことです。
用語集を使用する場合は、$glossaryId に glossaryId を設定します。

翻訳結果

good morning
good evening
good-bye
1文字目が大文字でなくていいのかな?

用語集作成方法

参考までに用語集の作成方法も書いておきます。
削除 → 作成という工程になると思います。

glossaryId は作成される度に内容が変わります。
PHP
require_once __DIR__ . '/vendor/autoload.php';
$translator = new \DeepL\Translator(AUTH_KEY);

// DELETE
$glossaries = $translator->listGlossaries();
foreach ($glossaries as $glossary) {
	$translator->deleteGlossary($glossary);
}

// CREATE
$myCsvGlossary = $translator->createGlossaryFromCsv(
'jatoen',
'ja',
'en',
file_get_contents(__DIR__ . '/jatoen.csv')
);

$myCsvGlossary = $translator->createGlossaryFromCsv(
'entoja',
'en',
'ja',
file_get_contents(__DIR__ . '/entoja.csv')
);

$glossaries = $translator->listGlossaries();

foreach ($glossaries as $glossary) {
	$glossary->glossaryId;
	$glossary->name;
}
CSVファイルは数行程度だと認識されないかもしれません。
(Google Translate API はそうでした。DeepLの場合は試していません。)
jatoen.csv
"こんちくわ","Hello"
"ちはー","Hello"
下記が翻訳された結果です。
https://blog.c-blanc.com/en/?article=10

英語に詳しい方、いかがでしょうか?

比較

ソース(日本語)
↓↓↓
今回は DeepL API Free を利用します。
1ヶ月に500,000文字まで翻訳できます。
用語集は1,000個作れるみたいですが、本ブログではほとんど使用しないと思います。

無料プランでもクレジットカードの登録が必要です。
JCBカードは使えませんでした。

登録が完了したら、APIで使用する認証キーが発行されます。


DeepL
↓↓↓
In this case, we will use DeepL API Free.
You can translate up to 500,000 words per month.
It seems that 1,000 glossaries can be created, but we will hardly use them in this blog.

You need to register a credit card even for the free plan.
JCB card was not accepted.

Once registration is complete, you will be issued an authentication key to use with the API.


Google Translate
↓↓↓
This time we will use DeepL API Free.
You can translate up to 500,000 characters per month.
It seems that 1,000 glossaries can be created, but I don't think I will use them in this blog.

A credit card is required even for the free plan.
I couldn't use my JCB card.

After registration is complete, you will be issued an authentication key to use with the API.

PROFILE

C:blanc
フリーランスのプログラマーです。
  • COMMENT

    送信しました! コメントは承認後表示されます!
  • あと150文字