PHPackages                             luuminhfpt/laravel-translatable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. luuminhfpt/laravel-translatable

ActiveLibrary[Localization &amp; i18n](/categories/localization)

luuminhfpt/laravel-translatable
===============================

A Laravel package for multilingual models

010PHP

Since Mar 15Pushed 3y agoCompare

[ Source](https://github.com/luuminhfpt/laravel-translatable)[ Packagist](https://packagist.org/packages/luuminhfpt/laravel-translatable)[ RSS](/packages/luuminhfpt-laravel-translatable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

**This package [has been deprecated](https://twitter.com/dimsav/status/1140750099875860481). But worry not. You can use [Astrotomic/laravel-translatable](https://github.com/Astrotomic/laravel-translatable)**.

---

Laravel-Translatable
====================

[](#laravel-translatable)

[![Total Downloads](https://camo.githubusercontent.com/fdc25f12d55f0014c133ca348d336e9fae3f98e35e7be765f9283b23a85eb9a3/68747470733a2f2f706f7365722e707567782e6f72672f64696d7361762f6c61726176656c2d7472616e736c617461626c652f646f776e6c6f6164732e737667)](https://packagist.org/packages/dimsav/laravel-translatable)[![Build Status](https://camo.githubusercontent.com/6c443a4093cae285d5ceb03333723790114e99c37260fdf2d8d26808e8f7c4b8/68747470733a2f2f636972636c6563692e636f6d2f67682f64696d7361762f6c61726176656c2d7472616e736c617461626c652e706e673f7374796c653d736869656c64)](https://circleci.com/gh/dimsav/laravel-translatable)[![Code Coverage](https://camo.githubusercontent.com/e2d922e709ce3a84ddecc3523fff9e25c9ecd8604f920176ebfb42b10890104b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64696d7361762f6c61726176656c2d7472616e736c617461626c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dimsav/laravel-translatable/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/130b4f47de9e012c97c34977fd9dbef19563670418d2200045261cca40d766cb/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64696d7361762f6c61726176656c2d7472616e736c617461626c652e737667)](https://packagist.org/packages/dimsav/laravel-translatable)[![License](https://camo.githubusercontent.com/f143559301dc501917397fb93900db9e9a5416d7ceaab9fcb6a39f5bad035732/68747470733a2f2f706f7365722e707567782e6f72672f64696d7361762f6c61726176656c2d7472616e736c617461626c652f6c6963656e73652e737667)](https://packagist.org/packages/dimsav/laravel-translatable)[![SensioLabsInsight](https://camo.githubusercontent.com/9d641f7cfbb68cfdce596c737af06b30228be8f9cd836ce2df3287f989d5cec3/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f63313035333538612d333231312d343765382d623636322d3934616139386431656565652f6d696e692e706e67)](https://insight.sensiolabs.com/projects/c105358a-3211-47e8-b662-94aa98d1eeee)[![StyleCI](https://camo.githubusercontent.com/fb868b75eff9c9b5597145b04c168ab4f0e09db356c9b919b72d2847c04d151a/68747470733a2f2f7374796c6563692e696f2f7265706f732f31363438303537362f736869656c64)](https://styleci.io/repos/16480576)

[![Laravel Translatable](img/laravel-translatable.png)](img/laravel-translatable.png)

**If you want to store translations of your models into the database, this package is for you.**

This is a Laravel package for translatable models. Its goal is to remove the complexity in retrieving and storing multilingual model instances. With this package you write less code, as the translations are being fetched/saved when you fetch/save your instance.

### Docs

[](#docs)

- [Demo](#demo)
- [Laravel compatibility](#laravel-compatibility)
- [Tutorials](#tutorials)
- [Installation](#installation-in-4-steps)
- [Configuration](#configuration)
- [Features list](#features-list)
- [FAQ / Support](#faq)
- [Donations](#donations)

Demo
----

[](#demo)

**Getting translated attributes**

```
  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // Greece

  App::setLocale('en');
  echo $greece->name;     // Greece

  App::setLocale('de');
  echo $greece->name;     // Griechenland
```

**Saving translated attributes**

```
  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // Greece

  $greece->translate('en')->name = 'abc';
  $greece->save();

  $greece = Country::where('code', 'gr')->first();
  echo $greece->translate('en')->name; // abc
```

**Filling multiple translations**

```
  $data = [
    'code' => 'gr',
    'en'  => ['name' => 'Greece'],
    'fr'  => ['name' => 'Grèce'],
  ];

  $greece = Country::create($data);

  echo $greece->translate('fr')->name; // Grèce
```

Laravel compatibility
---------------------

[](#laravel-compatibility)

LaravelTranslatable5.89.\*5.79.\*5.69.\*5.58.\*5.47.\*5.36.\*5.25.5 - 6.\*5.15.0 - 6.\*5.05.0 - 5.44.2.x4.4.x4.1.x4.4.x4.0.x4.3.xTutorials
---------

[](#tutorials)

- Check the tutorial about laravel-translatable in laravel-news: [*How To Add Multilingual Support to Eloquent*](https://laravel-news.com/2015/09/how-to-add-multilingual-support-to-eloquent/)
- [How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application](https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application)

Installation in 4 steps
-----------------------

[](#installation-in-4-steps)

### Step 1: Install package

[](#step-1-install-package)

Add the package in your composer.json by executing the command.

```
composer require dimsav/laravel-translatable
```

Next, add the service provider to `app/config/app.php`

```
Dimsav\Translatable\TranslatableServiceProvider::class,

```

### Step 2: Migrations

[](#step-2-migrations)

In this example, we want to translate the model `Country`. We will need an extra table `country_translations`:

```
Schema::create('countries', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('code');
    $table->timestamps();
});

Schema::create('country_translations', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('country_id')->unsigned();
    $table->string('name');
    $table->string('locale')->index();

    $table->unique(['country_id','locale']);
    $table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
});
```

### Step 3: Models

[](#step-3-models)

1. The translatable model `Country` should [use the trait](http://www.sitepoint.com/using-traits-in-php-5-4/) `Dimsav\Translatable\Translatable`.
2. The convention for the translation model is `CountryTranslation`.

```
// models/Country.php
class Country extends Eloquent {

    use \Dimsav\Translatable\Translatable;

    public $translatedAttributes = ['name'];
    protected $fillable = ['code'];

    /**
     * The relations to eager load on every query.
     *
     * @var array
     */
    // (optionaly)
    // protected $with = ['translations'];

}

// models/CountryTranslation.php
class CountryTranslation extends Eloquent {

    public $timestamps = false;
    protected $fillable = ['name'];

}
```

The array `$translatedAttributes` contains the names of the fields being translated in the "Translation" model.

### Step 4: Configuration

[](#step-4-configuration)

We copy the configuration file to our project.

Laravel 5.\*

```
php artisan vendor:publish --tag=translatable
```

Laravel 4.\*

```
php artisan config:publish dimsav/laravel-translatable
```

*Note: There isn't any restriction for the format of the locales. Feel free to use whatever suits you better, like "eng" instead of "en", or "el" instead of "gr". The important is to define your locales and stick to them.*

Configuration
-------------

[](#configuration)

### The config file

[](#the-config-file)

You can see the options for further customization in the [config file](src/config/translatable.php).

### The translation model

[](#the-translation-model)

The convention used to define the class of the translation model is to append the keyword `Translation`.

So if your model is `\MyApp\Models\Country`, the default translation would be `\MyApp\Models\CountryTranslation`.

To use a custom class as translation model, define the translation class (including the namespace) as parameter. For example:

```
