Replaced TranslationCompletedListener
with AfterTranslateDto
across translation-related classes to streamline and enhance the job chaining logic. Added validation to ensure the provided class implements the required interface and included support for additional contextual data in the translation process. This change improves flexibility and simplifies the translation workflow.
This commit is contained in:
@@ -5,10 +5,11 @@ namespace KorElf\TranslateLaravel\Jobs;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use KorElf\TranslateLaravel\Contracts\TranslationCompletedListener;
|
||||
use KorElf\TranslateLaravel\DTO\AfterTranslateDto;
|
||||
use KorElf\TranslateLaravel\DTO\Translated;
|
||||
use KorElf\TranslateLaravel\Exceptions\AfterTranslateException;
|
||||
|
||||
@@ -20,15 +21,12 @@ final class AfterTranslate implements ShouldQueue, ShouldBeEncrypted
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $groupName,
|
||||
private readonly TranslationCompletedListener $listener,
|
||||
private readonly Translated $translated,
|
||||
private readonly string $groupName,
|
||||
private readonly Translated $translated,
|
||||
private readonly AfterTranslateDto $afterTranslateDto
|
||||
) { }
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
public function handle(Application $application): void
|
||||
{
|
||||
$translated = [];
|
||||
$data = Cache::get($this->groupName, []);
|
||||
@@ -43,11 +41,15 @@ final class AfterTranslate implements ShouldQueue, ShouldBeEncrypted
|
||||
$translated[$key] = implode(' ', $data[$key]);
|
||||
}
|
||||
|
||||
if (empty($data)) {
|
||||
if (empty($data) || !empty($errors)) {
|
||||
throw new AfterTranslateException('Part or all of the text has not been translated. Keys: ' . implode(', ', $errors));
|
||||
}
|
||||
Cache::forget($this->groupName);
|
||||
$objectAfterTranslate = $application->make($this->afterTranslateDto->getClassName());
|
||||
if (!$objectAfterTranslate instanceof \KorElf\TranslateLaravel\Contracts\TranslationCompletedListener) {
|
||||
throw new AfterTranslateException('The class must implement the \KorElf\TranslateLaravel\Contracts\TranslationCompletedListener interface.');
|
||||
}
|
||||
$objectAfterTranslate->onTranslationCompleted($translated, $this->afterTranslateDto->getData());
|
||||
|
||||
$this->listener->onTranslationCompleted($translated);
|
||||
Cache::forget($this->groupName);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user