PHPackages                             flobbos/laravel-translatable-db - 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. flobbos/laravel-translatable-db

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

flobbos/laravel-translatable-db
===============================

A Laravel package for multilingual models

1.4.7(6y ago)53.3k↓50%2MITPHPPHP &gt;=7.1.0

Since Apr 7Pushed 4y ago2 watchersCompare

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

READMEChangelog (10)Dependencies (1)Versions (20)Used By (2)

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

[](#laravel-translatable-db)

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

**Adding multilingual support to your models with ease**

The package is based on Based on but with the added twist of loading the translation based on a `language_id` rather than a string based `locale` from the translations tables.

The package simply deals with retrieving translated content. If you want an easy way of storing your content you should check out which deals with saving content in your database in a compatible way.

### Docs

[](#docs)

- [Demo](#demo)
- [Laravel compatibility](#laravel-compatibility)
- [Installation](#installation)
- [Configuration](#configuration)
- [Translation Model](#translation-model)
- [Middleware](#middleware)

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
```

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

[](#laravel-compatibility)

LaravelTranslatable9.x1.4.\*8.x1.4.\*7.x1.4.\*6.x1.4.\*5.81.4.\*5.71.4.\*5.61.4.\*5.51.4.\*5.41.4.\*5.31.4.\*Installation
------------

[](#installation)

### Install package

[](#install-package)

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

```
composer require flobbos/laravel-translatable-db
```

Next, add the service provider to `config/app.php`. This step is not necessary if auto discover didn't work.

```
Flobbos\TranslatableDB\TranslatableDBServiceProvider::class,

```

### Migrations method 1

[](#migrations-method-1)

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

```
Schema::create('languages', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('locale');
    $table->string('name');
}

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('language_id')->index();

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

### Migrations method 2

[](#migrations-method-2)

```
Schema::create('languages', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('locale');
    $table->string('name');
}

Schema::create('countries', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('code');
    $table->string('name'); //the original name lives in this table
    $table->timestamps();
});

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

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

Method 2 assumes that your default translation lives in the same table as the model that is to be translated. In this case you have to set:

```
public $fallbackAttributes = ['name'];
```

The default translation will then be pulled from the original model's table.

### 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/) `Flobbos\TranslatableDB\TranslatableDB`.
2. The convention for the translation model is `CountryTranslation`.

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

    use \Flobbos\TranslatableDB\TranslatableDB;

    public $translatedAttributes = ['name'];
    public $fallbackAttributes = ['name'];
    protected $fillable = ['code'];
    //protected $fillable = ['code','name']; //if method 2 was used

    /**
     * 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: Publish config

[](#step-4-publish-config)

Laravel ^5.3.\*

```
php artisan vendor:publish
```

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

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

[](#configuration)

### DB Usage

[](#db-usage)

If you want to load languages from a table in the datbase you need to set this to true in the config.

```
    'use_db' => true,
```

### Language model

[](#language-model)

The language model is set to `App\Language` by default but it can be anything you want.

```
    'language_model' => 'App\Whatever'
```

### Language array

[](#language-array)

If you prefer to use the config file for storing your language based information you can setup the language array to your needs. We will still be relying on the language\_id as the identifier for each language.

```
    'language_array'        => [
        'de' => ['name' => 'Deutsch', 'language_id' => '1'],
        'en' => ['name' => 'English', 'language_id' => '2'],
        'fr' => ['name' => 'Français', 'language_id' => '3']
    ]
```

### Fallback

[](#fallback)

Sometimes translations can be missing. In this case we can use a fallback translation to prevent missing content on a page.

```
    'use_fallback' => true,
```

If you don't want to use a fallback, simply set this to false and you will get `null` if there's no suitable translation available.

### Default fallback

[](#default-fallback)

The default fallback varies depending on DB or non-DB usage in the package. You can set these options in the config with either:

```
    'fallback_locale' => 'de'
```

or

```
    'fallback_locale_id' => 1,
```

### Native mode

[](#native-mode)

Sometimes content has already been added to a table that later needs to be translated. To prevent the mess of migrating existing content into our translation tables we have the option to use native mode. This will assume the default content lives in the table of the translated model.

```
    'native_mode' => true
```

### Locale key

[](#locale-key)

The key used for finding the corresponding translation in our translations table. We assume language\_id as this is used throughout the package but it can be whatever you set it to be here.

```
    'locale_key' => 'language_id',
```

This will also set the value pushed into the request by the middleware.

### Locale column

[](#locale-column)

We need to identify the current locale by calling `app()->getLocale()` and find the corresponding language in the database. In case your language identifier is called something other than 'locale' in the DB, you can set it here:

```
    'locale_column' => 'locale',
```

### toArray translations

[](#toarray-translations)

It gets tricky when your translated models are output in an array or JSON format. If the translation isn't loaded into the model it will get omitted when either function is called. With this setting you can force the translation into the model. Beware of unnecessary n-queries!

```
    'to_array_always_loads_translations' => true,
```

### middleware\_default

[](#middleware_default)

If you don't want to use the middleware on all routes, then set this to false and add the middleware where needed.

```
    'middleware_default' => false
```

Translation Model
-----------------

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

```
