ilyasapunkov/orchid-translatable

A Laravel-orchid package for translatable models using JSON fields

0.0.4 2025-02-24 11:20 UTC

This package is auto-updated.

Last update: 2025-05-24 11:48:41 UTC


README

A Laravel Orchid package for managing translatable models using JSON fields.

Installation

  1. Install the package via Composer:

    composer require ilyasapunkov/orchid-translatable
    
  2. Publish the migration:

    php artisan vendor:publish --tag=translatable-migrations
    
  3. Run the migration:

    php artisan migrate
    

Usage

  1. Use the Translatable trait in your model:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use IlyaSapunkov\Translatable\Traits\Translatable;
    
    class Post extends Model
    {
        use Translatable;
    
        protected $translatableFields = ['title', 'description'];
    }
  2. Set translations:

    $post = Post::create();
    
    $post->syncTranslation([
      'ru' => [
           'title' => 'Заголовок на русском',
           'description' => 'Описание на русском',
       ]
    ]);
  3. Get translations:

    echo $post->title; // Заголовок на русском (если текущая локаль 'ru')
    echo $post->description; // Описание на русском
  4. Filter by translations:

    $posts = Post::hasTranslation('title')->get();
    $posts = Post::hasTranslation('title', 'en')->get();
  5. Orchid

In Orchid/PlatformProvider.php add menu

   public function menu(): array
    {
      return [
      //...
      Menu::make(__('app.Locales'))
         ->icon('bs.globe')
         ->route('translatable.locales')
         ->permission('translatable.locales'),
      //...
      ];
    }

In routes/platform.php add

// Platform > Content > Locales
    Route::screen('locales', LocaleListScreen::class)
        ->name('translatable.locales')
        ->breadcrumbs(fn (Trail $trail) => $trail
            ->parent('platform.index')
            ->push(__('app.Locales'), route('translatable.locales')));

// Platform > Content > Locales > Locale
    Route::screen('locales/{model}/edit', LocaleEditScreen::class)
        ->name('translatable.locales.edit')
        ->breadcrumbs(fn (Trail $trail, $model) => $trail
            ->parent('translatable.locales')
            ->push($model->name, route('translatable.locales.edit', $model)));

// Platform > Content > Locales > Create
    Route::screen('locales/create', LocaleCreateScreen::class)
        ->name('translatable.locales.create')
        ->breadcrumbs(fn (Trail $trail) => $trail
            ->parent('translatable.locales')
            ->push(__('Create'), route('translatable.locales.create')));