PHP SDK

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

<?php

require 'vendor/autoload.php';

use LingoDotDev\Sdk\LingoDotDevEngine;

$engine = new LingoDotDevEngine([
    'apiKey' => 'your-api-key-here',
]);

Text Translation

Translate simple text:

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

Translate to multiple languages:

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

Object Translation

Translate structured data:

$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',
]);
// Returns translated array with same structure

This method handles nested arrays and preserves the original structure while translating text values.

Chat Translation

Translate conversation arrays:

$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',
]);
// Returns translated conversation with same structure

The chat method preserves metadata like usernames while translating only the text content.

Language Detection

Automatically detect source language:

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

Use with automatic detection:

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

Language detection adds processing time, so use it only when the source language is unknown.

Configuration Options

Configure SDK behavior:

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

Batch size controls how many items are sent in each API request. Higher values reduce API calls but increase request size.

Ideal batch item size controls the target word count for optimal processing efficiency.

Translation Parameters

Speed vs quality control:

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

The fast parameter enables quicker translations with potentially reduced quality for time-sensitive applications.

Progress Tracking

Monitor large translation jobs:

$engine->localizeText('Hello, world!', [
    'sourceLocale' => 'en',
    'targetLocale' => 'es',
], function ($progress, $chunk, $processedChunk) {
    echo "Translation progress: $progress%\n";
    // Update UI or log progress
});

Progress callbacks provide real-time feedback for large translation operations, useful for user interface updates.

Error Handling

Implement proper error handling:

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

The SDK includes automatic retries for network issues, but application-level error handling remains important.

Laravel Integration

Use in Laravel applications:

// In a Laravel controller
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]);
    }
}

Requirements

  • PHP 8.1 or higher
  • Composer
  • GuzzleHttp Client
  • Respect Validation