Skip to content

Latest commit

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Langbly PHP SDK

Official PHP client for the Langbly translation API. Drop-in replacement for Google Translate v2.

Latest Stable VersionLicense

Installation

composer require langbly/langbly-php

Requires PHP 7.4+ and Guzzle 7.

Quick Start

<?phprequire_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 ($resultsas$t) {
echo$t->text . "\n";
}
// Detect language$detection = $client->detect('Bonjour');
echo$detection->language; // "fr"// List languages$languages = $client->languages('en');
foreach ($languagesas$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
);
ParameterTypeDefaultDescription
apiKeystringrequiredYour Langbly API key
baseUrlstringhttps://api.langbly.comAPI base URL
timeoutfloat30Request timeout in seconds
maxRetriesint2Retries 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');
ParameterTypeRequiredDescription
textstring/string[]yesText or array of texts
targetstringyesTarget language code
sourcestring/nullnoSource language (auto-detected)
formatstring/nullno"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

useLangbly\Exceptions\LangblyException;
useLangbly\Exceptions\RateLimitException;
useLangbly\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 nullecho"Rate limited. Retry after {$retryAfter}s";
} catch (LangblyException$e) {
// Other API errorsecho"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.phpuseLangbly\ClientasLangblyClient;
publicfunctionregister()
{
$this->app->singleton(LangblyClient::class, function () {
returnnewLangblyClient(config('services.langbly.api_key'));
});
}
// In a controllerpublicfunctiontranslate(Request$request, LangblyClient$langbly)
{
$result = $langbly->translate($request->input('text'), 'nl');
returnresponse()->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

About

Official PHP SDK for the Langbly Translation API. Drop-in replacement for Google Translate v2.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages