php 实现翻译
PHP 实现翻译的方法
使用 Google Translate API
Google Translate API 是一种付费服务,提供高质量的翻译功能。需要注册 Google Cloud 账号并获取 API 密钥。
<?php
function translateText($text, $targetLanguage, $apiKey) {
$url = "https://translation.googleapis.com/language/translate/v2?key=" . $apiKey;
$data = array(
'q' => $text,
'target' => $targetLanguage
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
return $response['data']['translations'][0]['translatedText'];
}
$apiKey = 'YOUR_GOOGLE_API_KEY';
$text = 'Hello, world!';
$translatedText = translateText($text, 'es', $apiKey);
echo $translatedText;
?>
使用 Microsoft Translator API
Microsoft Translator API 是另一种付费翻译服务,提供多语言支持。
<?php
function microsoftTranslate($text, $targetLanguage, $apiKey) {
$url = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=' . $targetLanguage;
$headers = array(
'Ocp-Apim-Subscription-Key: ' . $apiKey,
'Content-Type: application/json'
);
$data = array(array('Text' => $text));
$options = array(
'http' => array(
'header' => implode("\r\n", $headers),
'method' => 'POST',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result, true);
return $response[0]['translations'][0]['text'];
}
$apiKey = 'YOUR_MICROSOFT_API_KEY';
$text = 'Hello, world!';
$translatedText = microsoftTranslate($text, 'fr', $apiKey);
echo $translatedText;
?>
使用免费翻译 API (MyMemory)
MyMemory 是一个免费的翻译 API,适合小规模翻译需求。
<?php
function myMemoryTranslate($text, $targetLanguage) {
$url = 'https://api.mymemory.translated.net/get?q=' . urlencode($text) . '&langpair=en|' . $targetLanguage;
$result = file_get_contents($url);
$response = json_decode($result, true);
return $response['responseData']['translatedText'];
}
$text = 'Hello, world!';
$translatedText = myMemoryTranslate($text, 'de');
echo $translatedText;
?>
使用本地翻译库 (Stichoza/GoogleTranslate)
Stichoza/GoogleTranslate 是一个 PHP 库,模拟 Google 翻译的网页版请求,无需 API 密钥。
安装库:
composer require stichoza/google-translate-php
使用示例:
<?php
require 'vendor/autoload.php';
use Stichoza\GoogleTranslate\GoogleTranslate;
$tr = new GoogleTranslate('es');
echo $tr->translate('Hello, world!');
?>
使用数据库存储翻译
对于多语言网站,可以将翻译内容存储在数据库中,通过 PHP 动态加载。
数据库表结构示例:
CREATE TABLE translations (
id INT AUTO_INCREMENT PRIMARY KEY,
key VARCHAR(255) NOT NULL,
language VARCHAR(10) NOT NULL,
value TEXT NOT NULL
);
PHP 查询示例:

<?php
function getTranslation($key, $language) {
$pdo = new PDO('mysql:host=localhost;dbname=your_db', 'username', 'password');
$stmt = $pdo->prepare('SELECT value FROM translations WHERE key = ? AND language = ?');
$stmt->execute([$key, $language]);
$result = $stmt->fetch();
return $result ? $result['value'] : $key;
}
echo getTranslation('welcome_message', 'fr');
?>
注意事项
- 付费 API 如 Google 和 Microsoft 需要注册并获取 API 密钥。
- 免费 API 可能有请求限制,不适合大规模使用。
- 本地翻译库可能违反服务条款,需谨慎使用。
- 数据库存储翻译适合静态内容,动态内容需结合 API 使用。






