As globalization continues to grow, language translation has become an essential requirement in software development. Whether for international applications or daily tools, integrating translation features is increasingly important. This article will guide you on how to use PHP and Baidu Translation API to enable Portuguese to Japanese translation.
Before we begin, make sure you meet the following conditions:
First, create a new project folder called “translator” in your PHP development environment. Inside this folder, create a new file called “translation.php”.
At the beginning of the “translation.php” file, add the following code to include the required PHP libraries:
<?php require_once('vendor/autoload.php'); use StichozaGoogleTranslate\TranslateClient;
We use the third-party library “StichozaGoogleTranslate” to implement the translation function. You may also use other libraries depending on your needs.
In the “translation.php” file, add the following code to obtain Baidu Translation API credentials:
define('API_KEY', 'YOUR_API_KEY'); define('SECRET_KEY', 'YOUR_SECRET_KEY');
Replace “YOUR_API_KEY” and “YOUR_SECRET_KEY” with the API credentials you received from Baidu.
Next, at the end of the “translation.php” file, add the following code to implement the translation feature:
// Get API Token
$response = $httpClient->get('https://openapi.baidu.com/oauth/2.0/token', [
'query' => [
'grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret,
],
]);
$result = json_decode((string) $response->getBody(), true);
$accessToken = $result['access_token'];
// Translate Text
$response = $httpClient->get('https://fanyi-api.baidu.com/api/trans/vip/translate', [
'query' => [
'q' => $text,
'from' => $from,
'to' => $to,
'appid' => $appId,
'salt' => rand(10000, 99999),
'sign' => md5($appId . $text . rand(10000, 99999) . $appSecret),
],
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);
$result = json_decode((string) $response->getBody(), true);
return $result['trans_result'][0]['dst'];
}
In this code, we define a function named “translate” that takes three parameters: the text to translate, the source language, and the target language. First, we get the API Token using the API Key and Secret Key. Then, we use the token to translate the text. Finally, the translation result is returned.
After saving the “translation.php” file, execute the following command in your terminal:
php translation.php
You should see the following output in your terminal:
Translation Result: こんにちは、世界!
This indicates that we have successfully translated the Portuguese text into Japanese.
By following the steps above, we have successfully implemented Portuguese to Japanese translation using PHP and Baidu Translation API. This feature can be widely used in multilingual applications to help users break down language barriers and facilitate cross-cultural communication. If you have any questions or need further assistance, feel free to leave a comment.
Related Tags:
API