riseno / localizable
The localization plugin for laravel framework
Installs: 1 387
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
- illuminate/console: ^5.2
- illuminate/filesystem: ^5.2
- illuminate/support: ^5.2
Requires (Dev)
- illuminate/database: ^5.2
- phpunit/phpunit: ^5.1
This package is not auto-updated.
Last update: 2020-08-14 01:45:54 UTC
README
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