DEV Community

Cover image for Effortless Translations with AI in Laravel Nova
Dawid Makowski
Dawid Makowski

Posted on

Effortless Translations with AI in Laravel Nova

Photo by Cherry Lin on Unsplash

Enter the SharpAPI AI Translator for Laravel Nova. This package seamlessly plugs AI-powered translation directly into your Nova dashboard, eliminating repetitive translation tasks and freeing you up to focus on the good stuff.

Want to see all the package details? Head over to GitHub: https://github.com/sharpapi/nova-ai-translator

What Exactly Does This Package Do?

In a nutshell, it combines the Spatie’s laravel-translatable package with the superpowers of SharpAPI’s AI, transforming those content fields in your app into effortlessly translatable assets. The result? A new action on your Nova dashboard called 🤖 Initiate AI Translation that takes care of the translation work for you.

From the Nova resources list or the edit screen, you can queue up translations between any configured languages directly in Nova, with the AI taking over as soon as you hit the button. Need to translate a blog post from English to Spanish? It’s handled.

Who's This For?

If you're a Laravel Nova user managing content in multiple languages, this package is for you. It’s ideal for teams that regularly work with internationalized apps and need content quickly translated without manually flipping through Google Translate. Imagine all that time saved when your content auto-magically translates itself right from Nova!

Setting Up the SharpAPI AI Translator

Requirements

Make sure you’re running:

  • Laravel: ^9.0+
  • Laravel Nova: 4.0+
  • PHP: 8.0+
  • And have spatie/laravel-translatable installed

You’ll also need an account at SharpAPI.com for API access, but we’ll get to that.

Installation

  1. Install the Package:
   composer require sharpapi/nova-ai-translator
Enter fullscreen mode Exit fullscreen mode
  1. Configure API Access: Add your API key from SharpAPI to your .env:
   SHARP_API_KEY=your-sharp-api-key
Enter fullscreen mode Exit fullscreen mode
  1. Set Up Supported Languages: Define your locales in config/app.php under the locales key:
   return [
       'locales' => [
           'en' => 'English',
           'es' => 'Spanish',
           'fr' => 'French',
           // Add any other languages your app needs
       ],
   ];
Enter fullscreen mode Exit fullscreen mode
  1. Add to Your Nova Resource Models: Your translatable models should use:
    • The HasTranslations trait from Spatie.
    • [Highly Recommended] The Actionable and Notifiable traits to track actions.

Here’s a quick setup for, say, a BlogPost model:

   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'];
   }
Enter fullscreen mode Exit fullscreen mode
  1. Integrate the TranslateModel Action: Hook the TranslateModel action into your Nova resource by adding it to the actions array:
   use SharpAPI\NovaAiTranslator\Actions\TranslateModel;

   public function actions()
   {
       return [
           (new TranslateModel())->enabled(),
       ];
   }
Enter fullscreen mode Exit fullscreen mode
  1. Enable Queues: This action uses a queue to handle translations asynchronously, so make sure your queue is ready to go.

Using the TranslateModel Action in Nova

Once integrated, the action lives right in your Nova resource. Here’s how it works:

  1. Kickstart AI Translation: Open the action either from the resources list or from the edit view of any resource.

Example: Triggering the Action from the Edit View

  1. Select Translation Settings: A form lets you pick the source and target languages and even set the tone. You’ll also see a list of fields that will be translated, so there are no surprises.

  1. Hit Translate and Relax:
    Once you confirm, the action checks if the target fields are already populated. If they are, it gently suggests that you clear them before proceeding. Assuming all systems are go, it queues the translation job. You can even keep an eye on it if you’re using the Actionable and Notifiable traits.

  2. Track Progress and Logs:
    Nova’s action log feature helps track the translations. This is handy if you need to debug any issues or just like seeing AI in action.

Example: Translation Log in Action

Example: Error Handling (if it goes sideways)

Tips & Tricks

  • Set It and Forget It: This setup lets you queue translations without worrying about timing or load. It’s especially useful for scaling multilingual apps without scaling translation tasks.
  • Translation Strategy: Fine-tune how often you trigger translations based on the volume and frequency of content updates.
  • Localization Needs?: Since this setup integrates with spatie/laravel-translatable, you get the best of both worlds: structured localization with the muscle of an AI translation.

With SharpAPI AI Translator for Laravel Nova, your app’s translation game just got a massive upgrade with its new Laravel AI capabilities. Give it a spin, and let us know how it works for you!

Originally published at https://sharpapi.com/en/blog/post/effortless-translations-with-ai-in-laravel-nova

Top comments (0)