2017 © Pedro Peláez
 

library langust

A Laravel package

image

goodvin/langust

A Laravel package

  • Monday, March 23, 2015
  • by esomkin
  • Repository
  • 1 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Langust

This is a Laravel package for translatable models., (*1)

Demo

Creating new translations, (*2)

App\Models\Article::create([

    'url' => 'your-url',
    'en' => [

        'name'  => 'Article english name',
        'title' => 'Article english title',
    ],
    'fr' => [

        'name'  => 'Article french name',
        'title' => 'Article french title',
    ],
]);

Getting translated attributes, (*3)

$article = App\Models\Article::where('url', '=', 'your-url')->first();

App::setLocale('en');
echo $article->name;

echo $article->translate('en')->name;
echo $article->en->name;

Setting translated attributes, (*4)

$article->url           = 'your-url-change';

$article->name          = 'Article english name change';
$article->en->name      = 'Article english name change';
$article->translate('fr')->title= 'Article french title change';

$article->save();

or, (*5)

$article->save([

    'url'   => 'your-url-change',
    'es'    => [

        'name'  => 'Article spain name',
        'title' => 'Article spain title',
    ],
]);

or, (*6)

$article->fill([

    'en' => [

        'name'  => 'Article english name',
        'title' => 'Article english title',
    ],
    'es' => [

        'name'  => 'Article spain name',
        'title' => 'Article spain title',
    ],  

])->save();

Installation in 4 steps

Step 1: Install package

Add the package in your composer.json by executing the command., (*7)

composer require goodvin/langust:dev-master

Next, add the service provider to config/app.php, (*8)

'Goodvin\Langust\LangustServiceProvider',

Step 2: Migrations

For example, we need to translate Article model. It is require an extra ArticleLang model., (*9)

Schema::create('articles', function(Blueprint $table){

    $table->increments('id');
    $table->string('url', 200);
    $table->timestamps();
});
Schema::create('article_langs', function(Blueprint $table){

    $table->increments('id');
    $table->string('name', 200);
    $table->string('title', 200);
    $table->integer('article_id')->unsigned();
    $table->enum('lang', [

        'en',
        'fr',
        'es',
    ])->index();

    $table->unique([

        'article_id',
        'lang'
    ]);

    $table->foreign('article_id')
        ->references('id')
        ->on('articles')
        ->onDelete('cascade');
});

Step 3: Models

  1. The translatable model Article should use the trait Goodvin\Langust\Langust.
  2. The convention for the translation model is ArticleLang.
// /app/Models/Article.php
<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use \Goodvin\Langust\Langust;

    protected $fillable = [

        'url',
    ];

    protected $langust = [

        'name',
        'title',
    ];

}

// /app/Models/ArticleLang.php
<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArticleLang extends Model
{
    public $timestamps = false;
}

It is no need to set fillable fields in translatable model :), (*10)

Step 4: Configuration

Laravel 5.*, (*11)

php artisan vendor:publish 

The Versions

23/03 2015

dev-master

9999999-dev

A Laravel package

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Evgeniy Somkin

laravel

23/03 2015

v0.1-alpha

0.1.0.0-alpha

A Laravel package

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Evgeniy Somkin

laravel