https://github.com/latevaweb/laravel-translatable
Make Eloquent model attributes translatables using Translations table
https://github.com/latevaweb/laravel-translatable
laravel localization package translatable translations
Last synced: 5 months ago
JSON representation
Make Eloquent model attributes translatables using Translations table
- Host: GitHub
- URL: https://github.com/latevaweb/laravel-translatable
- Owner: latevaweb
- Created: 2019-12-20T10:43:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-05T10:28:30.000Z (almost 2 years ago)
- Last Synced: 2025-07-18T05:26:20.899Z (11 months ago)
- Topics: laravel, localization, package, translatable, translations
- Language: PHP
- Size: 76.2 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Laravel Translatable
Make Eloquent model attributes translatables using Translations table
[](https://scrutinizer-ci.com/g/latevaweb/laravel-translatable/?branch=master)
[](https://scrutinizer-ci.com/g/latevaweb/laravel-translatable/?branch=master)
[](https://scrutinizer-ci.com/g/latevaweb/laravel-translatable/build-status/master)
[](https://github.styleci.io/repos/229246130)
[](https://opensource.org/licenses/MIT)
[](http://laravel.com)
This package contains a trait to make Eloquent attributes translatable. Translations are stored in Translations database table.
Once the trait is installed on the model you can do these things:
```php
$customer = new Customer; // An Eloquent model
$customer
->setTranslation('greeting', 'en', 'Hello')
->setTranslation('greeting', 'es', 'Hola')
->save();
$customer->greeting; // Returns 'Hello' given that the current app locale is 'en'
$customer->getTranslation('greeting', 'es'); // returns 'Hola'
app()->setLocale('es');
$customer->greeting; // Returns 'Hola'
```
## Installation
You can install the package via composer:
``` bash
composer require latevaweb/laravel-translatable
```
If you want to change the default model or the default tables names, you could publish the config file:
``` bash
php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=config --force
```
You must publish the migration file to create polymorphic and main translations tables:
``` bash
php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=migrations --force
```
## Making a model translatable
The required steps to make a model translatable are:
- First, you need to add the `LaTevaWeb\Translatable\Traits\Translatable`-trait.
- Next, you should create a public static property `$translatable` which holds an array with all the names of attributes you wish to make translatable.
- You have to create a field in the migration of your model type `string` and `nullable`.
Here's an example of a prepared model:
``` php
use Illuminate\Database\Eloquent\Model;
use LaTevaWeb\Translatable\Traits\Translatable;
class NewsItem extends Model
{
use Translatable;
protected $fillable = ['greeting'];
public static $translatable = ['greeting'];
}
```