当前位置:首页 > PHP

php 实现翻译

2026-01-29 12:42:49PHP

使用 Google Translate API

PHP 可以通过调用 Google Translate API 实现翻译功能。需要注册 Google Cloud 账号并获取 API 密钥。

$apiKey = 'YOUR_GOOGLE_API_KEY';
$text = 'Hello, world!';
$targetLanguage = 'zh';

$url = "https://translation.googleapis.com/language/translate/v2?key=$apiKey";
$data = [
    'q' => $text,
    'target' => $targetLanguage
];

$options = [
    'http' => [
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => http_build_query($data)
    ]
];

$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$result = json_decode($response, true);

echo $result['data']['translations'][0]['translatedText'];

使用 Microsoft Translator API

Microsoft Azure 也提供翻译服务,需要注册 Azure 账号并获取 API 密钥。

php 实现翻译

$apiKey = 'YOUR_AZURE_API_KEY';
$text = 'Hello, world!';
$targetLanguage = 'zh-Hans';

$url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=$targetLanguage";
$headers = [
    'Ocp-Apim-Subscription-Key: ' . $apiKey,
    'Content-Type: application/json'
];

$body = json_encode([['Text' => $text]]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$result = json_decode($response, true);
echo $result[0]['translations'][0]['text'];

使用本地翻译库

对于不需要实时联网的场景,可以使用本地翻译库如 PHP-Google-Translate-Free。

php 实现翻译

require 'vendor/autoload.php';
use Statickidz\GoogleTranslate;

$source = 'en';
$target = 'zh';
$text = 'Hello, world!';

$trans = new GoogleTranslate();
$result = $trans->translate($source, $target, $text);

echo $result;

使用数据库存储翻译

对于多语言网站,可以在数据库中存储翻译内容。

$pdo = new PDO('mysql:host=localhost;dbname=translations', 'username', 'password');
$text = 'Welcome';
$language = 'fr';

$stmt = $pdo->prepare("SELECT translation FROM translations WHERE original = ? AND language = ?");
$stmt->execute([$text, $language]);
$result = $stmt->fetch();

echo $result['translation'];

使用 gettext 扩展

PHP 内置的 gettext 扩展适合本地化应用程序。

putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');
bindtextdomain('messages', './locale');
textdomain('messages');

echo _("Hello, world!");

每种方法适用于不同场景,API 方式适合需要高质量实时翻译,本地库适合离线环境,数据库方式适合网站多语言支持,gettext 适合应用程序本地化。

标签: php
分享给朋友:

相关文章

php实现增删改查

php实现增删改查

以下是在PHP中实现增删改查(CRUD)操作的详细方法,以MySQL数据库为例: 数据库连接配置 创建config.php文件存储数据库连接信息: <?php $host = 'localh…

php实现排序

php实现排序

PHP 数组排序方法 PHP 提供了多种内置函数用于数组排序,适用于不同的排序需求。以下是常见的排序方法: 按值升序排序 sort() 函数对数组按值升序排序,重置键名(索引数组适用): $num…

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inter…

php 实现排序

php 实现排序

PHP 实现数组排序的方法 PHP 提供了多种内置函数和方法来实现数组排序,适用于不同的排序需求。以下是一些常见的排序实现方式: 使用 sort() 函数对数组进行升序排序 sort() 函数对数组…

php 实现mvc

php 实现mvc

PHP 实现 MVC 架构的方法 MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、用户界面和用户输入。以下是 PHP 实现 MVC 架构的关键步骤:…

php实现文章

php实现文章

PHP实现文章管理功能 在PHP中实现文章管理功能通常涉及数据库操作、表单处理和页面展示。以下是实现文章管理的基本步骤: 数据库设计 创建articles表,包含字段如id、title、conten…