Installation
Install package
Add the package in your composer.json by executing the command.
composer require astrotomic/laravel-translatableConfiguration
We copy the configuration file to our project.
php artisan vendor:publish --tag=translatableAfter this you will have to configure the locales your app should use.
'locales' => [
'en',
'fr',
'es' => [
'MX', // mexican spanish
'CO', // colombian spanish
],
],That's the only configuration key you have to adjust. All the others have a working default value and are described in the configuration file itself.
Migrations
In this example, we want to translate the model Post. We will need an extra table post_translations:
Models
The translatable model Post should use the trait Astrotomic\Translatable\Translatable. The default convention for the translation model is PostTranslation. The array $translatedAttributes contains the names of the fields being translated in the PostTranslation model.
Custom foreign key
You may also define a custom foreign key for the package to use, e.g. in case of single table inheritance. So, you have a child class ChildPost that inherits from Post class, but has the same database table as its parent.
You will have to create a Translation Class for it.
This will try to get data from post_translations table using foreign key child_post_id according to Laravel. So, in this case, you will have to change the property $translationForeignKey to your 'post_id'.
Last updated
Was this helpful?