internetguru / laravel-translatable
v0.2.0
2025-02-19 01:02 UTC
Requires
- laravel/framework: ^11.0
Requires (Dev)
- laravel/pint: ^1.18
- orchestra/testbench: ^9.5
- phpunit/php-code-coverage: ^11.0
This package is auto-updated.
Last update: 2025-02-19 01:03:21 UTC
README
A Laravel package for translating Eloquent model attributes.
Installation
-
Install the package via Composer:
# First time installation composer require internetguru/translatable # For updating the package composer update internetguru/translatable
Run Tests Locally
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
Usage
-
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
License
The MIT License (MIT). Please see License File for more information.