Laravel 9 – 13
PHP 8.0+
The project is in a healthy, maintained state
A trait to make an Eloquent model hold translations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

^2.11.2|^3.9
mockery/mockery
^1.4
^6.0|^7.0|^8.0
^7.0|^8.0|^9.0|^10.0|^11.0
^1.20|^v2.0|^v3.0|^4.0

Runtime

^9.0|^10.0|^11.0|^12.0|^13.0
^9.0|^10.0|^11.0|^12.0|^13.0
 Project Readme

A trait to make Eloquent models translatable

Latest Version on Packagist MIT Licensed run-tests PHPStan Rector

This project is based on spatie/laravel-translatable, it resolves inconsistencies that have arisen over time in the initial project.

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 TypiCMS\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

Testing

composer test

Security

If you've found a bug regarding security, please mail sdebacker@gmail.com instead of using the issue tracker.

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.