C:blanc
Published
March 9 2023
Updated
December 22 2023
Blog PHP

I have introduced DeepL API to my blog.

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

Registration

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.

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

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

mounting

The official library was published.
https://github.com/DeepLcom/deepl-php

It was easier than 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" is a glossary.
To use a glossary, set $glossaryId to glossaryId.

Translation Results

good morning
good evening
good-bye
Is it OK if the first letter is not capitalized?

How to create a glossary

For reference, I will also write how to create a glossary.
The process would be Delete → Create.

The content of glossaryId changes each time it is created.
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 files may not be recognized if they are only a few lines long.
(Google Translate API did, I haven't tried it with DeepL)
jatoen.csv
"こんちくわ","Hello"
"ちはー","Hello"
Below are the translated results.
https://blog.c-blanc.com/en/?article=10

How about those who are familiar with English?

Comparison

ソース(日本語)
↓↓↓
今回は 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
I am a freelance programmer.
  • COMMENT

    Submitted. Comments will appear after approval.
  • あと150more characters