lingodotdev / sdk
Official PHP SDK for Lingo.dev
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
- respect/validation: ^2.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-05-03 07:20:21 UTC
README
Official PHP SDK for Lingo.dev, a powerful localization engine that supports various content types including plain text, objects, and chat sequences.
Installation
You can install the SDK via Composer:
composer require lingodotdev/sdk
Basic Usage
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 ]);
Scenarios demonstrated in this README
- Text Localization
- Object Localization
- Chat Localization
- Batch Localization
- Language Detection
- Progress Tracking
Requirements
- PHP 8.1 or higher
- Composer
- GuzzleHttp Client
- Respect Validation
Getting Started
Creating a New PHP Project with Lingo.dev SDK
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 <your.email@example.com>"
-
Add Lingo.dev SDK as a dependency:
composer require lingodotdev/sdk
API Scenarios
Initialize the SDK
<?php use LingoDotDev\Sdk\LingoDotDevEngine; // Initialize the SDK with your API key $engine = new LingoDotDevEngine([ 'apiKey' => 'your-api-key', ]);
Text Localization
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!"
Object Localization
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' ] ] */
Chat Localization
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?'] ] */
Language Detection
Detect the language of a given text:
// Detect language $locale = $engine->recognizeLocale('Bonjour le monde'); // Output: "fr"
Batch Localization
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!" ] */
Progress Tracking
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"; });
Demo App
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";
Release Process
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.
Documentation
For more detailed documentation, visit the Lingo.dev Documentation.
License
This SDK is released under the MIT License.