riseno/localizable

此包已被废弃且不再维护。未建议替代包。

laravel 框架的本地化插件

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

一个用于管理数据库表多语言的插件。

安装

使用以下命令通过 composer 安装此包

composer require riseno/localizable

更新 composer 后,将服务提供者添加到 config/app.php 中的 providers 数组

Riseno\Localizable\LocalizableServiceProvider::class,

运行 artisan 命令以生成数据库迁移文件

php artisan riseno:localizable:generate user

生成迁移文件后,打开迁移文件。添加任何需要本地化的列。

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');
});

将所有列添加到迁移文件后,运行迁移命令

php artisan migrate

前往您想实现本地化功能的模型,并将此特质添加到类中

use Riseno\Localizable\LocalizableTrait;

class User extends Authenticatable
{
    use LocalizableTrait;

还需要两个必需属性

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

另外一件事,添加一个 localizations 方法

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

使用方法

特质提供了一个通用的方法来访问本地化数据,

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

如果您想保存/更新本地化记录

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

许可

Laravel Localizable 是开源软件,根据 MIT 许可证 许可。