mmidu / translatable
Automatic translation of json multilingual fields for Laravel.
Requires
- illuminate/support: ~5|~6|~7
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3|~4
- phpunit/phpunit: ^8.0
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2024-06-10 17:04:51 UTC
README
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!