PHP SDK

AI translation with PHP and Lingo.dev

Introduction

The Lingo.dev PHP SDK adds real-time AI-powered translation to PHP applications and Laravel projects. The SDK handles dynamic content like chat messages, user comments, and live data that needs instant translation.

Unlike static file localization with Lingo.dev CLI, the SDK processes content on-demand, making it ideal for chat applications, email clients, and social media tools where content changes constantly.

Installation

composer require lingodotdev/sdk

Basic setup

The SDK requires an API key from Lingo.dev.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

Text translation

Translate simple text strings to a target language.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$result = $engine->localizeText('Hello, world!', [
    'sourceLocale' => 'en',
    'targetLocale' => 'es',
]);
echo $result;
// Output: ¡Hola, mundo!

Object translation

Translate nested arrays while preserving structure. The SDK recursively processes all string values.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$content = [
    'greeting' => 'Hello',
    'farewell' => 'Goodbye',
    'messages' => [
        'welcome' => 'Welcome to our service',
        'thanks' => 'Thank you for your business'
    ]
];

$translated = $engine->localizeObject($content, [
    'sourceLocale' => 'en',
    'targetLocale' => 'fr',
]);
print_r($translated);
// Output: Array with French translations maintaining structure

Batch translation to multiple languages

Translate content to multiple target languages in a single call. Returns an array with one result per target locale.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$results = $engine->batchLocalizeText('Hello, world!', [
    'sourceLocale' => 'en',
    'targetLocales' => ['es', 'fr', 'de'],
]);
print_r($results);
// Output: ["¡Hola, mundo!", "Bonjour le monde!", "Hallo, Welt!"]

Chat translation

Translate chat messages while preserving speaker names. Each message must have both "name" and "text" fields.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$conversation = [
    ['name' => 'Alice', 'text' => 'Hello, how are you?'],
    ['name' => 'Bob', 'text' => 'I am fine, thank you!'],
    ['name' => 'Alice', 'text' => 'What are you doing today?']
];

$translated = $engine->localizeChat($conversation, [
    'sourceLocale' => 'en',
    'targetLocale' => 'de',
]);

foreach ($translated as $message) {
    echo $message['name'] . ': ' . $message['text'] . "\n";
}
// Output:
// Alice: Hallo, wie geht es dir?
// Bob: Mir geht es gut, danke!
// Alice: Was machst du heute?

Progress tracking

Monitor translation progress with a callback. Useful for updating UI during large translation operations.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$largeText = 'This is a very long text that needs translation...';

$result = $engine->localizeText($largeText, [
    'sourceLocale' => 'en',
    'targetLocale' => 'es',
], function ($progress, $chunk, $processedChunk) {
    echo "Translation progress: $progress%\n";
});
// Output:
// Translation progress: 25%
// Translation progress: 50%
// Translation progress: 75%
// Translation progress: 100%

Translation parameters

Speed vs quality control for time-sensitive applications.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$result = $engine->localizeText('Hello world', [
    'sourceLocale' => 'en',
    'targetLocale' => 'es',
    'fast' => true, // Prioritize speed over quality
]);
echo $result;
// Output: Hola mundo

Configuration

Control batching behavior. The SDK splits large payloads based on item count and word count constraints.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
    'batchSize' => 100, // Max items per API request (default: 50, max: 250)
    'idealBatchItemSize' => 1000, // Target word count per batch (default: 500, max: 2500)
]);

$result = $engine->localizeText('Configuration test', [
    'sourceLocale' => 'en',
    'targetLocale' => 'es',
]);
echo $result;
// Output: Prueba de configuración

Language detection

Detect the language of a text string. Use only when the source language is unknown, as detection adds processing time.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$locale = $engine->recognizeLocale('Bonjour le monde');
echo $locale;
// Output: fr

Use with automatic detection:

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

$result = $engine->localizeText('Bonjour le monde', [
    'sourceLocale' => null, // Auto-detect
    'targetLocale' => 'en',
]);
echo $result;
// Output: Hello world

Laravel integration

Use in Laravel applications.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use LingoDotDev\Sdk\LingoDotDevEngine;

class TranslationController extends Controller
{
    private $engine;

    public function __construct()
    {
        $this->engine = new LingoDotDevEngine([
            'apiKey' => config('services.lingodotdev.api_key'),
        ]);
    }

    public function translateMessage(Request $request)
    {
        $translated = $this->engine->localizeText($request->message, [
            'sourceLocale' => $request->source_locale,
            'targetLocale' => $request->target_locale,
        ]);

        return response()->json(['translated' => $translated]);
    }
}

Error handling

The SDK includes automatic retries for network issues. Implement application-level error handling for other failures.

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => $_ENV['LINGODOTDEV_API_KEY'],
]);

try {
    $result = $engine->localizeText('Hello', [
        'sourceLocale' => 'en',
        'targetLocale' => 'es',
    ]);
    echo $result;
} catch (Exception $e) {
    error_log('Translation failed: ' . $e->getMessage());
    // Handle error appropriately
}