Skip to content

Commit 56d55f2

Browse files
committed
OpenAI ve Google Translate entegrasyonları eklendi; README.md dosyası güncellendi, composer.json dosyasına yeni bağımlılıklar eklendi ve yapılandırma dosyası güncellendi. Ayrıca, LaravelTranslatorCommand sınıfı kaldırıldı ve bazı hata kontrolü iyileştirmeleri yapıldı.
1 parent 175715b commit 56d55f2

13 files changed

+226
-80
lines changed

README.md

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,158 @@
1-
# This package allows you to use freely available online translation tools in your project.
1+
<img src="./art/logo.png" alt="Laravel Translator" />
22

3-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ferdiunal/laravel-translator.svg?style=flat-square)](https://packagist.org/packages/ferdiunal/laravel-translator)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ferdiunal/laravel-translator/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/ferdiunal/laravel-translator/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/ferdiunal/laravel-translator/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/ferdiunal/laravel-translator/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
6-
[![Total Downloads](https://img.shields.io/packagist/dt/ferdiunal/laravel-translator.svg?style=flat-square)](https://packagist.org/packages/ferdiunal/laravel-translator)
3+
# Laravel Translator
74

8-
This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
5+
A powerful and flexible translation package for Laravel applications that supports multiple translation services including OpenAI, DeepL, Google Translate, and NLPCloud.
96

10-
## Support us
7+
## Installation
118

12-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-translator.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-translator)
9+
```bash
10+
composer require ferdiunal/laravel-translator
11+
```
1312

14-
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
13+
## Configuration
1514

16-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
15+
First, publish the configuration file:
1716

18-
## Installation
17+
```bash
18+
php artisan vendor:publish --provider="Ferdiunal\LaravelTranslator\LaravelTranslatorServiceProvider"
19+
```
1920

20-
You can install the package via composer:
21+
This will create a `config/translator.php` file in your app that you can modify to set your configuration. You can configure the following settings:
2122

22-
```bash
23-
composer require ferdiunal/laravel-translator
23+
```php
24+
return [
25+
'deepl' => [
26+
'api_key' => env('DEEPL_API_KEY'),
27+
],
28+
29+
'nlpCloud' => [
30+
'api_key' => env('NLPCLOUD_API_KEY'),
31+
'model' => env('NLPCLOUD_MODEL', 'nllb-200-3-3b'),
32+
'languages' => [
33+
'az' => 'azj_Latn',
34+
'de' => 'deu_Latn',
35+
'en' => 'eng_Latn',
36+
'es' => 'spa_Latn',
37+
'it' => 'ita_Latn',
38+
'pt' => 'por_Latn',
39+
'tr' => 'tur_Latn',
40+
'ru' => 'rus_Cyrl',
41+
],
42+
],
43+
44+
'openai' => [
45+
'api_key' => env('OPENAI_API_KEY'),
46+
'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'),
47+
'model' => env('OPENAI_MODEL', 'gpt-4'),
48+
'messages' => [
49+
[
50+
'role' => 'system',
51+
'content' => 'You are an assistant who translates the text from English to Turkish. Just return the translated output.',
52+
],
53+
],
54+
],
55+
];
2456
```
2557

26-
You can publish and run the migrations with:
58+
Add the following environment variables to your `.env` file based on the services you want to use:
59+
60+
```env
61+
# DeepL Configuration
62+
DEEPL_API_KEY=your-deepl-api-key
63+
64+
# NLPCloud Configuration
65+
NLPCLOUD_API_KEY=your-nlpcloud-api-key
66+
NLPCLOUD_MODEL=nllb-200-3-3b
67+
68+
# OpenAI Configuration
69+
OPENAI_API_KEY=your-openai-api-key
70+
OPENAI_BASE_URL=https://api.openai.com/v1
71+
OPENAI_MODEL=gpt-4
72+
```
73+
74+
## Available Translation Services
75+
76+
### Google Translate
77+
78+
To use Google Translate, install the required package:
2779

2880
```bash
29-
php artisan vendor:publish --tag="laravel-translator-migrations"
30-
php artisan migrate
81+
composer require stichoza/google-translate-php
3182
```
3283

33-
You can publish the config file with:
84+
### DeepL Translate
85+
86+
To use DeepL Translate, install the required package and set up your API key:
3487

3588
```bash
36-
php artisan vendor:publish --tag="laravel-translator-config"
89+
composer require deeplcom/deepl-php
3790
```
3891

39-
This is the contents of the published config file:
92+
### OpenAI Translate
4093

41-
```php
42-
return [
43-
];
94+
To use OpenAI's translation capabilities, install the official PHP package:
95+
96+
```bash
97+
composer require openai/openai-php
4498
```
4599

46-
Optionally, you can publish the views using
100+
### NLPCloud Translate
101+
102+
To use NLPCloud's translation service, install their client package:
47103

48104
```bash
49-
php artisan vendor:publish --tag="laravel-translator-views"
105+
composer require nlpcloud/nlpcloud-client
50106
```
51107

52108
## Usage
53109

110+
### Basic Usage
111+
54112
```php
55-
$laravelTranslator = new Ferdiunal\LaravelTranslator();
56-
echo $laravelTranslator->echoPhrase('Hello, Ferdiunal!');
57-
```
113+
use Ferdiunal\LaravelTranslator\Facades\Translator;
58114

59-
## Testing
115+
// Translate a single text
116+
$translatedText = Translator::translate('Hello World', 'tr');
60117

61-
```bash
62-
composer test
63-
```
118+
// Translate multiple texts
119+
$translations = Translator::translate(['Hello', 'World'], 'tr');
64120

65-
## Changelog
121+
// Specify source language
122+
$translatedText = Translator::from('en')->translate('Hello World', 'tr');
66123

67-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
124+
// Use specific translation service
125+
$translatedText = Translator::using('openai')->translate('Hello World', 'tr');
126+
$translatedText = Translator::using('deepl')->translate('Hello World', 'tr');
127+
$translatedText = Translator::using('nlpcloud')->translate('Hello World', 'tr');
128+
```
68129

69-
## Contributing
130+
### Working with Laravel Collections
70131

71-
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
132+
```php
133+
use Ferdiunal\LaravelTranslator\Facades\Translator;
72134

73-
## Security Vulnerabilities
135+
$collection = collect([
136+
'title' => 'Hello World',
137+
'description' => 'This is a description',
138+
]);
74139

75-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
140+
// Translate all values in a collection
141+
$translatedCollection = $collection->map(function ($text) {
142+
return Translator::translate($text, 'tr');
143+
});
144+
```
145+
146+
### Blade Integration
76147

77-
## Credits
148+
```php
149+
{{-- Basic translation directive --}}
150+
{{ translate('Hello World', 'tr') }}
78151

79-
- [Ferdi ÜNAL](https://github.com/ferdiunal)
80-
- [All Contributors](../../contributors)
152+
{{-- Use specific translation service --}}
153+
{{ translate('Hello World', 'tr', 'openai') }}
154+
```
81155

82156
## License
83157

84-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
158+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

art/logo.png

154 KB
Loading

composer.json

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
"php": "^8.2",
3434
"deeplcom/deepl-php": "^1.9",
3535
"illuminate/contracts": "^10.0||^11.0||^12.0",
36-
"spatie/laravel-package-tools": "^1.16"
36+
"openai-php/client": "^0.10.3",
37+
"spatie/laravel-package-tools": "^1.16",
38+
"stichoza/google-translate-php": "^5.2"
3739
},
3840
"require-dev": {
3941
"laravel/pint": "^1.14",
@@ -58,17 +60,21 @@
5860
},
5961
"autoload-dev": {
6062
"psr-4": {
61-
"Ferdiunal\\LaravelTranslator\\Tests\\": "tests/"
63+
"Ferdiunal\\LaravelTranslator\\Tests\\": "tests/",
64+
"Workbench\\App\\": "workbench/app/",
65+
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
66+
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
6267
}
6368
},
6469
"scripts": {
65-
"post-autoload-dump": "@composer run prepare",
66-
"clear": "@php vendor/bin/testbench package:purge-laravel-translator --ansi",
67-
"prepare": "@php vendor/bin/testbench package:discover --ansi",
68-
"build": [
69-
"@composer run prepare",
70-
"@php vendor/bin/testbench workbench:build --ansi"
70+
"post-autoload-dump": [
71+
"@clear",
72+
"@prepare",
73+
"@composer run prepare"
7174
],
75+
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
76+
"prepare": "@php vendor/bin/testbench package:discover --ansi",
77+
"build": "@php vendor/bin/testbench workbench:build --ansi",
7278
"start": [
7379
"Composer\\Config::disableProcessTimeout",
7480
"@composer run build",
@@ -77,7 +83,16 @@
7783
"analyse": "vendor/bin/phpstan analyse",
7884
"test": "vendor/bin/pest",
7985
"test-coverage": "vendor/bin/pest --coverage",
80-
"format": "vendor/bin/pint"
86+
"format": "vendor/bin/pint",
87+
"serve": [
88+
"Composer\\Config::disableProcessTimeout",
89+
"@build",
90+
"@php vendor/bin/testbench serve --ansi"
91+
],
92+
"lint": [
93+
"@php vendor/bin/pint --ansi",
94+
"@php vendor/bin/phpstan analyse --verbose --ansi"
95+
]
8196
},
8297
"config": {
8398
"sort-packages": true,

config/translator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
'nlpCloud' => [
99
'api_key' => env('NLPCLOUD_API_KEY'),
10+
'model' => env('NLPCLOUD_MODEL', 'nllb-200-3-3b'),
1011
'languages' => [
1112
'az' => 'azj_Latn',
1213
'de' => 'deu_Latn',
@@ -18,4 +19,16 @@
1819
'ru' => 'rus_Cyrl',
1920
],
2021
],
22+
23+
'openai' => [
24+
'api_key' => env('OPENAI_API_KEY', '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'),
25+
'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'),
26+
'model' => env('OPENAI_MODEL', 'gpt-4o-mini'),
27+
'messages' => [
28+
[
29+
'role' => 'system',
30+
'content' => 'You are an assistant who translates the text from English to Turkish. Just return the translated output.',
31+
],
32+
],
33+
],
2134
];

src/Commands/LaravelTranslatorCommand.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Facades/LaravelTranslator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
/**
88
* @see \Ferdiunal\LaravelTranslator\LaravelTranslator
99
*
10-
* @param 'google'|'bing'|'deepl'|'myMemory'|'nlpCloud' $translator
10+
* @param 'google'|'bing'|'deepl'|'myMemory'|'nlpCloud'|'openai' $translator
11+
* @param 'az'|'de'|'en'|'es'|'it'|'pt'|'tr'|'ru' $source
12+
* @param 'az'|'de'|'en'|'es'|'it'|'pt'|'tr'|'ru' $target
13+
* @param string $text
1114
*
1215
* @method static string translate(string $translator, string $source, string $target, string $text): string
1316
*/

src/LaravelTranslator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* deepl: \Ferdiunal\LaravelTranslator\Translators\DeepLTranslator,
1212
* myMemory: \Ferdiunal\LaravelTranslator\Translators\MyMemoryTranslator,
1313
* nlpCloud: \Ferdiunal\LaravelTranslator\Translators\NLPCloudTranslator,
14+
* openai: \Ferdiunal\LaravelTranslator\Translators\OpenAITranslator,
1415
* }
1516
*/
1617
class LaravelTranslator

src/LaravelTranslatorServiceProvider.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Ferdiunal\LaravelTranslator;
44

5-
use Ferdiunal\LaravelTranslator\Commands\LaravelTranslatorCommand;
65
use Spatie\LaravelPackageTools\Package;
76
use Spatie\LaravelPackageTools\PackageServiceProvider;
87

@@ -17,7 +16,6 @@ public function configurePackage(Package $package): void
1716
*/
1817
$package
1918
->name('laravel-translator')
20-
->hasConfigFile()
21-
->hasCommand(LaravelTranslatorCommand::class);
19+
->hasConfigFile();
2220
}
2321
}

src/Translators/DeepLTranslator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DeepLTranslator extends Translator
1010
{
1111
public function handle(string $source, string $target, string $text): string
1212
{
13-
if (class_exists("\DeepL\Translator") === false) {
13+
if (class_exists(\DeepL\Translator::class) === false) {
1414
throw new RuntimeException(
1515
'The package deeplcom/deepl-php is not installed. Please run `composer require deeplcom/deepl-php`',
1616
);

src/Translators/NLPCloudTranslator.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class NLPCloudTranslator extends Translator
1010
{
1111
public function handle(string $source, string $target, string $text): string
1212
{
13-
if (class_exists("\NLPCloud\NLPCloud") === false) {
13+
if (class_exists(\NLPCloud\NLPCloud::class) === false) {
1414
throw new RuntimeException(
1515
'The package nlpcloud/nlpcloud-client is not installed. Please run `composer require nlpcloud/nlpcloud-client`',
1616
);
@@ -27,15 +27,13 @@ public function handle(string $source, string $target, string $text): string
2727
}
2828

2929
try {
30-
$translator = new \NLPCloud\NLPCloud('nllb-200-3-3b', $authKey, false);
30+
$translator = new \NLPCloud\NLPCloud(config('translator.nlpCloud.model'), $authKey, false);
3131
$translate = $translator->translation(
3232
$text,
3333
$sourceLang,
3434
$targetLang
3535
);
3636

37-
ray($translate);
38-
3937
return data_get($translate, 'translation_text');
4038
} catch (Exception|Error|RuntimeException $e) {
4139
ray($e);

0 commit comments

Comments
 (0)