A Laravel package for translating Eloquent model attributes.
Branch | Status | Code Coverage |
---|---|---|
Main | ||
Staging | ||
Dev |
-
Install the package via Composer:
# First time installation composer require internetguru/translatable # For updating the package composer update internetguru/translatable
In Visual Studio Code you can simpy use Ctrl+Shift+B
to run the tests.
To run the tests manually, you can use the following commands:
# Build the Docker image
docker build -t laravel-translatable .
# Run the tests
docker run --rm laravel-translatable
# Both steps combined
docker build -t laravel-translatable . && docker run --rm laravel-translatable
-
Add the
Translatable
trait to your Eloquent model:use InternetGuru\Translatable\Translatable; class Room extends Model { use Translatable; }
-
Define the translatable attributes in the model. Do not use
$fillable
for translatable attributes:protected $translatable = ['name'];
-
Use translatable attributes:
app()->setLocale('en'); app()->setFallbackLocale('en'); $room = new Room(); $room->save(); // No translation set echo $room->name; // null $room->name = 'Living Room'; echo $room->name; // Living Room app()->setLocale('cs'); $room->name = 'Obývací pokoj'; echo $room->name; // Obývací pokoj app()->setLocale('en'); echo $room->name; // Living Room app()->setLocale('cs'); echo $room->name; // Obývací pokoj // Fallback to the fallback locale app()->setLocale('de'); echo $room->name; // Living Room // Fallback to the first translation if fallback locale not found app()->setFallbackLocale('fr'); echo $room->name; // Living Room
The MIT License (MIT). Please see License File for more information.