Skip to content
Snippets Groups Projects
Select Git revision
  • master default protected
1 result

TranslateCreationJob.php

Blame
    • Sofiane Lasri's avatar
      950b4a5c
      feat: add AI translation job for creation descriptions · 950b4a5c
      Sofiane Lasri authored
      - Added `TranslateCreationJob` to handle the translation of creation descriptions from French to English using an AI provider service.
      - Updated the `CreationController` to dispatch the translation job when requested via a new route.
      - Added a button in the admin creations index view to trigger the translation process, enhancing user interaction.
      - Included frontend JavaScript for button state handling during the translation request.
      Verified
      950b4a5c
      History
      feat: add AI translation job for creation descriptions
      Sofiane Lasri authored
      - Added `TranslateCreationJob` to handle the translation of creation descriptions from French to English using an AI provider service.
      - Updated the `CreationController` to dispatch the translation job when requested via a new route.
      - Added a button in the admin creations index view to trigger the translation process, enhancing user interaction.
      - Included frontend JavaScript for button state handling during the translation request.
    TranslateCreationJob.php 2.58 KiB
    <?php
    
    namespace App\Jobs;
    
    use App\Models\Creation;
    use App\Models\Translation;
    use App\Services\AiProviderService;
    use Exception;
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Facades\Log;
    
    class TranslateCreationJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
        public function __construct(private readonly Creation $creation) {}
    
        public function handle(AiProviderService $aiProviderService): void
        {
            $frenchShortDesc = $this->creation->shortDescriptionTranslationKey->getTranslation('fr');
            $frenchDesc = $this->creation->descriptionTranslationKey->getTranslation('fr');
    
            $shortDescriptionTranslationKeyId = $this->creation->shortDescriptionTranslationKey->id;
            $descriptionTranslationKeyId = $this->creation->descriptionTranslationKey->id;
    
            if (! empty($frenchShortDesc)) {
                Translation::updateOrCreate(
                    ['translation_key_id' => $shortDescriptionTranslationKeyId, 'locale' => 'en'],
                    ['text' => $this->translate($frenchShortDesc, $aiProviderService)]
                );
                Cache::forget("translation_key_{$shortDescriptionTranslationKeyId}_en");
            }
    
            if (! empty($frenchDesc)) {
                Translation::updateOrCreate(
                    ['translation_key_id' => $descriptionTranslationKeyId, 'locale' => 'en'],
                    ['text' => $this->translate($frenchDesc, $aiProviderService)]
                );
                Cache::forget("translation_key_{$descriptionTranslationKeyId}_en");
            }
        }
    
        /**
         * Translate the given text from French to English
         *
         * @param  string  $text  The text to translate
         * @param  AiProviderService  $aiProviderService  The AI provider service
         * @return string The translated text
         */
        private function translate(string $text, AiProviderService $aiProviderService): string
        {
            try {
                $result = $aiProviderService->prompt(
                    'You are a helpful assistant that translates french markdown text in english and that outputs JSON in the format {message:string}. Markdown is supported.',
                    $text
                );
    
                return $result['message'] ?? '';
            } catch (Exception $e) {
                Log::error("Failed to translate text: {$text}", [
                    'exception' => $e->getMessage(),
                ]);
            }
    
            return '';