Photo by
Enter the SharpAPI AI Translator for Laravel Nova. This package seamlessly plugs
What Exactly Does This Package Do?
In a nutshell, it combines the for API access, but we’ll get to that.
Installation
Install the Package:
composer require sharpapi/nova-ai-translator
Configure API Access:
Add your API key from SharpAPI to your.env:
SHARP_API_KEY=your-sharp-api-key
Set Up Supported Languages:
Define your locales inconfig/app.phpunder thelocaleskey:
return [
'locales' => [
'en' => 'English',
'es' => 'Spanish',
'fr' => 'French',
// Add any other languages your app needs
],
];
Add to Your Nova Resource Models:
Your translatable models should use:
- The
HasTranslationstrait from Spatie.
[Highly Recommended] TheActionableandNotifiabletraits to track actions.
- The
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'];
}
Integrate the TranslateModel Action:
Hook theTranslateModelaction into your Nova resource by adding it to the actions array:
use SharpAPI\NovaAiTranslator\Actions\TranslateModel;
public function actions()
{
return [
(new TranslateModel())->enabled(),
];
}
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:
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
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 theActionableandNotifiabletraits.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
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 withspatie/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. 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
SOCIAL SHARE CARD GENERATOR