1. Go to this page and download the library: Download pavelzanek/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/ */
use PavelZanek\LaravelDeepl\DeeplClient;
$client = new DeeplClient();
$translatedText = $client->translateText()
->text('Hello, world!')
->sourceLang('en')
->targetLang('de')
->translate();
echo $translatedText; // Outputs: Hallo, Welt! (from cache or API)
use PavelZanek\LaravelDeepl\Facades\Deepl;
$translatedText = Deepl::translateText(
'Hello, world!',
'en',
'de',
[], // No additional options
false // Disables cache usage for this request
)->translate();
// or if u prefer named parameters
$translatedText = Deepl::translateText(
text: 'Hello, world!',
sourceLang: 'en',
targetLang: 'de',
useCache: false // Disables cache usage for this request
)->translate();
echo $translatedText; // Always fetches from API
use PavelZanek\LaravelDeepl\Facades\Deepl;
$translatedText = Deepl::translateText()
->text('Hello, world!')
->sourceLang('en')
->targetLang('de')
->withoutCache() // Disables cache usage for this request
->translate();
echo $translatedText; // Always fetches from API
use PavelZanek\LaravelDeepl\Facades\Deepl;
$translatedTexts = Deepl::translateText()
->texts(['Hello, world!', 'Good morning!'])
->sourceLang('en')
->targetLang('de')
->translate();
foreach ($translatedTexts as $translatedText) {
echo $translatedText->text . "\n"; // Outputs: Hallo, Welt! and Guten Morgen!
}
use PavelZanek\LaravelDeepl\Facades\Deepl;
// Upload the document for translation
$uploadResponse = Deepl::uploadDocument('path/to/document.pdf', 'de');
// Check the status of the translation
$status = Deepl::getDocumentStatus($uploadResponse['document_id'], $uploadResponse['document_key']);
if ($status->status === \PavelZanek\LaravelDeepl\Enums\V2\DocumentStatus::DONE->value) {
// Download the translated document
$translatedDocument = Deepl::downloadDocument($uploadResponse['document_id'], $uploadResponse['document_key']);
file_put_contents('path/to/translated-document.pdf', $translatedDocument);
}
use PavelZanek\LaravelDeepl\DeeplClient;
$client = app(DeeplClient::class);
// Upload the document for translation
$uploadResponse = $client->uploadDocument('path/to/document.pdf', 'de');
// Check the status of the translation
$status = $client->getDocumentStatus($uploadResponse['document_id'], $uploadResponse['document_key']);
if ($status->status === \PavelZanek\LaravelDeepl\Enums\V2\DocumentStatus::DONE->value) {
// Download the translated document
$translatedDocument = $client->downloadDocument($uploadResponse['document_id'], $uploadResponse['document_key']);
file_put_contents('path/to/translated-document.pdf', $translatedDocument);
}
use PavelZanek\LaravelDeepl\Facades\Deepl;
// Perform a document translation with chainable methods
$status = Deepl::translateDocumentBuilder()
->inputFile('path/to/input.docx')
->outputFile('path/to/output.docx')
->sourceLang('en')
->targetLang('de')
->enableMinification()
->options([
'formality' => 'more',
// Add other options as needed
])
->translate();
echo $status->status; // Outputs: done, translating, etc.
use PavelZanek\LaravelDeepl\DeeplClient;
$client = app(DeeplClient::class);
// Perform a document translation with chainable methods
$status = $client->translateDocumentBuilder()
->inputFile('path/to/input.pdf')
->outputFile('path/to/output.pdf')
->sourceLang('en')
->targetLang('fr')
->translate();
echo $status->status; // Outputs: done, translating, etc.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Translation\Loader as LoaderContract;
use Illuminate\Translation\FileLoader;
use Illuminate\Filesystem\Filesystem;
use PavelZanek\LaravelDeepl\Services\TranslationService;
use PavelZanek\LaravelDeepl\Services\TranslatorService;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
// Register this only if you intend to utilize the queue for handling translations
$this->app->singleton(LoaderContract::class, function ($app) {
return new FileLoader(new Filesystem(), lang_path());
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
$this->app->extend('translator', function ($translator, $app) {
$loader = $app['translation.loader'];
$translationService = $app->make(TranslationService::class);
return new TranslatorService($loader, $app, $translationService);
});
}
}
use PavelZanek\LaravelDeepl\Facades\Deepl;
use PavelZanek\LaravelDeepl\Enums\V2\Formality;
use PavelZanek\LaravelDeepl\Enums\V2\SourceLanguage;
use PavelZanek\LaravelDeepl\Enums\V2\TargetLanguage;
$translatedText = Deepl::translateText()
->text('How are you today?')
->sourceLang(SourceLanguage::ENGLISH->value)
->targetLang(TargetLanguage::GERMAN->value)
->formality(Formality::PREFER_LESS->value)
->translate();
echo $translatedText; // Outputs: Wie geht es dir heute?