With the increasing frequency of global communication, cross-language interaction has become essential. This article introduces how to implement Japanese to Korean translation using PHP and Baidu Translate API. We will walk through the complete steps including account registration, key acquisition, coding, and testing.
First, visit the Baidu Translate Open Platform to register a developer account and log into the console to get API access.
Create a new PHP project folder locally and add a file named translate.php in the root directory. Use Composer to install the Baidu Translate SDK by running the following command:
<span class="fun">composer require baidu-aip/php-sdk</span>
In the Baidu Translate Open Platform console, create a new application and obtain your App ID, API Key, and Secret Key. These keys will be used in your code.
Open translate.php, include the autoload file, and import the Baidu Translate class:
<?php
require_once 'vendor/autoload.php';
use AipTranslate;
$config = [
'appId' => 'your-app-id',
'apiKey' => 'your-api-key',
'secretKey' => 'your-secret-key'
];
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'];
}
$text = "こんにちは、世界!";
$translation = translate($text);
echo "Translation result: " . $translation;
Run the script in the terminal using:
<span class="fun">php translate.php</span>
If everything is configured correctly, you will see the Korean translation output in the terminal.
By following these steps, you can easily use PHP to call Baidu Translate API and achieve automated Japanese to Korean translation. This approach is also applicable to other language pairs, facilitating multilingual project development.
Related Tags:
API