Installation:
composer require vinkla/translator
Publish the config file:
php artisan vendor:publish --provider="Vinkla\Translator\TranslatorServiceProvider"
Model Setup:
Extend your Eloquent model with Vinkla\Translator\Traits\Translatable:
use Vinkla\Translator\Traits\Translatable;
class Post extends Model
{
use Translatable;
protected $translatable = ['title', 'content'];
}
First Use Case: Save translations for a model:
$post = new Post();
$post->title = 'English Title';
$post->content = 'English Content';
$post->translate('es', [
'title' => 'Título en Español',
'content' => 'Contenido en Español'
]);
$post->save();
Retrieving Translations:
$post->getTranslation('es', 'title'); // Returns 'Título en Español'
Dynamic Translation Handling:
Use translate() to add translations dynamically:
$post->translate($locale, $attributes);
Fallback Logic:
Implement fallback locales in config/translator.php:
'fallback_locales' => ['en', 'es'],
Then retrieve translations with fallback:
$post->getTranslation('fr', 'title'); // Falls back to 'en' if 'fr' missing
Query Scopes: Filter models by translation attributes:
$posts = Post::whereTranslation('title', 'like', '%Spanish%')->get();
Batch Translation Updates:
Use updateTranslations() for bulk updates:
$post->updateTranslations('es', ['title' => 'New Title']);
Locale-Specific Validation: Validate translations per locale:
$validator = Validator::make($request->all(), [
'title_en' => 'required|string|max:255',
'title_es' => 'required|string|max:255',
]);
return response()->json($post->getTranslations());
public function handle($request, Closure $next)
{
app()->setLocale($request->header('Accept-Language'));
return $next($request);
}
Archived Package:
spatie/laravel-translatable for new projects.Database Schema:
translations table with columns: translatable_id, locale, attributes.Locale Conflicts:
en_US)—use hyphens (en-us) to prevent SQL issues.Performance:
$posts = Post::withTranslations()->get();
Caching:
Cache::remember("post.{$post->id}.translations", now()->addHours(1), function() use ($post) {
return $post->getTranslations();
});
Missing Translations:
Check if the translatable array in your model includes all fields you’re translating.
// Debug:
dd($post->getTranslations());
SQL Errors:
Verify the translations table exists and has the correct columns. Run:
php artisan schema:dump
to inspect the schema.
Locale-Specific Issues: Ensure the locale is correctly set before querying:
app()->setLocale('es');
$post->getTranslation('title'); // Works if locale is 'es'
Custom Storage:
Override the getTranslationsTable() method to use a custom table:
protected function getTranslationsTable()
{
return 'custom_translations';
}
Translation Events:
Listen for translatable.saving and translatable.saved events:
Event::listen('translatable.saving', function ($model, $locale, $attributes) {
// Pre-save logic
});
Custom Query Builder: Extend the query builder for advanced filtering:
public function scopeWhereTranslationLike($query, $field, $value)
{
return $query->whereHas('translations', function ($q) use ($field, $value) {
$q->where('locale', app()->getLocale())
->whereJsonContains($field, $value);
});
}
How can I help you explore Laravel packages today?