As of my last update in September 2021, there is no official Laravel package called "laravel-translatable." However, there are several popular third-party packages available that provide translation support for Laravel models and databases. One such package is "spatie/laravel-translatable" by Spatie.

The "spatie/laravel-translatable" package allows you to create translatable Eloquent models, where certain attributes can have different values based on the locale or language. This is useful when building multilingual applications.

To use the "spatie/laravel-translatable" package, follow these steps:

  1. Installation: Install the package using Composer:

    composer require spatie/laravel-translatable
  2. Configuration: The package should automatically register its service provider, but if it doesn't, add it to the providers array in config/app.php:

    'providers' => [ // ... Spatie\Translatable\TranslatableServiceProvider::class, ],
  3. Model Setup: In your Eloquent model that needs translatable attributes, use the HasTranslations trait and define the $translatable property as an array with the names of the translatable attributes.

    namespace App\Models; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; class Product extends Model { use HasTranslations; protected $translatable = ['name', 'description']; }
  4. Database Migration: Make sure to add the translatable attributes to the migration of the corresponding model. You can use the json data type in your migration to store the translations.

    Schema::create('products', function (Blueprint $table) { $table->id(); $table->json('name'); $table->json('description'); // Other non-translatable columns $table->timestamps(); });
  5. Usage: You can now work with your translatable attributes as if they were regular attributes. The package will automatically handle the translations based on the current locale.

    // Creating a new translatable model $product = new Product(); $product->name = ['en' => 'English Name', 'fr' => 'French Name']; $product->description = ['en' => 'English Description', 'fr' => 'French Description']; $product->save(); // Accessing translatable attributes $productName = $product->name; $productDescription = $product->description; // Changing the locale app()->setLocale('fr'); $productNameInFrench = $product->name;

The "spatie/laravel-translatable" package simplifies the process of working with translatable attributes in Laravel models. It is well-documented and actively maintained by Spatie, so you can refer to its documentation for more details and advanced usage:

Have questions or queries?
Get in Touch