2017 © Pedro Peláez
 

library eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

image

folklore/eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

  • Wednesday, July 1, 2015
  • by dmongeau
  • Repository
  • 3 Watchers
  • 6 Stars
  • 1,253 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 2 % Grown

The README.md

Eloquent Localizable

Simple Laravel 4 and 5 package to add localization capabilities to Eloquent Models, (*1)

Installation

Dependencies:

Installation:

1- Require the package with Composer, (*2)

$ composer require folklore/eloquent-localizable

Usage

Configuration

This package use a Trait to add localization capabilities to your Eloquent modal. Basically it will add a locales relation to your model and provide a sync method for easy saving., (*3)

For example, if you have a Page model and you would like to add localized title, description. You first add the trait to your Page class., (*4)


use Folklore\EloquentLocalizable\LocalizableTrait; class Page extends Eloquent { use LocalizableTrait; }

By default the trait will look for a [MODEL_NAME]Locale model. So in this example, it will looks for a PageLocale. Just create a PageLocale class next to your Page model., (*5)


use Folklore\EloquentLocalizable\LocaleModel; class PageLocale extends LocaleModel { protected $table = 'pages_locales'; }

You can change the locale model class by overiding the getLocaleModelClass method., (*6)


use Folklore\EloquentLocalizable\LocalizableTrait; class Page extends Eloquent { use LocalizableTrait; protected function getLocaleModelClass() { return 'App\Models\CustomLocaleModel'; } }

You also need to create the table for you localization. Following the example, we will create a migration for a pages_locales table., (*7)

Schema::create('pages_locales', function(Blueprint $table)
{
    $table->increments('id');
    $table->integer('page_id')->unsigned();
    $table->string('locale',2);
    $table->string('title');
    $table->string('description');
    $table->timestamps();

    $table->index('page_id');
    $table->index('locale');
});

Getting locales

You can now use the locales relation on your Page model., (*8)

Getting a page with all locales, (*9)

$page = Page::with('locales')->first();

//Getting the title for a specific locale
echo $page->locales->fr->title;

//Looping through all locales
foreach($page->locales as $locale)
{
    echo $locale->locale.': '.$locale->title;
}

Getting a page with a specific locale, (*10)

$page = Page::withLocale('fr')->first();

//Getting a the title
echo $page->locale->fr->title;

If you want to always include the locales when fetching a page., (*11)


use Folklore\EloquentLocalizable\LocalizableTrait; class Page extends Eloquent { use LocalizableTrait; protected $with = ['locales']; }

Saving locales

You can use the syncLocales method to save your locales., (*12)

$locales = array(
    'en' => array(
        'title' => 'A page',
        'description' => 'This is the description of this page'
    ),
    'fr' => array(
        'title' => 'Une page',
        'description' => 'Ceci est la description de cette page'
    )
);

//or

$locales = array(
    array(
        'locale' => 'en',
        'title' => 'A page',
        'description' => 'This is the description of this page'
    ),
    array(
        'locale' => 'fr',
        'title' => 'Une page',
        'description' => 'Ceci est la description de cette page'
    )
);

$page = new Page();
$page->save(); //We need an id for this page, so we save before.

$page->syncLocales($locales);

The Versions

01/07 2015

dev-master

9999999-dev http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

20/06 2015

v0.2.2

0.2.2.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

20/06 2015

v0.2.1

0.2.1.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

09/04 2015

v0.2.0

0.2.0.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 and 5 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

21/11 2014

v0.1.2

0.1.2.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

21/11 2014

v0.1.1

0.1.1.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization international

02/07 2014

v0.1.0

0.1.0.0 http://github.com/Folkloreatelier/eloquent-localizable

Laravel 4 package to add localization capabilities to Eloquent Models

  Sources   Download

MIT

The Requires

 

laravel eloquent translation locale localization