Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Translatable Laravel Package

astrotomic/laravel-translatable

Laravel package for translatable Eloquent models. Store model translations in the database and automatically fetch/save multilingual attributes based on locale, reducing boilerplate when working with multi-language content.

View on GitHub
Deep Wiki
Context7

Installation

Install package

Add the package in your composer.json by executing the command.

composer require astrotomic/laravel-translatable

Configuration

We copy the configuration file to our project.

php artisan vendor:publish --tag=translatable

After this you will have to configure the locales your app should use.

'locales' => [
    'en',
    'fr',
    'es' => [
        'MX', // mexican spanish
        'CO', // colombian spanish
    ],
],

{% hint style="info" %} There isn't any restriction for the format of the locales. Feel free to use whatever suits you better, like "eng" instead of "en", or "el" instead of "gr". The important is to define your locales and stick to them. {% endhint %}

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:

{% code title="create_posts_table.php" %}

Schema::create('posts', function(Blueprint $table) {
    $table->increments('id');
    $table->string('author');
    $table->timestamps();
});

{% endcode %}

{% code title="create_post_translations_table" %}

Schema::create('post_translations', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('post_id')->unsigned();
    $table->string('locale')->index();
    $table->string('title');
    $table->text('content');

    $table->unique(['post_id', 'locale']);
    $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});

{% endcode %}

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.

{% code title="Post.php" %}

use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Astrotomic\Translatable\Translatable;

class Post extends Model implements TranslatableContract
{
    use Translatable;

    public $translatedAttributes = ['title', 'content'];
    protected $fillable = ['author'];
}

{% endcode %}

{% code title="PostTranslation.php" %}

class PostTranslation extends Model
{
    public $timestamps = false;
    protected $fillable = ['title', 'content'];
}

{% endcode %}

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.

{% code title="ChildPost.php" %}

class ChildPost extends Post
{
    protected $table = 'posts';
}

{% endcode %}

You will have to create a Translation Class for it.

{% code title="ChildPostTranslation.php" %}

use Illuminate\Database\Eloquent\Model;

class ChildPostTranslation extends Model
{
    protected $table = 'post_translations';
    public $timestamps = false;
    protected $fillable = ['title', 'content'];
}

{% endcode %}

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'.

{% code title="ChildPost.php" %}

class ChildPost extends Post
{
    protected $table = 'posts';
    protected $translationForeignKey = 'post_id';
}

{% endcode %}

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope