langbly/langbly-php
Composer 安装命令:
composer require langbly/langbly-php
包简介
Official PHP SDK for the Langbly translation API. Drop-in replacement for Google Translate v2.
README 文档
README
Official PHP client for the Langbly translation API. Drop-in replacement for Google Translate v2.
Installation
composer require langbly/langbly-php
Requires PHP 7.4+ and Guzzle 7.
Quick Start
<?php require_once 'vendor/autoload.php'; $client = new \Langbly\Client('your-api-key'); // Translate text $result = $client->translate('Hello, world!', 'nl'); echo $result->text; // "Hallo, wereld!" // Batch translate $results = $client->translate(['Hello', 'Goodbye'], 'de'); foreach ($results as $t) { echo $t->text . "\n"; } // Detect language $detection = $client->detect('Bonjour'); echo $detection->language; // "fr" // List languages $languages = $client->languages('en'); foreach ($languages as $lang) { echo "{$lang->code}: {$lang->name}\n"; }
API Reference
Constructor
$client = new \Langbly\Client( apiKey: 'your-api-key', baseUrl: 'https://api.langbly.com', // optional timeout: 30.0, // optional, seconds maxRetries: 2 // optional );
| Parameter | Type | Default | Description |
|---|---|---|---|
apiKey |
string | required | Your Langbly API key |
baseUrl |
string | https://api.langbly.com |
API base URL |
timeout |
float | 30 |
Request timeout in seconds |
maxRetries |
int | 2 |
Retries for transient errors (429, 5xx) |
translate()
// Single string $result = $client->translate('Hello', 'nl'); // Returns: Translation { text, source, model } // Batch $results = $client->translate(['Hello', 'Goodbye'], 'nl'); // Returns: Translation[] // With options $result = $client->translate('Hello', 'nl', 'en', 'html');
| Parameter | Type | Required | Description |
|---|---|---|---|
text |
string/string[] | yes | Text or array of texts |
target |
string | yes | Target language code |
source |
string/null | no | Source language (auto-detected) |
format |
string/null | no | "text" or "html" |
detect()
$detection = $client->detect('Bonjour le monde'); echo $detection->language; // "fr" echo $detection->confidence; // 0.98
languages()
// All languages $languages = $client->languages(); // With localized names $languages = $client->languages('en'); echo $languages[0]->code; // "af" echo $languages[0]->name; // "Afrikaans"
Error Handling
use Langbly\Exceptions\LangblyException; use Langbly\Exceptions\RateLimitException; use Langbly\Exceptions\AuthenticationException; try { $result = $client->translate('Hello', 'nl'); } catch (AuthenticationException $e) { // Invalid API key (401) echo "Auth error: " . $e->getMessage(); } catch (RateLimitException $e) { // Too many requests (429) $retryAfter = $e->getRetryAfter(); // seconds, or null echo "Rate limited. Retry after {$retryAfter}s"; } catch (LangblyException $e) { // Other API errors echo "Error ({$e->getStatusCode()}): " . $e->getMessage(); }
The client automatically retries transient errors (429, 500, 502, 503, 504) with exponential backoff. It respects the Retry-After header when present.
EU Data Residency
For EU-only data processing, use the EU endpoint:
$client = new \Langbly\Client('your-api-key', 'https://eu.langbly.com');
Laravel Integration
// config/services.php 'langbly' => [ 'api_key' => env('LANGBLY_API_KEY'), ], // app/Providers/AppServiceProvider.php use Langbly\Client as LangblyClient; public function register() { $this->app->singleton(LangblyClient::class, function () { return new LangblyClient(config('services.langbly.api_key')); }); } // In a controller public function translate(Request $request, LangblyClient $langbly) { $result = $langbly->translate($request->input('text'), 'nl'); return response()->json(['translated' => $result->text]); }
Migrating from Google Translate
If you're currently using Google Cloud Translation, see the migration guide.
The API format is identical. Change the base URL and authentication:
// Before (Google) $client->post('https://translation.googleapis.com/language/translate/v2', [ 'headers' => ['Authorization' => 'Bearer ' . $googleToken], 'json' => ['q' => 'Hello', 'target' => 'nl'], ]); // After (Langbly SDK) $langbly = new \Langbly\Client($apiKey); $result = $langbly->translate('Hello', 'nl');
Links
License
MIT
langbly/langbly-php 适用场景与选型建议
langbly/langbly-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「translation」 「api」 「localization」 「translate」 「google-translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 langbly/langbly-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 langbly/langbly-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 langbly/langbly-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
A Laravel Eloquent model trait for translatable resource
A PSR-7 compatible library for making CRUD API endpoints
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
Nette Framework adapter for I18n package
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-19