ferdiunal / laravel-translator
This package allows you to use freely available online translation tools in your project.
Fund package maintenance!
ferdiunal
Buymeacoffee
Requires
- php: ^8.2
- deeplcom/deepl-php: ^1.9
- illuminate/contracts: ^10.0||^11.0||^12.0
- openai-php/client: ^0.10.3
- spatie/laravel-package-tools: ^1.16
- stichoza/google-translate-php: ^5.2
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
This package is auto-updated.
Last update: 2025-04-09 23:04:22 UTC
README
Laravel Translator
A powerful and flexible translation package for Laravel applications that supports multiple translation services including OpenAI, DeepL, Google Translate, and NLPCloud.
Installation
composer require ferdiunal/laravel-translator
Configuration
First, publish the configuration file:
php artisan vendor:publish --provider="Ferdiunal\LaravelTranslator\LaravelTranslatorServiceProvider"
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:
return [ 'deepl' => [ 'api_key' => env('DEEPL_API_KEY'), ], 'nlpCloud' => [ 'api_key' => env('NLPCLOUD_API_KEY'), 'model' => env('NLPCLOUD_MODEL', 'nllb-200-3-3b'), 'languages' => [ 'az' => 'azj_Latn', 'de' => 'deu_Latn', 'en' => 'eng_Latn', 'es' => 'spa_Latn', 'it' => 'ita_Latn', 'pt' => 'por_Latn', 'tr' => 'tur_Latn', 'ru' => 'rus_Cyrl', ], ], 'openai' => [ 'api_key' => env('OPENAI_API_KEY'), 'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'), 'model' => env('OPENAI_MODEL', 'gpt-4'), 'messages' => [ [ 'role' => 'system', 'content' => 'You are an assistant who translates the text from English to Turkish. Just return the translated output.', ], ], ], ];
Add the following environment variables to your .env
file based on the services you want to use:
# DeepL Configuration DEEPL_API_KEY=your-deepl-api-key # NLPCloud Configuration NLPCLOUD_API_KEY=your-nlpcloud-api-key NLPCLOUD_MODEL=nllb-200-3-3b # OpenAI Configuration OPENAI_API_KEY=your-openai-api-key OPENAI_BASE_URL=https://api.openai.com/v1 OPENAI_MODEL=gpt-4
Available Translation Services
Google Translate
To use Google Translate, install the required package:
composer require stichoza/google-translate-php
DeepL Translate
To use DeepL Translate, install the required package and set up your API key:
composer require deeplcom/deepl-php
OpenAI Translate
To use OpenAI's translation capabilities, install the official PHP package:
composer require openai/openai-php
NLPCloud Translate
To use NLPCloud's translation service, install their client package:
composer require nlpcloud/nlpcloud-client
Usage
Basic Usage
use Ferdiunal\LaravelTranslator\Facades\Translator; // Translate a single text $translatedText = Translator::translate('Hello World', 'tr'); // Translate multiple texts $translations = Translator::translate(['Hello', 'World'], 'tr'); // Specify source language $translatedText = Translator::from('en')->translate('Hello World', 'tr'); // Use specific translation service $translatedText = Translator::using('openai')->translate('Hello World', 'tr'); $translatedText = Translator::using('deepl')->translate('Hello World', 'tr'); $translatedText = Translator::using('nlpcloud')->translate('Hello World', 'tr');
Working with Laravel Collections
use Ferdiunal\LaravelTranslator\Facades\Translator; $collection = collect([ 'title' => 'Hello World', 'description' => 'This is a description', ]); // Translate all values in a collection $translatedCollection = $collection->map(function ($text) { return Translator::translate($text, 'tr'); });
Blade Integration
{{-- Basic translation directive --}} {{ translate('Hello World', 'tr') }} {{-- Use specific translation service --}} {{ translate('Hello World', 'tr', 'openai') }}
License
The MIT License (MIT). Please see License File for more information.