jetcod/laravel-model-translation

Laravel Model Translation simplifies managing model attribute translations in your Laravel applications.

v1.1.0 2024-10-23 08:47 UTC

This package is not auto-updated.

Last update: 2024-12-18 09:36:49 UTC


README

Actions Status

Latest Stable Version Total Downloads License

Laravel Translation is a package that provides a simple and efficient way to manage translations in your Laravel applications. It allows you to store translations of all your models attributes in database, making it easy to manage and update translations without modifying language files.

Installation

You can install the package via Composer:

composer require jetcod/laravel-model-translation

Configuration

After installing the package, you need to publish the configuration file and migration:

php artisan vendor:publish --tag=translation-config
php artisan vendor:publish --tag=translation-migrations

Then, run the migration to create the translations table:

php artisan migrate

In order to avoid conflictig with other packages and database tables, you can customize your database table name in the config/translation.php config file:

return [
    'database' => [
        'prefix' => env('TRANSLATION_TABLE_PREFIX', 'lt_'),
        'table_name' => env('TRANSLATION_TABLE_NAME', 'translations'),
    ],
];

Usage

Setup model

First, you need to import the Jetcode\Laravel\Translation\Traits\HasTranslations trait in your model and specify the fields you want to translate:

use Jetcode\Laravel\Translation\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    protected const TRANSLATABLE_ATTRIBUTES = ['title', 'content'];
}

Alternatively, you can specify the fields in a method in your model class:

use Jetcode\Laravel\Translation\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    protected function getTranslatableAttributes()
    {
        return ['title', 'content'];
    }
}

NOTE: The TRANSLATABLE_ATTRIBUTES constant or the getTranslatableAttributes method return value can be either a string or an array of the model attribute names.

Defining translatable attributes is optional, but it is recommended to define them.

Create a translation

Now, you can create translations for the model attributes through the defined relations:

// Create a new post with translations
$post = Post::create([
    'title' => 'Hello World',
    'content' => 'This is a post',
]);

// Create a new translation for the post
$post->translation()->saveMany([
    new Translation([
        'locale' => 'fr_FR',
        'key'    => 'title',
        'value'  => 'Bonjour le monde',
    ]),
    new Translation([
        'locale' => 'fr_FR',
        'key'    => 'content',
        'value'  => 'Ceci est un article',
    ]),
]);

Retrieve translated model

This package is compatible with Laravel localization system, so the models are translated according to the current locale. All you need to do is to set the appl locale and your model will be translated automatically:

$post = Post::find(123);
var_dump($post->title);     // "Hello World"

app()->setLocale('fr_FR');
var_dump($post->title);     // "Bonjour le monde"

Alternatively, you can use the withLocale method to retrieve the translated model for a specific locale:

var_dump($post->withLocale('fr_FR')->title); // "Bonjour le monde"

// Current locale is reset to default afterwards
var_dump($post->title); // "Hello World"

Testing

The package includes tests that you can run using PHPUnit:

composer test

You can also run static analysis with PHPStan:

composer phpstan

License

This package is open-source software licensed under the MIT license.