Skip to content
This repository has been archived by the owner on May 17, 2020. It is now read-only.

risenohk/localizable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Localizable

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

About

Laravel eloquent multi-language plugin

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages