Official PHP SDK for Lingo.dev, a powerful localization engine that supports various content types including plain text, objects, and chat sequences.
You can install the SDK via Composer:
composer require lingodotdev/sdk
After installing the package, bootstrap the engine with your API key:
require 'vendor/autoload.php';
use LingoDotDev\Sdk\LingoDotDevEngine;
$engine = new LingoDotDevEngine([
'apiKey' => 'your-api-key', // replace with your actual key
]);
- Text Localization
- Object Localization
- Chat Localization
- Batch Localization
- Language Detection
- Progress Tracking
- PHP 8.1 or higher
- Composer
- GuzzleHttp Client
- Respect Validation
Follow these steps to create a new PHP project that uses the Lingo.dev SDK:
-
Create a project directory:
mkdir my-lingo-project cd my-lingo-project
-
Initialize Composer:
composer init --name=your-vendor/your-project --description="Your project description" --type=project --require="php:^8.1" --author="Your Name <[email protected]>"
-
Add Lingo.dev SDK as a dependency:
composer require lingodotdev/sdk
<?php
use LingoDotDev\Sdk\LingoDotDevEngine;
// Initialize the SDK with your API key
$engine = new LingoDotDevEngine([
'apiKey' => 'your-api-key',
]);
Translate a simple text string from one language to another:
// Localize a text string from English to Spanish
$localizedText = $engine->localizeText('Hello, world!', [
'sourceLocale' => 'en',
'targetLocale' => 'es',
]);
// Output: "¡Hola, mundo!"
Translate an array of strings while preserving the structure:
// Localize an object from English to French
$localizedObject = $engine->localizeObject([
'greeting' => 'Hello',
'farewell' => 'Goodbye',
'messages' => [
'welcome' => 'Welcome to our service',
'thanks' => 'Thank you for your business'
]
], [
'sourceLocale' => 'en',
'targetLocale' => 'fr',
]);
/* Output:
[
'greeting' => 'Bonjour',
'farewell' => 'Au revoir',
'messages' => [
'welcome' => 'Bienvenue dans notre service',
'thanks' => 'Merci pour votre confiance'
]
]
*/
Translate a chat conversation while preserving speaker names:
// Localize a chat conversation from English to German
$localizedChat = $engine->localizeChat([
['name' => 'Alice', 'text' => 'Hello, how are you?'],
['name' => 'Bob', 'text' => 'I am fine, thank you!'],
['name' => 'Alice', 'text' => 'What are you doing today?']
], [
'sourceLocale' => 'en',
'targetLocale' => 'de',
]);
/* Output:
[
['name' => 'Alice', 'text' => 'Hallo, wie geht es dir?'],
['name' => 'Bob', 'text' => 'Mir geht es gut, danke!'],
['name' => 'Alice', 'text' => 'Was machst du heute?']
]
*/
Detect the language of a given text:
// Detect language
$locale = $engine->recognizeLocale('Bonjour le monde');
// Output: "fr"
Translate a text to multiple languages at once:
// Batch localize text to multiple languages
$localizedTexts = $engine->batchLocalizeText('Hello, world!', [
'sourceLocale' => 'en',
'targetLocales' => ['es', 'fr', 'de', 'it'],
]);
/* Output:
[
"¡Hola, mundo!",
"Bonjour le monde!",
"Hallo, Welt!",
"Ciao, mondo!"
]
*/
Track the progress of a localization operation:
// Localize with progress tracking
$engine->localizeText('Hello, world!', [
'sourceLocale' => 'en',
'targetLocale' => 'es',
], function ($progress, $chunk, $processedChunk) {
echo "Localization progress: $progress%\n";
});
If you prefer to start with a minimal example instead of the detailed scenarios above, create index.php in an empty folder, copy the following snippet, install dependencies with composer require lingodotdev/sdk
, set LINGODOTDEV_API_KEY
, and run php index.php
.
Want to see everything in action?
- Clone this repository or copy the
index.php
from the demo below into an empty directory. - Run
composer install
to pull in the SDK. - Populate the
LINGODOTDEV_API_KEY
environment variable with your key. - Execute the script with
php index.php
and observe the output.
index.php
demo:
<?php
require 'vendor/autoload.php';
use LingoDotDev\Sdk\LingoDotDevEngine;
$engine = new LingoDotDevEngine([
'apiKey' => getenv('LINGODOTDEV_API_KEY'),
]);
// 1. Text
$helloEs = $engine->localizeText('Hello world!', [
'sourceLocale' => 'en',
'targetLocale' => 'es',
]);
echo "Text ES ⇒ $helloEs\n\n";
// 2. Object
$object = [
'greeting' => 'Good morning',
'farewell' => 'Good night',
];
$objectFr = $engine->localizeObject($object, [
'sourceLocale' => 'en',
'targetLocale' => 'fr',
]);
print_r($objectFr);
// 3. Chat
$chatJa = $engine->localizeChat([
['name' => 'Alice', 'text' => 'Hi'],
['name' => 'Bob', 'text' => 'Hello!'],
], [
'sourceLocale' => 'en',
'targetLocale' => 'ja',
]);
print_r($chatJa);
// 4. Detect language
$lang = $engine->recognizeLocale('Ciao mondo');
echo "Detected: $lang\n";
The SDK uses semantic versioning (MAJOR.MINOR.PATCH) and is automatically published to Packagist when changes are merged to the main branch. The release process includes:
- Running tests to ensure code quality
- Detecting the current version from git tags
- Automatically bumping the patch version
- Creating a new git tag for the new version
Packagist automatically fetches new versions from the GitHub repository when tags are pushed, making the new version immediately available for installation via Composer. The version is determined by git tags rather than being stored in composer.json, following Packagist best practices.
For more detailed documentation, visit the Lingo.dev Documentation.
This SDK is released under the MIT License.