Skip to content

Commit 23b40de

Browse files
committed
Initial commit - SharpAPI Laravel package
Laravel package for SharpAPI.com integration. Part of the SharpAPI Laravel SDK collection.
0 parents  commit 23b40de

File tree

8 files changed

+356
-0
lines changed

8 files changed

+356
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.idea
2+
.phpunit.result.cache
3+
build
4+
composer.lock
5+
coverage
6+
docs
7+
phpunit.xml
8+
phpstan.neon
9+
testbench.yaml
10+
vendor
11+
node_modules

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-content-translate` will be documented in this file.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) SharpAPI <contact@sharpapi.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# AI Content Translator for Laravel
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/sharpapi/laravel-content-translate.svg?style=flat-square)](https://packagist.org/packages/sharpapi/laravel-content-translate)
4+
[![Total Downloads](https://img.shields.io/packagist/dt/sharpapi/laravel-content-translate.svg?style=flat-square)](https://packagist.org/packages/sharpapi/laravel-content-translate)
5+
6+
This package provides a Laravel integration for the SharpAPI Content Translation service. It allows you to translate text to different languages with AI-powered accuracy, which is perfect for multilingual applications, content localization, and more.
7+
8+
## Installation
9+
10+
You can install the package via composer:
11+
12+
```bash
13+
composer require sharpapi/laravel-content-translate
14+
```
15+
16+
## Configuration
17+
18+
Publish the config file with:
19+
20+
```bash
21+
php artisan vendor:publish --tag="sharpapi-content-translate"
22+
```
23+
24+
This is the contents of the published config file:
25+
26+
```php
27+
return [
28+
'api_key' => env('SHARP_API_KEY'),
29+
'base_url' => env('SHARP_API_BASE_URL', 'https://sharpapi.com/api/v1'),
30+
'api_job_status_polling_wait' => env('SHARP_API_JOB_STATUS_POLLING_WAIT', 180),
31+
'api_job_status_polling_interval' => env('SHARP_API_JOB_STATUS_POLLING_INTERVAL', 10),
32+
'api_job_status_use_polling_interval' => env('SHARP_API_JOB_STATUS_USE_POLLING_INTERVAL', false),
33+
];
34+
```
35+
36+
Make sure to set your SharpAPI key in your .env file:
37+
38+
```
39+
SHARP_API_KEY=your-api-key
40+
```
41+
42+
## Usage
43+
44+
```php
45+
use SharpAPI\ContentTranslate\ContentTranslateService;
46+
47+
$service = new ContentTranslateService();
48+
49+
// Translate text to a different language
50+
$translatedText = $service->translate(
51+
'Hello, how are you today?',
52+
'Spanish', // target language
53+
'friendly', // optional voice tone
54+
'casual conversation' // optional context
55+
);
56+
57+
// $translatedText will contain the translated text as a string
58+
```
59+
60+
## Parameters
61+
62+
- `text` (string): The text content to translate
63+
- `language` (string): The target language for translation
64+
- `voiceTone` (string|null): The tone of voice for the translation (e.g., professional, casual, friendly)
65+
- `context` (string|null): Additional context to improve translation accuracy
66+
67+
## Response Format
68+
69+
```json
70+
{
71+
"data": {
72+
"type": "api_job_result",
73+
"id": "5de4887a-0dfd-49b6-8edb-9280e468c210",
74+
"attributes": {
75+
"status": "success",
76+
"type": "content_translate",
77+
"result": {
78+
"content": "The rise in sea levels threatens to engulf the Maldives where fresh water is already starting to run out, but the new president of the Indian Ocean archipelago refuses any relocation of its population abroad. In an interview with AFP, President Mohamed Muizzu, a 45-year-old civil engineering graduate trained in the United Kingdom, instead promises an ambitious program of land rehabilitation and island elevation, which environmental organizations criticize.",
79+
"to_language": "English",
80+
"from_language": "French"
81+
}
82+
}
83+
}
84+
}
85+
86+
## Supported Languages
87+
88+
The service supports a wide range of languages, including but not limited to:
89+
- English
90+
- Spanish
91+
- French
92+
- German
93+
- Italian
94+
- Portuguese
95+
- Russian
96+
- Chinese
97+
- Japanese
98+
- Arabic
99+
- And many more
100+
101+
## Credits
102+
103+
- [Dawid Makowski](https://github.com/dawidmakowski)
104+
- [All Contributors](../../contributors)
105+
106+
## License
107+
108+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"name": "sharpapi/laravel-content-translate",
3+
"description": "AI Content Translation for Laravel powered by SharpAPI.com",
4+
"keywords": [
5+
"sharpapi",
6+
"ai-powered",
7+
"ai capabilities",
8+
"api",
9+
"ai api",
10+
"api integration",
11+
"artificial intelligence",
12+
"natural language processing",
13+
"restful api",
14+
"php",
15+
"laravel",
16+
"software development",
17+
"content generation",
18+
"content analysis",
19+
"translation",
20+
"language",
21+
"multilingual"
22+
],
23+
"homepage": "https://github.com/sharpapi/laravel-content-translate",
24+
"license": "MIT",
25+
"authors": [
26+
{
27+
"name": "Dawid Makowski",
28+
"email": "contact@sharpapi.com",
29+
"role": "Developer"
30+
}
31+
],
32+
"require": {
33+
"php": "^8.1",
34+
"ext-json": "*",
35+
"guzzlehttp/guzzle": "^7.0",
36+
"laravel/framework": "^9.0|^10.0|^11.0|^12.0",
37+
"kongulov/interact-with-enum": "^1.0",
38+
"sharpapi/php-core": "^1.0",
39+
"spatie/laravel-data": "^3.0|^4.0",
40+
"spatie/url": "^2.4"
41+
},
42+
"require-dev": {
43+
"laravel/pint": "^1.0"
44+
},
45+
"autoload": {
46+
"psr-4": {
47+
"SharpAPI\\ContentTranslate\\": "src/"
48+
}
49+
},
50+
"autoload-dev": {
51+
"psr-4": {
52+
"SharpAPI\\ContentTranslate\\Tests\\": "tests/"
53+
}
54+
},
55+
"scripts": {
56+
"post-autoload-dump": "",
57+
"build": [
58+
],
59+
"start": [
60+
"Composer\\Config::disableProcessTimeout",
61+
"@composer run build"
62+
],
63+
"analyse": "vendor/bin/phpstan analyse",
64+
"test": "vendor/bin/pest",
65+
"test-coverage": "vendor/bin/pest --coverage",
66+
"format": "vendor/bin/pint"
67+
},
68+
"config": {
69+
"sort-packages": true,
70+
"allow-plugins": {
71+
"pestphp/pest-plugin": true,
72+
"phpstan/extension-installer": true
73+
}
74+
},
75+
"extra": {
76+
"laravel": {
77+
"providers": [
78+
"SharpAPI\\ContentTranslate\\ContentTranslateProvider"
79+
]
80+
}
81+
},
82+
"minimum-stability": "dev",
83+
"prefer-stable": true
84+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'api_key' => env('SHARP_API_KEY'),
7+
'base_url' => env('SHARP_API_BASE_URL', 'https://sharpapi.com/api/v1'), // as ENV is mock server needed
8+
// how long (in seconds) the client should wait in polling mode for results
9+
'api_job_status_polling_wait' => env('SHARP_API_JOB_STATUS_POLLING_WAIT', 180),
10+
// how many seconds the client should wait between each result request
11+
// usually Retry-After header is used (default 10s), this value won't have an effect unless
12+
// api_job_status_use_polling_interval is set to TRUE
13+
'api_job_status_polling_interval' => env('SHARP_API_JOB_STATUS_POLLING_INTERVAL', 10),
14+
'api_job_status_use_polling_interval' => env('SHARP_API_JOB_STATUS_USE_POLLING_INTERVAL', false),
15+
// for affiliate program members use
16+
];

