astrotomic/laravel-translatable
Laravel package for translatable Eloquent models. Store model translations in the database and automatically fetch/save multilingual attributes based on locale, reducing boilerplate when working with multi-language content.
Strengths:
translateOrDefault) ensure graceful degradation when translations are missing, improving UX resilience.$post->translate('en')->title) aligns with Laravel’s magic methods, reducing cognitive load for developers.translations array), and locale hierarchies (e.g., es_MX), making it adaptable to complex multilingual needs.Weaknesses:
spatie/laravel-translation) if used simultaneously.getTranslation() results).en, fr) or dynamic (e.g., user-selected)? Does the app need locale hierarchies (e.g., es_MX)?spatie/laravel-translation) preferable?en → es → default)?en, es, de) with fallback logic.Post, Product).posts_translations) with locale and translated fields.Schema::create('posts_translations', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->string('locale')->index();
$table->string('title');
$table->text('content');
$table->unique(['post_id', 'locale']);
});
Translatable trait to models and define $translatedAttributes.class Post extends Model {
use Translatable;
public $translatedAttributes = ['title', 'content'];
}
update or custom scripts).Post::query()->each(function ($post) {
$post->translate('en')->title = $post->title;
$post->save();
});
translatable.php:
'locales' => ['en', 'fr', 'es'],
'fallback_locale' => 'en',
'translations_wrapper' => null, // or 'translations'
$post->translate('fr')->title).$post->translate('it') → en).Post, Product).Post::create(['title' => ['en' => 'Hello']])).Post::withTranslations()->get();
Translatable trait, reducing duplication.Translatable trait and model relationships.How can I help you explore Laravel packages today?