Skip to content

Commit 0d63be7

Browse files
Migration of PHP SDK and demo from lingo.dev
Co-Authored-By: Max Prilutskiy <maks.prilutskiy@gmail.com>
1 parent ce64911 commit 0d63be7

File tree

12 files changed

+1204
-0
lines changed

12 files changed

+1204
-0
lines changed

.github/workflows/publish.yml

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

.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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Lingo.dev PHP SDK
2+
3+
Official PHP SDK for Lingo.dev.
4+
5+
## Installation
6+
7+
```bash
8+
composer require lingodotdev/sdk
9+
```
10+
11+
## Usage
12+
13+
```php
14+
<?php
15+
16+
use Lingodotdev\Sdk\LingoDotDevEngine;
17+
18+
// Initialize the SDK with your API key
19+
$engine = new LingoDotDevEngine([
20+
'apiKey' => 'your-api-key',
21+
]);
22+
23+
// Localize a text string
24+
$localizedText = $engine->localizeText('Hello, world!', [
25+
'sourceLocale' => 'en',
26+
'targetLocale' => 'es',
27+
]);
28+
29+
// Localize an object
30+
$localizedObject = $engine->localizeObject([
31+
'greeting' => 'Hello',
32+
'farewell' => 'Goodbye'
33+
], [
34+
'sourceLocale' => 'en',
35+
'targetLocale' => 'fr',
36+
]);
37+
38+
// Localize a chat conversation
39+
$localizedChat = $engine->localizeChat([
40+
['name' => 'Alice', 'text' => 'Hello, how are you?'],
41+
['name' => 'Bob', 'text' => 'I am fine, thank you!']
42+
], [
43+
'sourceLocale' => 'en',
44+
'targetLocale' => 'de',
45+
]);
46+
47+
// Detect language
48+
$locale = $engine->recognizeLocale('Bonjour le monde');
49+
```
50+
51+
## Documentation
52+
53+
[Documentation](https://lingo.dev/go/docs)

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+
}

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

demo/.gitignore

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

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/sdk-php.git
25+
cd sdk-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/sdk-php)
106+
- [Packagist Package](https://packagist.org/packages/lingodotdev/sdk)

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+
}

0 commit comments

Comments
 (0)