目次
サンプル・プログラムの実行例

サンプル・プログラム
translate.php | サンプル・プログラム本体 |
バージョン | 更新日 | 内容 |
---|---|---|
2.0.1 | 2024/09/23 | 参考URL変更 |
1.0.0 | 2023/03/04 | Google Cloud Translate,DeepL APIに対応 |
準備:クラウド翻訳サービス選択
148: //クラウド翻訳サービス選択
149: //利用しないクラウド翻訳サービスはコメントアウトすること.
150: var $apiTable = array(
151: //値 タイトル 翻訳メソッド
152: 'google' => array('title'=>'Google翻訳', 'func'=>'googleCloudTranslate'),
153: 'deepl' => array('title'=>'DeepL翻訳', 'func'=>'deepLTranslate'),
154: );
複数のクラウド翻訳サービスを使い分けることができるが、もし利用しないのであれば、配列 $apiTable で使わないサービスの行をコメントアウトすればよい。
準備:Google Cloud Translation API

このAPIは、1ヶ月当たり最初の50万文字までは無料だが、それ以上は有料になる。
137: //Google Cloud Platform APIキー
138: //https://cloud.google.com/maps-platform/ から無料申込み.
139: //1ヶ月当たり最初の50万文字までは無料だが、それ以上は有料になる.
140: //利用しないのなら未定義のままでよい.
141: var $GOOGLE_API_KEY = '**************************************';
なお、Google Cloud Translation API を利用しないのであれば、代入する必要はない。
仕様:Google Cloud Translation API v2
URL |
---|
https://translation.googleapis.com/language/translate/v2 |
フィールド名 | 要否 | 内 容 |
---|---|---|
q | 必須 | 翻訳したいテキスト(UTF-8) |
target | 必須 | ターゲット(翻訳先)言語(ISO-639コード) |
key | 必須 | APIキー |
format | 任意 | html | text(デフォルト) |
source | 任意 | 翻訳したいテキストの言語(ISO-639コード) |
model | 任意 | 翻訳モデル。nmt固定 |
解説:Google Cloud Translation API v2
253: /**
254: * Google Cloud Translate APIを使って翻訳を行う.
255: * @param string $text 翻訳したいテキスト
256: * @param string $targetLanguage 翻訳したい言語
257: * @return string 翻訳テキスト/FALSE:失敗
258: */
259: function googleCloudTranslate($text, $targetLanguage='ja') {
260: $this->webapi = 'https://translation.googleapis.com/language/translate/v2';
261: $data = array(
262: 'q' => $text,
263: 'target' => $targetLanguage,
264: 'key' => $this->GOOGLE_API_KEY,
265: );
266:
267: //Google Cloud Translate APIを呼び出す.
268: $ch = curl_init();
269: curl_setopt($ch, CURLOPT_URL, $this->webapi);
270: curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
271: curl_setopt($ch, CURLOPT_POST, TRUE);
272: curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
273:
274: curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
275: curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
276: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
277: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
278:
279: $response = curl_exec($ch);
280: curl_close($ch);
281:
282: //APIエラー
283: if ($response == FALSE) {
284: $this->errmsg = 'Google Cloud Translate APIにアクセスできません';
285: $ret = FALSE;
286: } else {
287: $arr = json_decode($response, TRUE);
288: if ($arr == FALSE) {
289: $this->errmsg = 'Google Cloud Translate APIの応答が不正です';
290: $ret = FALSE;
291: //翻訳成功
292: } else if (isset($arr['data']['translations'][0]['translatedText'])) {
293: $ret = (string)$arr['data']['translations'][0]['translatedText'];
294: } else {
295: $this->errmsg = 'Google Cloud Translate APIの応答が不正です';
296: $ret = FALSE;
297: }
298: }
299:
300: return $ret;
301: }
API呼び出しには cURL関数を用いた。
準備:DeepL API



137: //Google Cloud Platform APIキー
138: //https://cloud.google.com/maps-platform/ から無料申込み.
139: //1ヶ月当たり最初の50万文字までは無料だが、それ以上は有料になる.
140: //利用しないのなら未定義のままでよい.
141: var $GOOGLE_API_KEY = '**************************************';
142:
143: //DeepL APIキー
144: //https://www.deepl.com/ja/docs-api から無料申込み.
145: //利用しないのなら未定義のままでよい.
146: var $DEEPL_API_KEY = '****************************************';
なお、DeepL API を利用しないのであれば、代入する必要はない。
仕様:DeepL API v2
URL |
---|
https://api-free.deepl.com/v2/translate |
フィールド名 | 要否 | 内 容 |
---|---|---|
text | 必須 | 翻訳したいテキスト(UTF-8) |
target_lang | 必須 | ターゲット(翻訳先)言語(英大文字) |
auth_key | 必須 | APIキー |
解説:DeepL API v2
303: /**
304: * DeepL APIを使って翻訳を行う.
305: * @param string $text 翻訳したいテキスト
306: * @param string $targetLanguage 翻訳したい言語
307: * @return string 翻訳テキスト/FALSE:失敗
308: */
309: function deepLTranslate($text, $targetLanguage='ja') {
310: $this->webapi = 'https://api-free.deepl.com/v2/translate';
311: $data = array(
312: 'text' => $text,
313: 'target_lang' => strtoupper($targetLanguage),
314: 'auth_key' => $this->DEEPL_API_KEY,
315: );
316:
317: //Google Cloud Translate APIを呼び出す.
318: $ch = curl_init();
319: curl_setopt($ch, CURLOPT_URL, $this->webapi);
320: curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
321: curl_setopt($ch, CURLOPT_POST, TRUE);
322: curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
323:
324: curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
325: curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
326: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
327: curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
328:
329: $response = curl_exec($ch);
330: curl_close($ch);
331:
332: //APIエラー
333: if ($response == FALSE) {
334: $this->errmsg = 'DeepL APIにアクセスできません';
335: $ret = FALSE;
336: } else {
337: $arr = json_decode($response, TRUE);
338: if ($arr == FALSE) {
339: $this->errmsg = 'DeepL APIの応答が不正です';
340: $ret = FALSE;
341: //翻訳成功
342: } else if (isset($arr['translations'][0]['text'])) {
343: $ret = (string)$arr['translations'][0]['text'];
344: } else {
345: $this->errmsg = 'DeepL APIの応答が不正です';
346: $ret = FALSE;
347: }
348: }
349:
350: return $ret;
351: }