sharpapi/nova-ai-translator

v1.0.0 2024-11-10 15:53 UTC

This package is auto-updated.

Last update: 2024-12-10 16:12:21 UTC


README

Welcome to SharpAPI AI Translator for Laravel Nova – the package that’s here to make your content translation smoother and smarter with the power of SharpAPI's Advanced Text Translator AI API and to leverage AI for Laravel. This package extends the already-awesome Spatie's laravel-translatable with a Nova action you can trigger from your resource’s edit or list views.

🤖 Initiate AI Translation can be run from either:

  • The Nova resources list view:

  • Or from the edit screen of an individual resource:

Here's the form you’ll use to dispatch the translation:

Requirements

  • Laravel: ^9.0+
  • Laravel Nova: 4.0+
  • PHP: 8.0+
  • spatie/laravel-translatable: used for detecting translatable fields

Installation & Configuration

1. Install the package via Composer

composer require sharpapi/nova-ai-translator

2. API Key Configuration

You’ll need an API key from SharpAPI.com. Add it to your .env file like so:

SHARP_API_KEY=your-sharp-api-key

3. Supported Locales Configuration

Add your supported locales in config/app.php under the locales key:

return [
   'locales' => [
       'en' => 'English',
       'es' => 'Spanish',
       'fr' => 'French',
       // Add other supported languages here
   ],
];

4. Add to Nova Resource Models

For any model you want to translate, make sure it:

  • Uses Spatie’s HasTranslations trait.
  • Specifies which attributes are translatable.
  • [OPTIONAL], yet Highly Recommended: Use Actionable and Notifiable traits to track actions and notifications. This ensures you can log and monitor translation progress effectively.

Example:

namespace App;

use Laravel\Nova\Actions\Actionable;
use Illuminate\Notifications\Notifiable;
use Spatie\Translatable\HasTranslations;

class BlogPost
{
   use Actionable, Notifiable, HasTranslations;

   protected $translatable = ['title', 'subtitle', 'content'];
}

5. Attach the Action in a Nova Resource

Add the TranslateModel action to any Nova resource, such as BlogPost:

use SharpAPI\NovaAiTranslator\Actions\TranslateModel;

public function actions()
{
   return [
       (new TranslateModel())->enabled(),
   ];
}

6. Queue Setup

The TranslateModel action runs as a queued job, which is essential for smooth, asynchronous processing. Make sure your app’s queue is up and running for best results.

Using the TranslateModel Action

  • From the resource’s edit screen, trigger the TranslateModel action.
  • A form will pop up where you can select the source and target languages and choose an optional tone.
  • Note: The action checks if all target language fields already contain content. If they do, you’ll be prompted to clear these fields first if you want a fresh AI-generated translation.
  • Once triggered, the action queues the job, and you can monitor its progress if your model uses the Actionable and Notifiable traits.

Tips

  • Translation logs: Helpful for tracking what was translated.

  • Error handling: With Laravel\Nova\Actions\Actionable, you get detailed logs if something goes awry.

Changelog

See CHANGELOG for recent updates.

Credits

License

Licensed under the MIT License – see the License File for details.

Follow SharpAPI for More!

Stay tuned for updates, tips, and tricks: