roundly-consulting / translatable-for-laravel
Locale-map (jsonb) translatable attributes with a fallback chain for Eloquent; per-locale slugs via sluggable-for-laravel — native, zero third-party deps.
Package info
github.com/roundly-consulting/translatable-for-laravel
pkg:composer/roundly-consulting/translatable-for-laravel
Fund package maintenance!
Requires
- php: ^8.4
- ext-json: *
- illuminate/console: ^12.0|^13.0
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
- illuminate/validation: ^12.0|^13.0
- illuminate/view: ^12.0|^13.0
- roundly-consulting/enums-for-laravel: ^1.0
- roundly-consulting/package-toolkit-for-laravel: ^1.0
- roundly-consulting/sluggable-for-laravel: ^1.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.0
- nunomaduro/collision: ^8.0
- orchestra/testbench: ^10.0|^11.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- roundly-consulting/testing-for-laravel: ^1.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Translatable for Laravel
Translatable Eloquent attributes stored as a plain { "en": "…", "sk": "…" } locale map in one
json/jsonb column. Reads return the current locale through a configurable fallback chain, so
content never renders blank, and per-locale slugs come built in.
Installation
Requires PHP 8.4, Laravel 12 or 13, and any Laravel-supported database.
composer require roundly-consulting/translatable-for-laravel
Usage
Give each translatable attribute one locale-map column and list it on the model:
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use RoundlyConsulting\Translatable\Concerns\HasTranslations; use RoundlyConsulting\Translatable\Contracts\Translatable; Schema::create('topics', function (Blueprint $table): void { $table->id(); $table->translatable('name'); // a jsonb column $table->timestamps(); }); final class Topic extends Model implements Translatable { use HasTranslations; /** @var list<string> */ public array $translatable = ['name']; }
Read and write in the current locale:
$topic = Topic::create(['name' => ['en' => 'Investing']]); app()->setLocale('sk'); $topic->name; // 'Investing' — falls back to `en` $topic->missingLocales('name'); // ['sk'] $topic->setTranslation('name', 'sk', 'Investovanie')->save(); $topic->name; // 'Investovanie' $topic->getTranslations('name'); // ['en' => 'Investing', 'sk' => 'Investovanie']
The facade handles everything that isn't per-model state:
use RoundlyConsulting\Translatable\Facades\Translatable; Translatable::usingLocale('en', fn (): string => $topic->name); // 'Investing' $topic->setTranslations('name', Translatable::fromInput($request->input('name'))); // clean admin input
Documentation
The full documentation — configuration, every feature and its API, and testing — lives on our website: roundly-consulting.com/open-source/docs/translatable-for-laravel
Release notes are in CHANGELOG.md. To contribute, see the contributing guide.
Support our work
This package is free and open source, built and maintained by Roundly Consulting. If it saves you time, please consider supporting our open-source work — a one-time donation, a monthly pledge on Patreon or a crypto donation helps fund maintenance, new features and new packages.
License
The MIT License (MIT). Copyright (c) roundly-consulting. See LICENSE.md.