|
1 |
| -# This package allows you to use freely available online translation tools in your project. |
| 1 | +<img src="./art/logo.png" alt="Laravel Translator" /> |
2 | 2 |
|
3 |
| -[](https://packagist.org/packages/ferdiunal/laravel-translator) |
4 |
| -[](https://github.com/ferdiunal/laravel-translator/actions?query=workflow%3Arun-tests+branch%3Amain) |
5 |
| -[](https://github.com/ferdiunal/laravel-translator/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) |
6 |
| -[](https://packagist.org/packages/ferdiunal/laravel-translator) |
| 3 | +# Laravel Translator |
7 | 4 |
|
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. |
9 | 6 |
|
10 |
| -## Support us |
| 7 | +## Installation |
11 | 8 |
|
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 | +``` |
13 | 12 |
|
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 |
15 | 14 |
|
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: |
17 | 16 |
|
18 |
| -## Installation |
| 17 | +```bash |
| 18 | +php artisan vendor:publish --provider="Ferdiunal\LaravelTranslator\LaravelTranslatorServiceProvider" |
| 19 | +``` |
19 | 20 |
|
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: |
21 | 22 |
|
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 | +]; |
24 | 56 | ```
|
25 | 57 |
|
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: |
27 | 79 |
|
28 | 80 | ```bash
|
29 |
| -php artisan vendor:publish --tag="laravel-translator-migrations" |
30 |
| -php artisan migrate |
| 81 | +composer require stichoza/google-translate-php |
31 | 82 | ```
|
32 | 83 |
|
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: |
34 | 87 |
|
35 | 88 | ```bash
|
36 |
| -php artisan vendor:publish --tag="laravel-translator-config" |
| 89 | +composer require deeplcom/deepl-php |
37 | 90 | ```
|
38 | 91 |
|
39 |
| -This is the contents of the published config file: |
| 92 | +### OpenAI Translate |
40 | 93 |
|
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 |
44 | 98 | ```
|
45 | 99 |
|
46 |
| -Optionally, you can publish the views using |
| 100 | +### NLPCloud Translate |
| 101 | + |
| 102 | +To use NLPCloud's translation service, install their client package: |
47 | 103 |
|
48 | 104 | ```bash
|
49 |
| -php artisan vendor:publish --tag="laravel-translator-views" |
| 105 | +composer require nlpcloud/nlpcloud-client |
50 | 106 | ```
|
51 | 107 |
|
52 | 108 | ## Usage
|
53 | 109 |
|
| 110 | +### Basic Usage |
| 111 | + |
54 | 112 | ```php
|
55 |
| -$laravelTranslator = new Ferdiunal\LaravelTranslator(); |
56 |
| -echo $laravelTranslator->echoPhrase('Hello, Ferdiunal!'); |
57 |
| -``` |
| 113 | +use Ferdiunal\LaravelTranslator\Facades\Translator; |
58 | 114 |
|
59 |
| -## Testing |
| 115 | +// Translate a single text |
| 116 | +$translatedText = Translator::translate('Hello World', 'tr'); |
60 | 117 |
|
61 |
| -```bash |
62 |
| -composer test |
63 |
| -``` |
| 118 | +// Translate multiple texts |
| 119 | +$translations = Translator::translate(['Hello', 'World'], 'tr'); |
64 | 120 |
|
65 |
| -## Changelog |
| 121 | +// Specify source language |
| 122 | +$translatedText = Translator::from('en')->translate('Hello World', 'tr'); |
66 | 123 |
|
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 | +``` |
68 | 129 |
|
69 |
| -## Contributing |
| 130 | +### Working with Laravel Collections |
70 | 131 |
|
71 |
| -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 132 | +```php |
| 133 | +use Ferdiunal\LaravelTranslator\Facades\Translator; |
72 | 134 |
|
73 |
| -## Security Vulnerabilities |
| 135 | +$collection = collect([ |
| 136 | + 'title' => 'Hello World', |
| 137 | + 'description' => 'This is a description', |
| 138 | +]); |
74 | 139 |
|
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 |
76 | 147 |
|
77 |
| -## Credits |
| 148 | +```php |
| 149 | +{{-- Basic translation directive --}} |
| 150 | +{{ translate('Hello World', 'tr') }} |
78 | 151 |
|
79 |
| -- [Ferdi ÜNAL](https://github.com/ferdiunal) |
80 |
| -- [All Contributors](../../contributors) |
| 152 | +{{-- Use specific translation service --}} |
| 153 | +{{ translate('Hello World', 'tr', 'openai') }} |
| 154 | +``` |
81 | 155 |
|
82 | 156 | ## License
|
83 | 157 |
|
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. |
0 commit comments