PHPackages                             beyaty/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. [Database &amp; ORM](/categories/database)
4. /
5. beyaty/laravel-translatable

ActiveLibrary[Database &amp; ORM](/categories/database)

beyaty/laravel-translatable
===========================

A Laravel package for multilingual models

v5.1(11y ago)8241MITPHPPHP &gt;=5.4.0

Since Feb 11Pushed 11y ago2 watchersCompare

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

READMEChangelogDependencies (3)Versions (21)Used By (0)

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/ed433cb20e8cc7c88910c7bfe1872d75feb434f418bdd59a063862891a5b6349/68747470733a2f2f7472617669732d63692e6f72672f64696d7361762f6c61726176656c2d7472616e736c617461626c652e7376673f6272616e63683d76342e33)](https://travis-ci.org/dimsav/laravel-translatable)[![Code Coverage](https://camo.githubusercontent.com/8d0b41a044454784f45c9942a12ac96263462fe6c92998afc1082664d3f92976/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64696d7361762f6c61726176656c2d7472616e736c617461626c652f6261646765732f636f7665726167652e706e673f733d64613666383832383736313066663431626266616631636434373131396634333333303430653838)](https://scrutinizer-ci.com/g/dimsav/laravel-translatable/)[![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)

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.

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

- [Demo](#what-is-this-package-doing)
- [Installation](#installation-in-4-steps)
- [Configuration](#configuration)
- [Documentation](#documentation)
- [Support](#faq)

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

[](#laravel-compatibility)

LaravelTranslatable5.0.x5.x4.2.x4.4.x4.1.x4.4.x4.0.x4.3.xDemo
----

[](#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
```

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',

```

### 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', 'name'];

}

// 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)

Laravel 4.\*

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

Laravel 5.\*

```
php artisan vendor:publish
```

With this command, initialize the configuration and modify the created file, located under `app/config/packages/dimsav/laravel-translatable/translatable.php`.

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

```
