Package Data | |
---|---|
Maintainer Username: | shanginn |
Maintainer Contact: | shanginn@gmail.com (Shangin Nikolai) |
Package Create Date: | 2017-02-20 |
Package Last Update: | 2019-10-14 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 15:01:36 |
Package Statistics | |
---|---|
Total Downloads: | 2,088 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Warning: this package is actively developed, be careful
composer require shanginn/yalt
Add the service provider to config/app.php
'providers' => [
//...
Shanginn\Yalt\YaltServiceProvider::class,
]
By default suffix for translatable table is set to ll
, so for example
translations table for things
table should be named things_lls
.
You can change this suffix in the config file (translation_suffix).
php artisan make:migration create_things_localizations_table
Let's assume you need translatable title and description for the Thing
.
Open up your migration and edit up
method like this:
// database/migrations/2017_02_20_200652_create_things_localizations_table.php
public function up()
{
Schema::create('things_lls', function (Blueprint $table) {
$table->increments('id');
$table->char('locale', 2)->index();
$table->integer('thing_id')->index();
$table->string('title');
$table->string('description');
$table->unique(['thing_id', 'locale']);
$table->foreign('thing_id', 'thing_idx')
->references('id')->on('things')
->onDelete('cascade')
->onUpdate('cascade');
});
}
All you need to do to make Thing
model translatable is to import
Translatable trait, use it and define translatable fields.
// App/Thing.php
//...
use Shanginn\Yalt\Eloquent\Concerns\Translatable;
class Thing extends Model
{
use Translatable;
protected $translatable = ['title', 'description'];
//...
That's it! No models, no relations. Pure magic.
If you want to change current app locale based on 'Accept-Language' header,
register locale
middleware in the Http/Kernel.php
.
// Http/Kernel.php
//...
protected $routeMiddleware = [
//...
'locale' => \Shanginn\Yalt\Http\Middleware\Localization::class,
//...
]
And apply it to some routes.
Thing::create([
'title' => [ // Explicit locale definition for title
'en' => 'Title in english',
'ru' => 'Русский заголовок'
],
// Use default locale for description
'description' => 'Description in the default locale'
])
Sorry. That's it for now. More info coming... Take a look into sources.
id
PK with [item_id
, locale
] composite PK in _lls tableBased on dimsav/laravel-translatable. But I was too lazy to create the same model for every translatable thing so I rewrote this package almost completely.
Feel free to contribute in any way!