Convert Figma logo to code with AI

spatie logolaravel-translatable

Making Eloquent models translatable

2,217
276
2,217
2

Top Related Projects

[Deprecated] A Laravel package for multilingual models

Easy localization for Laravel

Manage Laravel translation files

Quick Overview

Laravel Translatable is a package that adds multi-language support to Eloquent models in Laravel applications. It allows you to easily store and retrieve translated content for your models, providing a flexible and efficient way to manage multilingual data in your database.

Pros

  • Easy integration with existing Laravel projects
  • Supports fallback locales for missing translations
  • Provides a clean and intuitive API for working with translations
  • Allows for dynamic locale switching without changing the database structure

Cons

  • Requires additional database columns for each translatable attribute
  • May increase query complexity when filtering or sorting by translated attributes
  • Limited support for advanced querying on translated fields
  • Potential performance impact on large datasets with many translations

Code Examples

  1. Defining a translatable model:
use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations;

    public $translatable = ['name', 'description'];
}
  1. Setting and getting translated attributes:
$product = new Product();
$product->setTranslation('name', 'en', 'Laptop');
$product->setTranslation('name', 'nl', 'Laptop');
$product->save();

echo $product->getTranslation('name', 'en'); // Output: Laptop
  1. Querying translatable models:
$products = Product::whereTranslation('name', 'Laptop')->get();

Getting Started

  1. Install the package via Composer:
composer require spatie/laravel-translatable
  1. Add the HasTranslations trait to your model and define translatable attributes:
use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations;

    public $translatable = ['name', 'description'];
}
  1. Use the translation methods in your application:
$product = Product::create([
    'name' => ['en' => 'My Product', 'nl' => 'Mijn Product'],
    'description' => ['en' => 'Description', 'nl' => 'Beschrijving'],
]);

echo $product->getTranslation('name', 'nl'); // Output: Mijn Product

Competitor Comparisons

[Deprecated] A Laravel package for multilingual models

Pros of laravel-translatable

  • More mature and established project with a larger community
  • Supports fallback locales out of the box
  • Offers more advanced features like translation caching

Cons of laravel-translatable

  • More complex setup and configuration
  • Heavier impact on database structure (separate translation tables)
  • Steeper learning curve for beginners

Code Comparison

laravel-translatable:

class Post extends Model
{
    use \Dimsav\Translatable\Translatable;

    public $translatedAttributes = ['title', 'content'];
}

spatie/laravel-translatable:

class Post extends Model
{
    use Spatie\Translatable\HasTranslations;

    public $translatable = ['title', 'content'];
}

Both packages offer similar functionality for model translation, but spatie/laravel-translatable provides a more straightforward implementation with less boilerplate code. The main difference lies in the database structure and how translations are stored. laravel-translatable uses separate translation tables, while spatie/laravel-translatable stores translations in JSON columns within the same table.

Choose laravel-translatable for more advanced features and established community support, or opt for spatie/laravel-translatable for a simpler, lightweight solution with easier integration into existing projects.

Easy localization for Laravel

Pros of laravel-localization

  • Provides URL-based localization, making it easier to manage language-specific routes
  • Includes middleware for automatic language detection and switching
  • Offers helper functions for language-related operations in views and controllers

Cons of laravel-localization

  • Requires more configuration and setup compared to laravel-translatable
  • May have a steeper learning curve for developers new to Laravel localization
  • Less flexible for complex, database-driven content translations

Code Comparison

laravel-localization:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
    Route::get('/', 'HomeController@index');
});

{{ LaravelLocalization::getLocalizedURL('es', null, [], true) }}

laravel-translatable:

$post = new Post();
$post->setTranslation('title', 'en', 'My English Title');
$post->setTranslation('title', 'es', 'Mi Título en Español');
$post->save();

echo $post->getTranslation('title', 'es');

Both packages offer unique approaches to Laravel localization. laravel-localization focuses on URL-based language switching and route management, while laravel-translatable provides a more streamlined approach for translating model attributes. The choice between them depends on your project's specific requirements and complexity.

Manage Laravel translation files

Pros of laravel-translation-manager

  • Provides a user-friendly web interface for managing translations
  • Supports automatic translation using external services like Google Translate
  • Allows for easy import/export of translation files

Cons of laravel-translation-manager

  • Focuses on file-based translations rather than database-stored translations
  • May require more setup and configuration compared to laravel-translatable
  • Less suitable for dynamic content translation in models

Code Comparison

laravel-translation-manager:

use Barryvdh\TranslationManager\Models\Translation;

$translations = Translation::where('group', 'messages')->get();
foreach ($translations as $translation) {
    echo $translation->key . ': ' . $translation->value;
}

laravel-translatable:

use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations;
    
    public $translatable = ['name', 'description'];
}

$product->setTranslation('name', 'en', 'English Name');
echo $product->getTranslation('name', 'en');

laravel-translation-manager is better suited for managing static translations across an entire application, while laravel-translatable excels at handling translatable content within database models. The choice between the two depends on the specific needs of your project and whether you prioritize a GUI for translation management or seamless integration with Eloquent models.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

A trait to make Eloquent models translatable

Latest Version on Packagist MIT Licensed GitHub Workflow Status Total Downloads

This package contains a trait HasTranslations to make Eloquent models translatable. Translations are stored as json. There is no extra table needed to hold them.

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class NewsItem extends Model
{
    use HasTranslations;
    
    // ...
}

After the trait is applied on the model you can do these things:

$newsItem = new NewsItem;
$newsItem
   ->setTranslation('name', 'en', 'Name in English')
   ->setTranslation('name', 'nl', 'Naam in het Nederlands')
   ->save();

$newsItem->name; // Returns 'Name in English' given that the current app locale is 'en'
$newsItem->getTranslation('name', 'nl'); // returns 'Naam in het Nederlands'

app()->setLocale('nl');

$newsItem->name; // Returns 'Naam in het Nederlands'

// If you want to query records based on locales, you can use the `whereLocale` and `whereLocales` methods.

NewsItem::whereLocale('name', 'en')->get(); // Returns all news items with a name in English

NewsItem::whereLocales('name', ['en', 'nl'])->get(); // Returns all news items with a name in English or Dutch

// Returns all news items that has name in English with value `Name in English` 
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in English')->get();

// Returns all news items that has name in English or Dutch with value `Name in English` 
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in English')->get();

// The last argument is the "operand" which you can tweak to achieve something like this:

// Returns all news items that has name in English with value like `Name in...` 
NewsItem::query()->whereJsonContainsLocale('name', 'en', 'Name in%', 'like')->get();

// Returns all news items that has name in English or Dutch with value like `Name in...` 
NewsItem::query()->whereJsonContainsLocales('name', ['en', 'nl'], 'Name in%', 'like')->get();


Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

All documentation is available on our documentation site.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

We got the idea to store translations as json in a column from Mohamed Said. Parts of the readme of his multilingual package were used in this readme.

License

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