latevaweb/laravel-translatable

Laravel package to make Eloquent models attributes translatables
1,152
Install
composer require latevaweb/laravel-translatable
Latest Version:0.1.9
PHP:^7.3|^8.2|^8.3
Older releases:6 · 7 · 8
License:MIT
Last Updated:Aug 5, 2024
Links: GitHub  ·  Packagist
Maintainer: LaTevaWeb

Laravel Translatable

Make Eloquent model attributes translatables using Translations table

Scrutinizer Code Quality Code Coverage Build Status StyleCI License: MIT Laravel 6.x

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:

$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:

composer require latevaweb/laravel-translatable

If you want to change the default model or the default tables names, you could publish the config file:

php artisan vendor:publish --provider="LaTevaWeb\Translatable\TranslatableServiceProvider" --tag=config --force

You must publish the migration file to create polymorphic and main translations tables:

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:

use Illuminate\Database\Eloquent\Model;
use LaTevaWeb\Translatable\Traits\Translatable;

class NewsItem extends Model
{
    use Translatable;
       
    protected $fillable = ['greeting'];
    public static $translatable = ['greeting'];
}

Related Packages

esign/laravel-underscore-translatable

A laravel package to make your eloquent models translatable.

31,491 3
sertxudeveloper/laravel-translatable

Manage localized routes and use translatable models in a Laravel app.

3,605 18
imcity-tech/laravel-translatable

[fork] The package provides possibility to translate your Eloquent models into d...

1,678 0
square-bit/laravel-pt-localization

Portuguese Localization package for Laravel

4,314 11
ericlagarda/locally

Laravel 5 User Preferred Localization

21 1

Version History

Version Released PHP Laravel License
0.1.9 ^7.3|^8.2|^8.3 MIT
0.1.8 ^7.3 ^6.0|^7.0|^8.0 MIT
0.1.7 ^7.3 ^6.0|^7.0 MIT
0.1.6 ^7.3 ^6.0|^7.0 MIT
0.1.5 ^7.3 ^6.0|^7.0 MIT