riseno/localizable

This package is abandoned and no longer maintained. No replacement package was suggested.

The localization plugin for laravel framework

v1.1.1 2016-02-12 17:41 UTC

This package is not auto-updated.

Last update: 2020-08-14 01:45:54 UTC


README

Latest Stable Version Total Downloads License

A small plugin for managing multi-language in database table.

Installation

Require this package with composer using the following command:

composer require riseno/localizable

After updating composer, add the service provider to the providers array in config/app.php

Riseno\Localizable\LocalizableServiceProvider::class,

Run artisan command to generate the database migration file

php artisan riseno:localizable:generate user

After generated migration file, open the migration file. Add any column that should be localized.

Schema::create('user_localizations', function(Blueprint $table)
{
	$table->increments('id');
	$table->unsignedInteger('user_id');
	$table->string('locale');
	$table->boolean('default')->default(false);
	$table->string('name')->nullable();
	$table->timestamps();
	$table->foreign('user_id')->references('id')->on('users');
});

Once all the columns is added to migration file, run migrate command

php artisan migrate

Go to the model that you want to implement localizable function, and add this trait to the class

use Riseno\Localizable\LocalizableTrait;

class User extends Authenticatable
{
    use LocalizableTrait;

And also two required properties

protected $localizeModel  = UserLocalizations::class;
protected $localizeFields = ['name'];

One more thing, add a localizations method

public function localizations()
{
    return $this->hasMany(UserLocalizations::class, 'user_id', 'id');
}

Usage

The trait has provided a generic method for accessing the localized data,

$user->localize('en_US');
// or access value directly
$user->localize('en_US')->name;

If you want to save / update the localized record

$user->saveLocalize('en_US', ['name' => 'Riseno']);

License

The Laravel Localizable is open-sourced software licensed under the MIT license