2017 © Pedro Peláez
 

library yalt

Yet Another Laravel Translator for Eloquent models (Yalt)

image

shanginn/yalt

Yet Another Laravel Translator for Eloquent models (Yalt)

  • Thursday, March 1, 2018
  • by shanginn
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,043 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 5 % Grown

The README.md

Yet Another Laravel Translator

Warning: this package is actively developed, be careful, (*1)

Requires

  • PHP 7.*
  • Laravel 5.* (Only tested with 5.4)

Installation

Package

composer require shanginn/yalt

Add the service provider to config/app.php, (*2)

'providers' => [
    //...
    Shanginn\Yalt\YaltServiceProvider::class,
]

Migrations

By default suffix for translatable table is set to ll, so for example translations table for things table should be named things_lls., (*3)

You can change this suffix in the config file (translation_suffix)., (*4)

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:, (*5)

// 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');
    });
}

Model

All you need to do to make Thing model translatable is to import Translatable trait, use it and define translatable fields., (*6)

// 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., (*7)

Middleware

If you want to change current app locale based on 'Accept-Language' header, register locale middleware in the Http/Kernel.php., (*8)

// Http/Kernel.php
//...
protected $routeMiddleware = [
//...
    'locale' => \Shanginn\Yalt\Http\Middleware\Localization::class,
//...
]

And apply it to some routes., (*9)

Usage

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., (*10)

TODO

  • [x] Write basic info here
  • [ ] Write more info here
  • [ ] Create tests
  • [ ] Test all provided functional
  • [ ] Add artisan command to create migrations for Translatable models
  • [ ] Replace id PK with [item_id, locale] composite PK in _lls table
  • [ ] Add ability to choose between plural and single table suffix (ex. things_lls vs thing_lls)

Additional info

Based 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., (*11)

Feel free to contribute in any way!, (*12)

The Versions

01/03 2018

dev-master

9999999-dev https://github.com/shanginn/Yet-Another-Laravel-Translator

Yet Another Laravel Translator for Eloquent models (Yalt)

  Sources   Download

MIT

The Requires

 

laravel eloquent translation translator database translation yalt