Automatic translation of json multilingual fields for Laravel.
mmidu/translatable is a Laravel package for automatic translation of json multilingual fields for laravel..
It currently has 0 GitHub stars and 32 downloads on Packagist.
Install it with composer require mmidu/translatable.
Discover more Laravel packages by mmidu
or browse all Laravel packages to compare alternatives.
Last updated
Automatic translation of json multilingual fields for Laravel.
usage:
add the service provider to config/app.php in the "providers" array like this. 'providers' => [ ... Mmidu\Translatable\TranslatableServiceProvider::class, ... ],
refresh the config cache with the command
php artisan config:cache
and publish the config file with the command
php artisan vendor:publish --provider "Mmidu\Translatable\TranslatableServiceProvider"
use the Translatable trait to the model you want to use it for.
set the languages config in the config/translatable.php file, you can add and remove locales.
the locale used for the translation has to be put in the session.
add the translatable fields in the model's translatable attribute like this
protected $translatable = [ 'name', 'description' ];
the translatable fields must be JSON with the following structure:
{ "it": "nome", "en": "name", "es": "nombre" etc... }
access the right locale version through $model->attribute automatically.
es. Session::put('locale', 'es'); echo $model->name;
result = 'nombre'
Translatable adds two virtual attributes, multilingual_attribute and mulitilingual_attribute_str which give the multilingual version of the translatable attributes in array version. multilingual_attribute gives [] if there are no values, multilingual_attribute_str gives an empty string.
Enjoy!