Skip to content

Commit 4f353ff

Browse files
Merge pull request #1 from lingodotdev/devin/1746244952-setup-php-sdk
feat: set up automated publishing of PHP SDK to Packagist
2 parents ce64911 + dbcf777 commit 4f353ff

File tree

6 files changed

+1056
-0
lines changed

6 files changed

+1056
-0
lines changed

.github/workflows/publish.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Publish PHP SDK to Packagist
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
permissions:
10+
contents: write
11+
12+
jobs:
13+
test-and-publish:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: '8.1'
25+
extensions: mbstring, intl
26+
coverage: none
27+
tools: composer:v2
28+
29+
- name: Validate composer.json
30+
run: composer validate --strict
31+
32+
- name: Install dependencies
33+
run: composer install --prefer-dist
34+
35+
- name: Run tests
36+
run: vendor/bin/phpunit tests/
37+
38+
# Only proceed with version bump and publishing if on main branch and tests passed
39+
- name: Get current version
40+
id: current_version
41+
if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch')
42+
run: |
43+
# Check if version exists in composer.json
44+
VERSION=$(php -r '
45+
$composerJson = json_decode(file_get_contents("composer.json"), true);
46+
echo isset($composerJson["version"]) ? $composerJson["version"] : "";
47+
')
48+
49+
if [[ -z "$VERSION" || ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
50+
echo "Setting initial version to 0.1.0"
51+
VERSION="0.1.0"
52+
fi
53+
54+
echo "version=$VERSION" >> $GITHUB_OUTPUT
55+
56+
- name: Bump patch version
57+
id: bump_version
58+
if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch')
59+
run: |
60+
# Get current version
61+
CURRENT_VERSION=${{ steps.current_version.outputs.version }}
62+
63+
# Validate current version format
64+
if [[ ! "$CURRENT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
65+
echo "Warning: Invalid version format: $CURRENT_VERSION. Using 0.1.0 as base."
66+
CURRENT_VERSION="0.1.0"
67+
fi
68+
69+
# Use PHP to increment patch version
70+
NEW_VERSION=$(php -r '
71+
$version = "${{ steps.current_version.outputs.version }}";
72+
if (!preg_match("/^[0-9]+\.[0-9]+\.[0-9]+$/", $version)) {
73+
$version = "0.1.0";
74+
}
75+
list($major, $minor, $patch) = explode(".", $version);
76+
$patch++;
77+
echo "$major.$minor.$patch";
78+
')
79+
80+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
81+
82+
# Update version in composer.json
83+
php -r '
84+
$composerJson = json_decode(file_get_contents("composer.json"), true);
85+
$composerJson["version"] = "${{ steps.bump_version.outputs.new_version }}";
86+
file_put_contents("composer.json", json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
87+
'
88+
89+
- name: Commit and push version bump
90+
uses: stefanzweifel/git-auto-commit-action@v4
91+
if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch')
92+
with:
93+
commit_message: "chore: bump PHP SDK version to ${{ steps.bump_version.outputs.new_version }}"
94+
file_pattern: composer.json
95+
commit_user_name: "Lingo.dev"
96+
commit_user_email: "hi@lingo.dev"
97+
98+
- name: Create tag for release
99+
if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch')
100+
run: |
101+
git tag v${{ steps.bump_version.outputs.new_version }}
102+
git push origin v${{ steps.bump_version.outputs.new_version }}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/vendor/
2+
/composer.lock
3+
.env
4+
/demo/vendor/
5+
/demo/composer.lock

README.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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.

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "lingodotdev/sdk",
3+
"description": "Official PHP SDK for Lingo.dev",
4+
"type": "library",
5+
"license": "MIT",
6+
"version": "0.1.2",
7+
"authors": [
8+
{
9+
"name": "Lingo.dev Team",
10+
"email": "hi@lingo.dev"
11+
}
12+
],
13+
"require": {
14+
"php": "^8.1",
15+
"guzzlehttp/guzzle": "^7.0",
16+
"respect/validation": "^2.0"
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^9.0"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"LingoDotDev\\Sdk\\": "src/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"LingoDotDev\\Sdk\\Tests\\": "tests/"
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)