PHPackages                             simexis/multi-language - 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. simexis/multi-language

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

simexis/multi-language
======================

Multi language for laravel 5.x

1.0.7(10y ago)5115MITCSSPHP &gt;=5.5.9

Since Oct 21Pushed 10y ago3 watchersCompare

[ Source](https://github.com/jooorooo/multi-language)[ Packagist](https://packagist.org/packages/simexis/multi-language)[ RSS](/packages/simexis-multi-language/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (16)Used By (0)

Laravel 5.x Multilanguage and localization module
=================================================

[](#laravel-5x-multilanguage-and-localization-module)

Note: This package use by default use language ISO 639-1 two-letter codes for locale: `https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes`

The idea for this package is based on: [Laravel-Translatable](https://github.com/dimsav/laravel-translatable)[Upgrading Laravel's localization module](https://github.com/Waavi/translation)[Laravel 5 Translation Manager](https://github.com/barryvdh/laravel-translation-manager)

- This is a Laravel package for translatable models.
- Read all translation files and save them in the database.
- Url modifier for local prefix. Examples: login/ =&gt; /it/login . / =&gt; /it

Installation
------------

[](#installation)

Require this package in your composer.json and run composer update (or run `composer require simexis/multi-language` directly):

```
simexis/multi-language

```

After updating composer, add the ServiceProvider to the providers array in config/app.php

```
'providers' => [
    Simexis\MultiLanguage\MultiLanguageServiceProvider::class,
]
```

You need to run the publish and migration.

```
$ php artisan vendor:publish
$ php artisan migrate

```

Routes are added in the ServiceProvider. You can set the group parameters for the routes in the configuration. You can change the prefix or filter/middleware for the routes. If you want full customisation, you can extend the ServiceProvider and override the `map()` function.

This example will make the translation manager available at `http://yourdomain.com/multilanguage`

Usage
-----

[](#usage)

### Set current locale.

[](#set-current-locale)

```
App::setLocale('ru');
```

Thene package work with `ru` locale.

Usage translation part
----------------------

[](#usage-translation-part)

### Web interface

[](#web-interface)

When you have imported your translation (via buttons or command), you can view them in the webinterface (on the url you defined the with the controller). You can click on a translation and an edit field will popup. Just click save and it is saved :) When a translation is not yet created in a different locale, you can also just edit it to create it.

Using the buttons on the webinterface, you can append, replace, truncate and clear the translations.

You can also use the commands below.

### Import command

[](#import-command)

The import command will search through resources/lang and load all strings in the database, so you can easily manage them.

```
$ php artisan translator:append

```

Note: By default, only new strings are added. Translations already in the DB are kept the same. If you want to replace all values with the ones from the files, use replace command: `php artisan translator:replace`

### Truncate (delete) all transaltions from Database.

[](#truncate-delete-all-transaltions-from-database)

```
$ php artisan translator:truncate

```

### Clear non existings translations.

[](#clear-non-existings-translations)

```
$ php artisan translator:clear

```

Usage Url modifier for local prefix
-----------------------------------

[](#usage-url-modifier-for-local-prefix)

For transform link from `http://yourdomain.com/page` to `http://yourdomain.com/en/page`, just use route or url functions. Example url('page') display `http://yourdomain.com/en/page` where `en` is App::getLocale()

Usage translatable models
-------------------------

[](#usage-translatable-models)

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' => 'Grece'],
  ];

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

  echo $greece->translate('fr')->name; // Grece
```

### Step 1: Migrations

[](#step-1-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->char(config('multilanguage.locale_key'), 2)->index('idx_locale');

	$table->unique(['country_id',config('multilanguage.locale_key')]);
	$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
	$table->foreign(config('multilanguage.locale_key'))
		->references(config('multilanguage.locale_key'))
		->on('languages')->onDelete('cascade')->onUpdate('cascade');
});
```

### Step 2: Models

[](#step-2-models)

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

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

    use \Simexis\MultiLanguage\Traits\Translatable;

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

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'countries';
}

// models/CountryTranslation.php
use Illuminate\Database\Eloquent\Model;

class CountryTranslation extends Model
{

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

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'country_translations';
}
```

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

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:

```
