Skip to content

Commit 7a8e7c4

Browse files
feat: add PHP SDK demo with usage examples (#734)
* feat: add PHP SDK demo with README and example code Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> * fix: rename environment variable to LINGODOTDEV_API_KEY Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent 7b5cf50 commit 7a8e7c4

File tree

5 files changed

+212
-0
lines changed

5 files changed

+212
-0
lines changed

php/demo/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LINGODOTDEV_API_KEY=your-api-key-here

php/demo/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
.env

php/demo/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Lingo.dev PHP SDK Demo
2+
3+
This demo shows how to use the [Lingo.dev PHP SDK](https://packagist.org/packages/lingodotdev/sdk) for localizing content in various formats. This guide is designed to be easy to follow for non-PHP developers.
4+
5+
## Prerequisites
6+
7+
Before you begin, make sure you have:
8+
9+
1. **PHP installed** (version 8.1 or later)
10+
- For Windows: [Download from windows.php.net](https://windows.php.net/download/)
11+
- For macOS: `brew install php` (using Homebrew)
12+
- For Linux: `sudo apt install php8.1` (Ubuntu/Debian) or `sudo yum install php` (CentOS/RHEL)
13+
14+
2. **Composer installed** (PHP package manager)
15+
- Follow the [official Composer installation guide](https://getcomposer.org/download/)
16+
17+
3. **Lingo.dev API Key**
18+
- Sign up at [Lingo.dev](https://lingo.dev) to get your API key
19+
20+
## Installation
21+
22+
1. Clone this repository:
23+
```bash
24+
git clone https://github.com/lingodotdev/lingo.dev.git
25+
cd lingo.dev/php/demo
26+
```
27+
28+
2. Install dependencies:
29+
```bash
30+
composer install
31+
```
32+
33+
## Running the Demo
34+
35+
You can run the demo in two ways:
36+
37+
### Option 1: Pass API key as command-line argument
38+
```bash
39+
php index.php your-api-key-here
40+
```
41+
42+
### Option 2: Set environment variable
43+
44+
For Linux/macOS:
45+
```bash
46+
export LINGODOTDEV_API_KEY=your-api-key-here
47+
php index.php
48+
```
49+
50+
For Windows Command Prompt:
51+
```cmd
52+
set LINGODOTDEV_API_KEY=your-api-key-here
53+
php index.php
54+
```
55+
56+
For Windows PowerShell:
57+
```powershell
58+
$env:LINGODOTDEV_API_KEY="your-api-key-here"
59+
php index.php
60+
```
61+
62+
## What the Demo Shows
63+
64+
The demo demonstrates four key features of the Lingo.dev PHP SDK:
65+
66+
1. **Text Localization**: Translating a simple text string from English to Spanish
67+
2. **Object Localization**: Translating an array of strings from English to French
68+
3. **Chat Localization**: Translating a chat conversation while preserving speaker names from English to German
69+
4. **Language Detection**: Detecting the language of a given text
70+
71+
## How It Works
72+
73+
The demo initializes the Lingo.dev SDK with your API key, then shows examples of each localization method:
74+
75+
```php
76+
// Initialize the SDK
77+
$engine = new LingoDotDevEngine([
78+
'apiKey' => $apiKey,
79+
]);
80+
81+
// Example: Localize text
82+
$localizedText = $engine->localizeText("Hello world", [
83+
'sourceLocale' => 'en',
84+
'targetLocale' => 'es',
85+
]);
86+
```
87+
88+
## Modifying the Demo
89+
90+
Feel free to modify the examples in `index.php` to test different texts, languages, or features. The main configuration options include:
91+
92+
- `sourceLocale`: The source language code (e.g., 'en', 'fr')
93+
- `targetLocale`: The target language code for translation
94+
- `fast`: Set to `true` for faster but potentially less accurate translations
95+
96+
## Troubleshooting
97+
98+
- **API Key Issues**: Make sure your API key is valid and has proper permissions
99+
- **PHP Version**: Ensure you're using PHP 8.1 or later
100+
- **Composer Dependencies**: If you encounter errors, try running `composer update` to update dependencies
101+
102+
## Additional Resources
103+
104+
- [Lingo.dev Documentation](https://lingo.dev/go/docs)
105+
- [PHP SDK Source Code](https://github.com/lingodotdev/lingo.dev/tree/main/php/sdk)
106+
- [Packagist Package](https://packagist.org/packages/lingodotdev/sdk)

php/demo/composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "lingodotdev/sdk-demo",
3+
"description": "Demo application for Lingo.dev PHP SDK",
4+
"type": "project",
5+
"require": {
6+
"php": "^8.1",
7+
"lingodotdev/sdk": "^0.1.0"
8+
},
9+
"autoload": {
10+
"psr-4": {
11+
"LingoDotDev\\Demo\\": "src/"
12+
}
13+
}
14+
}

php/demo/index.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
use LingoDotDev\Sdk\LingoDotDevEngine;
6+
7+
if (!isset($_ENV['LINGODOTDEV_API_KEY']) && !isset($argv[1])) {
8+
echo "Error: API key is required. Either set the LINGODOTDEV_API_KEY environment variable or pass it as a command-line argument.\n";
9+
echo "Usage: php index.php <your-api-key>\n";
10+
exit(1);
11+
}
12+
13+
$apiKey = $_ENV['LINGODOTDEV_API_KEY'] ?? $argv[1];
14+
15+
$engine = new LingoDotDevEngine([
16+
'apiKey' => $apiKey,
17+
]);
18+
19+
echo "Lingo.dev PHP SDK Demo\n";
20+
echo "======================\n\n";
21+
22+
$exampleText = "Hello, welcome to Lingo.dev PHP SDK!";
23+
echo "Original text: $exampleText\n";
24+
25+
try {
26+
$localizedText = $engine->localizeText($exampleText, [
27+
'sourceLocale' => 'en',
28+
'targetLocale' => 'es',
29+
]);
30+
echo "Localized to Spanish: $localizedText\n\n";
31+
} catch (\Exception $e) {
32+
echo "Error localizing text: " . $e->getMessage() . "\n\n";
33+
}
34+
35+
$exampleObject = [
36+
'greeting' => 'Welcome to our website',
37+
'message' => 'Thank you for trying our SDK',
38+
'farewell' => 'Have a great day'
39+
];
40+
41+
echo "Original object:\n";
42+
print_r($exampleObject);
43+
44+
try {
45+
$localizedObject = $engine->localizeObject($exampleObject, [
46+
'sourceLocale' => 'en',
47+
'targetLocale' => 'fr',
48+
]);
49+
echo "Localized object to French:\n";
50+
print_r($localizedObject);
51+
echo "\n";
52+
} catch (\Exception $e) {
53+
echo "Error localizing object: " . $e->getMessage() . "\n\n";
54+
}
55+
56+
$exampleChat = [
57+
['name' => 'Alice', 'text' => 'Hello, how can I help you today?'],
58+
['name' => 'Bob', 'text' => 'I need information about the SDK.'],
59+
['name' => 'Alice', 'text' => 'Sure, what would you like to know?']
60+
];
61+
62+
echo "Original chat:\n";
63+
foreach ($exampleChat as $message) {
64+
echo $message['name'] . ": " . $message['text'] . "\n";
65+
}
66+
67+
try {
68+
$localizedChat = $engine->localizeChat($exampleChat, [
69+
'sourceLocale' => 'en',
70+
'targetLocale' => 'de',
71+
]);
72+
echo "Localized chat to German:\n";
73+
foreach ($localizedChat as $message) {
74+
echo $message['name'] . ": " . $message['text'] . "\n";
75+
}
76+
echo "\n";
77+
} catch (\Exception $e) {
78+
echo "Error localizing chat: " . $e->getMessage() . "\n\n";
79+
}
80+
81+
$textToDetect = "Bonjour le monde";
82+
echo "Text for language detection: $textToDetect\n";
83+
84+
try {
85+
$detectedLocale = $engine->recognizeLocale($textToDetect);
86+
echo "Detected language: $detectedLocale\n";
87+
} catch (\Exception $e) {
88+
echo "Error detecting language: " . $e->getMessage() . "\n";
89+
}

0 commit comments

Comments
 (0)