aaix/eloquent-translatable

High performance translations for Laravel models.

v1.0.2 2025-07-25 10:47 UTC

This package is auto-updated.

Last update: 2025-07-25 11:03:03 UTC


README

Laravel Eloquent Translatable Logo

Laravel Eloquent Translatable

High performance, developer-first translations for Laravel models.

Latest Version on Packagist Total Downloads GitHub Actions License

Eloquent Translatable is a Laravel package built for raw performance and a clean, focused developer experience. It uses direct, indexed database queries instead of relying on JSON columns or complex Eloquent model hydration, making it significantly faster and more memory-efficient than other solutions.

Key Features

  • 🚀 Performance-First: Designed for speed at scale. No Eloquent overhead, no JSON parsing.
  • ✨ Intuitive API: A clean, fluent, and predictable interface.
  • 🤝 Spatie-Compatible: Optional API compatibility with spatie/laravel-translatable.
  • 🔒 Secure by Default: Explicitly define which attributes are translatable.
  • ⚙️ Artisan Command: Scaffold translation migrations with a single command.
  • 🛡️ Enum-Powered: Ships with a Locale enum for type-safe, readable code.

Documentation

For the full documentation, please visit our documentation website.

Installation

You can install the package via Composer:

composer require aaix/eloquent-translatable

Quick Example

  1. Prepare your model:

    // app/Models/Product.php
    namespace App\Models;
    
    use Aaix\EloquentTranslatable\Traits\HasTranslations;
    use Illuminate\Database\Eloquent\Model;
    
    class Product extends Model
    {
        use HasTranslations;
    
        public array $translatable = ['name', 'description'];
    }
  2. Store and access translations:

    use Aaix\EloquentTranslatable\Enums\Locale;
    
    $product = Product::create(['name' => 'My awesome product']);
    
    // Store a translation
    $product->storeTranslation('name', Locale::GERMAN, 'Mein tolles Produkt');
    
    // Access it (will fall back to the app's locale)
    app()->setLocale('de');
    echo $product->name; // Output: Mein tolles Produkt
    
    // Or get a specific locale
    echo $product->getTranslation('name', Locale::GERMAN); // Output: Mein tolles Produkt

Testing

To run the package's test suite, clone the repository and run:

composer install
composer test

Contributing

Contributions are welcome!

License

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