PHP code example of cleargoal / laravel-deepl

1. Go to this page and download the library: Download cleargoal/laravel-deepl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cleargoal / laravel-deepl example snippets


use Cleargoal\LaravelDeepL\Facades\DeepL;

// Translate English to German
$translated = DeepL::translate('Hello, world!', 'en', 'de');
// Result: "Hallo, Welt!"

// Translate Ukrainian to French
$translated = DeepL::translate('Доброго ранку', 'uk', 'fr');
// Result: "Bon matin"

use Cleargoal\LaravelDeepL\Facades\DeepL;

$translations = DeepL::translateToAll('Welcome to our platform', 'en');

// Result:
// [
//     'en' => 'Welcome to our platform',
//     'uk' => 'Ласкаво просимо на нашу платформу',
//     'de' => 'Willkommen auf unserer Plattform',
//     'fr' => 'Bienvenue sur notre plateforme',
//     'es' => 'Bienvenido a nuestra plataforma'
// ]

use Cleargoal\LaravelDeepL\Facades\DeepL;

$detectedLang = DeepL::detectLanguage('Bonjour le monde');
// Result: 'fr'

$detectedLang = DeepL::detectLanguage('Hola mundo');
// Result: 'es'

use Cleargoal\LaravelDeepL\Services\DeepLTranslationService;

class TranslationController extends Controller
{
    public function __construct(
        private DeepLTranslationService $deepl
    ) {}

    public function translate(Request $request)
    {
        $translated = $this->deepl->translate(
            $request->input('text'),
            $request->input('source'),
            $request->input('target')
        );

        return response()->json(['translation' => $translated]);
    }
}

use Cleargoal\LaravelDeepL\Services\DeepLApiKeyManager;

$keyManager = app(DeepLApiKeyManager::class);

// Add key
$keyManager->addKey('your-api-key', 15, 'Production Key');

// Get all keys
$keys = $keyManager->getAllKeys();

// Check quota for specific key
$quota = $keyManager->checkQuota(0);

// Update key
$keyManager->updateKey(0, [
    'label' => 'Updated Label',
    'is_active' => false,
    'renewal_day' => 1
]);

// Delete key
$keyManager->deleteKey(0);

use Cleargoal\LaravelDeepL\Filament\Pages\ManageDeepLKeys as BaseManageDeepLKeys;

class ManageDeepLKeys extends BaseManageDeepLKeys
{
    protected static ?string $navigationGroup = 'Administration';

    protected static ?int $navigationSort = 10;

    // Add custom logic here
}

// In your FilamentPanelProvider or config
->authGuard('web')
->authMiddleware([
    Authenticate::class,
])

// app/Listeners/NotifyAdminsQuotaExhausted.php


namespace App\Listeners;

use App\Models\User;
use App\Notifications\DeepLQuotaExhausted;
use Illuminate\Support\Facades\Log;

class NotifyAdminsQuotaExhausted
{
    public function handle($event): void
    {
        Log::error('DeepL quota exhausted', [
            'source_locale' => $event['source_locale'],
            'target_locale' => $event['target_locale'],
            'keys_tried' => $event['keys_tried'],
            'next_renewal' => $event['next_renewal']?->format('Y-m-d'),
        ]);

        // Notify all administrators
        User::where('role', 'admin')->each->notify(
            new DeepLQuotaExhausted(
                $event['source_locale'],
                $event['target_locale'],
                $event['keys_tried'],
                $event['next_renewal']
            )
        );
    }
}

use Illuminate\Support\Facades\Event;

Event::listen('deepl.quota.exhausted', NotifyAdminsQuotaExhausted::class);

'quota_exhausted_callback' => function($sourceLocale, $targetLocale, $keyCount, $nextRenewal) {
    \App\Models\User::where('role', 'admin')->each->notify(
        new \App\Notifications\DeepLQuotaExhausted($sourceLocale, $targetLocale, $keyCount, $nextRenewal)
    );
},
bash
php artisan vendor:publish --tag=deepl-config
bash
php artisan vendor:publish --tag=deepl-migrations
bash
php artisan migrate
bash
php artisan deepl:add-key "your-deepl-api-key-here" 1 --label="Production Key"
bash
php artisan deepl:add-key "your-api-key" 15 --label="Production Key #2"
bash
php artisan deepl:list-keys
bash
php artisan deepl:check-quota
bash
php artisan deepl:check-quota --key=0
bash
php artisan deepl:remove-key 0
bash
php artisan vendor:publish --tag=deepl-views
bash
composer analyse