despark / laravel-db-localization

用于 Laravel 框架的数据库本地化包

v3.1 2018-05-22 07:39 UTC

README

注意:如果您正在寻找 Laravel 4.2 的版本,请查看 v1 分支

安装

打开您的项目中的 composer.json 文件,并在 require 数组中添加以下内容

"despark/laravel-db-localization": "2.0.*"

现在运行 composer update 安装新的需求。

安装完成后,您需要在 config/app.php 文件中的 providers 数组中注册服务提供者

'providers' => array(
  ...
  Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider::class,
);

发布配置文件:php artisan vendor:publish --provider="Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider" --tag="config"

发布迁移:php artisan vendor:publish --provider="Despark\LaravelDbLocalization\LaravelDbLocalizationServiceProvider" --tag="migrations"

如何使用它

数据库示例

  • 首先您需要创建您的语言表
Schema::create('i18n', function (Blueprint $table) {
        $table->increments('id');
        $table->string('locale')->unique()->index();
        $table->string('name')->index();
        $table->timestamps();
});
  • 可翻译表的示例
Schema::create('contacts', function (Blueprint $table) {
        $table->increments('id');

        // untranslatable columns
        $table->string('fax');
        $table->string('phone');
        $table->timestamps();
});
  • 翻译表的示例
Schema::create('contacts_i18n', function (Blueprint $table) {

        $table->integer('contact_id')->unsigned();
        $table->foreign('contact_id')->references('id')->on('contacts')->onDelete('cascade');
        $table->integer('i18n_id')->unsigned();
        $table->foreign('i18n_id')->references('id')->on('i18n')->onDelete('cascade');

        // translatable columns
        $table->string('name', 100);
        $table->string('location', 100);

        $table->unique(['contact_id', 'i18n_id']);
        $table->primary(['contact_id', 'i18n_id']);
        $table->timestamps();
});

模型示例

use Despark\LaravelDbLocalization\i18nModelTrait;

class Contacts extends Eloquent
{
    use i18nModelTrait; // You must use i18nModelTrait

    protected $fillable = [
        'fax',
        'phone',
    ];

    protected $translator = 'Despark\LaravelDbLocalization\ContactsI18n'; // Here you need to add your translations table model name

    protected $translatorField = 'contact_id'; // your translator field name

    protected $localeField = 'i18n_id'; // here is your locale field name

    protected $translatedAttributes = ['contact_id', 'i18n_id', 'name', 'location']; // translatable fillables
}

class ContactsI18n extends Eloquent
{
    protected $table = 'contacts_i18n';
}

视图示例

创建

{!! Form::text("fax", null) !!}
{!! Form::text("phone", null) !!}

@foreach($languages as $language)
    {!! Form::text("name[name_$language->id]", null) !!}  // Follow this convention array( fieldname_languageId );
    {!! Form::text("location[location_$language->id]", null) !!}
@endforeach

检索

    // locale string
    $contacts->translate('en'); // all fields
    $contacts->translate('en')->location; // specific field

    // locale id
    $i18nId = 2;
    $contacts->translate($i18nId); // all fields
    $contacts->translate($i18nId)->location; // specific field

配置示例

config/laravel-db-localization.php
    'locale_class' => 'Despark\LaravelDbLocalization\I18n', // Eloquent model that handles your languages.