With the increasing globalization, language translation plays a vital role in international communication. This article explains how to combine PHP with Baidu Translate API to achieve automatic translation from Japanese to Korean, making it easier for developers to integrate multi-language support into their projects.
First, visit Baidu Translate Open Platform to register as a developer and log into the console. After registration, create an application and obtain the necessary API keys.
Create a PHP project folder locally and create a file named translate.php in the project root directory. Use Composer to install Baidu Translate API SDK with the following command:
composer require baidu-aip/php-sdk
After logging into the console, obtain your appId, apiKey, and secretKey. Define these in your code configuration:
<?php // Baidu Translate API configuration $config = [ 'appId' => 'your-app-id', 'apiKey' => 'your-api-key', 'secretKey' => 'your-secret-key' ]; ?>
In translate.php, include the SDK and implement the translation function:
<?php require_once 'vendor/autoload.php'; use AipTranslate; function translate($text) { global $config; $client = new AipTranslate($config['appId'], $config['apiKey'], $config['secretKey']); $result = $client->trans($text, 'jp', 'ko'); return $result['trans_result'][0]['dst']; } ?>
Test the translation function by calling it and outputting the result:
<?php $text = "こんにちは、世界!"; $translation = translate($text); echo "Translation result: " . $translation; ?>
Execute the script via command line:
php translate.php
If successful, the terminal will display the Korean translation of the input text.
This article covered how to use PHP with Baidu Translate API to achieve automatic translation from Japanese to Korean. The process includes account registration, environment setup, coding, and testing. Using this approach, developers can easily extend support for more languages and enhance the internationalization of their applications.