|
| 1 | +# Lingo.dev PHP SDK |
| 2 | + |
| 3 | +Official PHP SDK for Lingo.dev, a powerful localization engine that supports various content types including plain text, objects, and chat sequences. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You can install the SDK via Composer: |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require lingodotdev/sdk |
| 11 | +``` |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- PHP 8.1 or higher |
| 16 | +- Composer |
| 17 | +- GuzzleHttp Client |
| 18 | +- Respect Validation |
| 19 | + |
| 20 | +## Getting Started |
| 21 | + |
| 22 | +### Creating a New PHP Project with Lingo.dev SDK |
| 23 | + |
| 24 | +Follow these steps to create a new PHP project that uses the Lingo.dev SDK: |
| 25 | + |
| 26 | +1. **Create a project directory**: |
| 27 | + ```bash |
| 28 | + mkdir my-lingo-project |
| 29 | + cd my-lingo-project |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Initialize Composer**: |
| 33 | + ```bash |
| 34 | + composer init --name=your-vendor/your-project --description="Your project description" --type=project --require="php:^8.1" --author="Your Name <your.email@example.com>" |
| 35 | + ``` |
| 36 | + |
| 37 | +3. **Add Lingo.dev SDK as a dependency**: |
| 38 | + ```bash |
| 39 | + composer require lingodotdev/sdk |
| 40 | + ``` |
| 41 | + |
| 42 | +4. **Create a simple PHP script** (index.php): |
| 43 | + ```php |
| 44 | + <?php |
| 45 | + |
| 46 | + require 'vendor/autoload.php'; |
| 47 | + |
| 48 | + use LingoDotDev\Sdk\LingoDotDevEngine; |
| 49 | + |
| 50 | + // Get API key from environment variable or command line |
| 51 | + $apiKey = getenv('LINGODOTDEV_API_KEY') ?: $argv[1] ?? null; |
| 52 | + |
| 53 | + if (!$apiKey) { |
| 54 | + echo "Error: API key is required. Set LINGODOTDEV_API_KEY environment variable or pass it as a command-line argument.\n"; |
| 55 | + exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + // Initialize the SDK |
| 59 | + $engine = new LingoDotDevEngine([ |
| 60 | + 'apiKey' => $apiKey, |
| 61 | + ]); |
| 62 | + |
| 63 | + // Make your first localization call |
| 64 | + try { |
| 65 | + $result = $engine->localizeText('Hello, this is my first localization with Lingo.dev!', [ |
| 66 | + 'sourceLocale' => 'en', |
| 67 | + 'targetLocale' => 'es', |
| 68 | + ]); |
| 69 | + |
| 70 | + echo "Original: Hello, this is my first localization with Lingo.dev!\n"; |
| 71 | + echo "Translated to Spanish: $result\n"; |
| 72 | + } catch (\Exception $e) { |
| 73 | + echo "Error: " . $e->getMessage() . "\n"; |
| 74 | + } |
| 75 | + ``` |
| 76 | + |
| 77 | +5. **Run your script**: |
| 78 | + ```bash |
| 79 | + # Option 1: Pass API key as command-line argument |
| 80 | + php index.php your-api-key-here |
| 81 | + |
| 82 | + # Option 2: Set environment variable and run |
| 83 | + export LINGODOTDEV_API_KEY=your-api-key-here |
| 84 | + php index.php |
| 85 | + ``` |
| 86 | + |
| 87 | +## Basic Usage |
| 88 | + |
| 89 | +### Initialize the SDK |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | + |
| 94 | +use LingoDotDev\Sdk\LingoDotDevEngine; |
| 95 | + |
| 96 | +// Initialize the SDK with your API key |
| 97 | +$engine = new LingoDotDevEngine([ |
| 98 | + 'apiKey' => 'your-api-key', |
| 99 | +]); |
| 100 | +``` |
| 101 | + |
| 102 | +### Text Localization |
| 103 | + |
| 104 | +Translate a simple text string from one language to another: |
| 105 | + |
| 106 | +```php |
| 107 | +// Localize a text string from English to Spanish |
| 108 | +$localizedText = $engine->localizeText('Hello, world!', [ |
| 109 | + 'sourceLocale' => 'en', |
| 110 | + 'targetLocale' => 'es', |
| 111 | +]); |
| 112 | +// Output: "¡Hola, mundo!" |
| 113 | +``` |
| 114 | + |
| 115 | +### Object Localization |
| 116 | + |
| 117 | +Translate an array of strings while preserving the structure: |
| 118 | + |
| 119 | +```php |
| 120 | +// Localize an object from English to French |
| 121 | +$localizedObject = $engine->localizeObject([ |
| 122 | + 'greeting' => 'Hello', |
| 123 | + 'farewell' => 'Goodbye', |
| 124 | + 'messages' => [ |
| 125 | + 'welcome' => 'Welcome to our service', |
| 126 | + 'thanks' => 'Thank you for your business' |
| 127 | + ] |
| 128 | +], [ |
| 129 | + 'sourceLocale' => 'en', |
| 130 | + 'targetLocale' => 'fr', |
| 131 | +]); |
| 132 | +/* Output: |
| 133 | +[ |
| 134 | + 'greeting' => 'Bonjour', |
| 135 | + 'farewell' => 'Au revoir', |
| 136 | + 'messages' => [ |
| 137 | + 'welcome' => 'Bienvenue dans notre service', |
| 138 | + 'thanks' => 'Merci pour votre confiance' |
| 139 | + ] |
| 140 | +] |
| 141 | +*/ |
| 142 | +``` |
| 143 | + |
| 144 | +### Chat Localization |
| 145 | + |
| 146 | +Translate a chat conversation while preserving speaker names: |
| 147 | + |
| 148 | +```php |
| 149 | +// Localize a chat conversation from English to German |
| 150 | +$localizedChat = $engine->localizeChat([ |
| 151 | + ['name' => 'Alice', 'text' => 'Hello, how are you?'], |
| 152 | + ['name' => 'Bob', 'text' => 'I am fine, thank you!'], |
| 153 | + ['name' => 'Alice', 'text' => 'What are you doing today?'] |
| 154 | +], [ |
| 155 | + 'sourceLocale' => 'en', |
| 156 | + 'targetLocale' => 'de', |
| 157 | +]); |
| 158 | +/* Output: |
| 159 | +[ |
| 160 | + ['name' => 'Alice', 'text' => 'Hallo, wie geht es dir?'], |
| 161 | + ['name' => 'Bob', 'text' => 'Mir geht es gut, danke!'], |
| 162 | + ['name' => 'Alice', 'text' => 'Was machst du heute?'] |
| 163 | +] |
| 164 | +*/ |
| 165 | +``` |
| 166 | + |
| 167 | +### Language Detection |
| 168 | + |
| 169 | +Detect the language of a given text: |
| 170 | + |
| 171 | +```php |
| 172 | +// Detect language |
| 173 | +$locale = $engine->recognizeLocale('Bonjour le monde'); |
| 174 | +// Output: "fr" |
| 175 | +``` |
| 176 | + |
| 177 | +### Batch Localization |
| 178 | + |
| 179 | +Translate a text to multiple languages at once: |
| 180 | + |
| 181 | +```php |
| 182 | +// Batch localize text to multiple languages |
| 183 | +$localizedTexts = $engine->batchLocalizeText('Hello, world!', [ |
| 184 | + 'sourceLocale' => 'en', |
| 185 | + 'targetLocales' => ['es', 'fr', 'de', 'it'], |
| 186 | +]); |
| 187 | +/* Output: |
| 188 | +[ |
| 189 | + "¡Hola, mundo!", |
| 190 | + "Bonjour le monde!", |
| 191 | + "Hallo, Welt!", |
| 192 | + "Ciao, mondo!" |
| 193 | +] |
| 194 | +*/ |
| 195 | +``` |
| 196 | + |
| 197 | +### Progress Tracking |
| 198 | + |
| 199 | +Track the progress of a localization operation: |
| 200 | + |
| 201 | +```php |
| 202 | +// Localize with progress tracking |
| 203 | +$engine->localizeText('Hello, world!', [ |
| 204 | + 'sourceLocale' => 'en', |
| 205 | + 'targetLocale' => 'es', |
| 206 | +], function ($progress, $chunk, $processedChunk) { |
| 207 | + echo "Localization progress: $progress%\n"; |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | +## Release Process |
| 212 | + |
| 213 | +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: |
| 214 | + |
| 215 | +1. Running tests to ensure code quality |
| 216 | +2. Automatically bumping the patch version |
| 217 | +3. Creating a git tag for the new version |
| 218 | + |
| 219 | +Packagist automatically fetches new versions from the GitHub repository when tags are pushed, making the new version immediately available for installation via Composer. |
| 220 | + |
| 221 | +## Documentation |
| 222 | + |
| 223 | +For more detailed documentation, visit the [Lingo.dev Documentation](https://lingo.dev/go/docs). |
| 224 | + |
| 225 | +## License |
| 226 | + |
| 227 | +This SDK is released under the MIT License. |
0 commit comments