kaantanis/filament-model-translatable

This is my package filament-model-translatable

v0.1.0-alpha 2024-08-15 08:06 UTC

This package is auto-updated.

Last update: 2024-10-19 07:18:45 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Filament model translatable is a package that provides a trait to make your models translatable in Laravel Filament. The data is stored in a new table with the model's id, the field name, the language, and the value.

Screenshot Screenshot

Installation

You can install the package via composer:

composer require kaantanis/filament-model-translatable

You can publish and run the migrations with:

php artisan vendor:publish --tag="filament-model-translatable-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="filament-model-translatable-config"

This is the contents of the published config file:

<?php
return [
    // except default locale on your app.php
    'supported_locales' => [
        'tr',
        'de',
    ],

    'cache_time' => 10, // in minutes
];

Usage

// model
use KaanTanis\FilamentModelTranslatable\Traits\ModelTranslatable;

class Post extends Model
{
    use ModelTranslatable;

    protected $translatable = [
        'title',
        'body',
    ];
}
// Resource
TextInput::make('title')
    ->translatable() // It works magically via macro
// Get the value
$model->title // It will return the value of the app()->getLocale()
$model->getTranslation('title', 'tr') // It will return the value of the target locale

// if the given locale does not exist from database, it will return the title of the model itself

Important Notice

When using the translatable method, the ->translatable() method must be called the last. Otherwise, other methods will not work.

When using Select component, be sure to use the ->options() method after the ->translatable() method.

// This will all work
TextInput::make('title')
    ->required()
    ->translatable()

// This will only main component field required but other cloned translatable components will not be require
// But still translatable
TextInput::make('title')
    ->translatable()
    ->required()

We need new ideas for version 2.0.0. Please open a discussion for your ideas.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.