Skip to content

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

License

Notifications You must be signed in to change notification settings

jetcod/laravel-model-translation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Model Translation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages