承接 flowcontrol/filters 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

flowcontrol/filters

Composer 安装命令:

composer require flowcontrol/filters

包简介

Filter module

README 文档

README

It is still a work-in-progress.

Installation

Using Composer:

composer require flowcontrol/localization

Add the service provider:

\FlowControl\Localization\LocalizationServiceProvider::class,

The Facade:

'Locale'    => \FlowControl\Localization\LocalizationFacade::class,

Register the middleware in your App Kernel and be sure that the session middleware is registered before it:

\FlowControl\Localization\LocalizationMiddleware::class,

What it does

It basically checks and persist the current language in a session and it assumes that you are using a parameter for the language in your routes. If you are using a diffrent method for changing languages, you can register your own middleware and utilize the Localizator class included in this package.

Also it creates a table for storing the available languages and has a Language model under the namespace FlowControl\Localization\Language. If you have multilingual data in your db, it also provides a Translatable model, which extends the base Eloquent model and adds functionality for translations.

Usage of translatable models

You will have to create two tables - one that stores the main model data that is not translatable and a second that will hold the translations.

They can look something like this:

Schema::create('people', function (Blueprint $table) {
    $table->increments('id');
    $table->boolean('is_visible')->default(0);
    $table->timestamps();
});

Schema::create('people_translations', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('language_id')->unsigned();
    $table->integer('person_id')->unsigned();

    $table->string('name', 100);
    $table->string('position', 100);

    $table->timestamps();

    $table->foreign('language_id')->references('id')->on('flowcontrol_languages')->onDelete('cascade');
    $table->foreign('person_id')->references('id')->on('people')->onDelete('cascade');
});

And the corresponding models:

// Person.php

namespace App\Models;

use FlowControl\Localization\Models\Translatable;

class Person extends Translatable
{
    protected $table = 'people';
    protected $fillable = ['is_visible', 'name', 'position'];
    protected $translatable = ['name', 'position'];
}

// PersonTranslation.php

use Illuminate\Database\Eloquent\Model;

class PersonTranslation extends Model
{
    protected $table = 'people_translations';
    protected $fillable = ['language_id', 'name', 'position'];
}

And then in your app code:

 // Get the first model with translation in the current app locale
$person = Person::translated()->first();

 // Translated in the language with id of 1
$person = Person::translated(1)->first();

// Translated in the language with code of 'bg'.
// Have in mind that when you do it like this,
// an additional query will be made to find the language id.
$person = Person::translated('bg')->first();

$person->name; // You can access the translation model properties through the main model

// Create an instance of the main model
// with a translation in the current locale.
// If you do not pass translatable fields,
// only the main model will be persisted.
Person::create([
    'name'      => 'Miroslav Vitanov',
    'position'  => 'Developer',
]);

// Updates the model and the translation that was
// created in the current locale.
Person::find(1)->update([
    'name'      => 'Miroslav Vitanov',
    'position'  => 'PHP Developer',
]);

// Translate a model in another language.
// You can pass a locale code or language id.
Person::find(1)->translate('bg')
->fill([
   'name'      => 'Мирослав Витанов',
   'position'  => 'Програмист',
])
->save();

// Check if any translations exist
$person->hasTranslations();

// Check if a translation with language id exists
$person->isTranslatedIn(1);

// Get all translations
$person->translations;

// When a model is serialized to an array,
// it will include the current translation
$person->toArray();

统计信息

  • 总下载量: 200
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-12-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固