src/ContentTranslateProvider.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SharpAPI\ContentTranslate;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
9+
/**
10+
* @api
11+
*/
12+
class ContentTranslateProvider extends ServiceProvider
13+
{
14+
/**
15+
* Bootstrap the application services.
16+
*/
17+
public function boot(): void
18+
{
19+
if ($this->app->runningInConsole()) {
20+
$this->publishes([
21+
__DIR__.'/../config/sharpapi-content-translate.php' => config_path('sharpapi-content-translate.php'),
22+
], 'sharpapi-content-translate');
23+
}
24+
}
25+
26+
/**
27+
* Register the application services.
28+
*/
29+
public function register(): void
30+
{
31+
// Merge the package configuration with the app configuration.
32+
$this->mergeConfigFrom(
33+
__DIR__.'/../config/sharpapi-content-translate.php', 'sharpapi-content-translate'
34+
);
35+
}
36+
}

src/ContentTranslateService.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SharpAPI\ContentTranslate;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use InvalidArgumentException;
9+
use SharpAPI\Core\Client\SharpApiClient;
10+
11+
/**
12+
* @api
13+
*/
14+
class ContentTranslateService extends SharpApiClient
15+
{
16+
/**
17+
* Initializes a new instance of the class.
18+
*
19+
* @throws InvalidArgumentException if the API key is empty.
20+
*/
21+
public function __construct()
22+
{
23+
parent::__construct(config('sharpapi-content-translate.api_key'));
24+
$this->setApiBaseUrl(
25+
config(
26+
'sharpapi-content-translate.base_url',
27+
'https://sharpapi.com/api/v1'
28+
)
29+
);
30+
$this->setApiJobStatusPollingInterval(
31+
(int) config(
32+
'sharpapi-content-translate.api_job_status_polling_interval',
33+
5)
34+
);
35+
$this->setApiJobStatusPollingWait(
36+
(int) config(
37+
'sharpapi-content-translate.api_job_status_polling_wait',
38+
180)
39+
);
40+
$this->setUserAgent('SharpAPILaravelContentTranslate/1.0.0');
41+
}
42+
43+
/**
44+
* Translates text to the specified language.
45+
* You can set your preferred writing style by providing an optional voice_tone parameter.
46+
* Additional context can be provided to improve translation quality.
47+
*
48+
* @param string $text The text to translate
49+
* @param string $language The target language for translation
50+
* @param string|null $voiceTone The tone of voice for the translation (optional)
51+
* @param string|null $context Additional context for better translation (optional)
52+
* @return string The translated text or an error message
53+
*
54+
* @throws GuzzleException
55+
*
56+
* @api
57+
*/
58+
public function translate(
59+
string $text,
60+
string $language,
61+
?string $voiceTone = null,
62+
?string $context = null
63+
): string {
64+
$response = $this->makeRequest(
65+
'POST',
66+
'/content/translate',
67+
[
68+
'content' => $text,
69+
'language' => $language,
70+
'voice_tone' => $voiceTone,
71+
'context' => $context,
72+
]
73+
);
74+
75+
return $this->parseStatusUrl($response);
76+
}
77+
}

0 commit comments

Comments
 (0)