Localization package for AdministrCMS
Although it is written to work with the aministr package, you can use this one as a standalone with Laravel 5.2.
It is still a work-in-progress.
Installation
Using Composer:
composer require administrcms/localization
Add the service provider:
\Administr\Localization\LocalizationServiceProvider::class,
The Facade:
'Locale' => \Administr\Localization\LocalizationFacade::class,
Register the middleware in your App Kernel and be sure that the session middleware is registered before it:
\Administr\Localization\LocalizationMiddleware::class,
What it does
It basically checks and persist the current language in a session and it assumes that you are using a parameter for the language in your routes. If you are using a diffrent method for changing languages, you can register your own middleware and utilize the Localizator class included in this package.
Also it creates a table for storing the available languages and has a Language model under the namespace Administr\Localization\Language. If you have multilingual data in your db, it also provides a Translatable model, which extends the base Eloquent model and adds functionality for translations.
Usage of translatable models
You will have to create two tables - one that stores the main model data that is not translatable and a second that will hold the translations.
They can look something like this:
Schema::create('people', function (Blueprint $table) {
$table->increments('id');
$table->boolean('is_visible')->default(0);
$table->timestamps();
});
Schema::create('people_translations', function (Blueprint $table) {
$table->increments('id');
$table->integer('language_id')->unsigned();
$table->integer('person_id')->unsigned();
$table->string('name', 100);
$table->string('position', 100);
$table->timestamps();
$table->foreign('language_id')->references('id')->on('administr_languages')->onDelete('cascade');
$table->foreign('person_id')->references('id')->on('people')->onDelete('cascade');
});
And the corresponding models:
namespace App\Models;
use Administr\Localization\Models\Translatable;
class Person extends Translatable
{
protected $table = 'people';
protected $fillable = ['is_visible', 'name', 'position'];
protected $translatable = ['name', 'position'];
}
use Illuminate\Database\Eloquent\Model;
class PersonTranslation extends Model
{
protected $table = 'people_translations';
protected $fillable = ['language_id', 'name', 'position'];
}
And then in your app code:
$person = Person::translated()->first();
$person = Person::translated(1)->first();
$person = Person::translated('bg')->first();
$person->name;
Person::create([
'name' => 'Miroslav Vitanov',
'position' => 'Developer',
]);
Person::find(1)->update([
'name' => 'Miroslav Vitanov',
'position' => 'PHP Developer',
]);
Person::find(1)->translate('bg')
->fill([
'name' => 'Мирослав Витанов',
'position' => 'Програмист',
])
->save();
$person->hasTranslations();
$person->isTranslatedIn(1);
$person->translations;
$person->toArray();