flowcontrol/localization

1.0.3 2017-02-24 13:43 UTC

This package is auto-updated.

Last update: 2024-09-14 19:36:54 UTC


README

虽然它是为与 flowcontrol/admin 包一起使用而编写的,但你也可以将它作为独立的包与 Laravel 5.2 一起使用。

它仍在开发中。

安装

使用 Composer

composer require flowcontrol/localization

添加服务提供者

\FlowControl\Localization\LocalizationServiceProvider::class,

外观

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

在您的 App Kernel 中注册中间件,并确保在它之前注册会话中间件

\FlowControl\Localization\LocalizationMiddleware::class,

它的功能

它基本上会检查并持久化当前语言到会话中,并假设你在你的路由中使用了语言参数。如果你使用的是不同的语言更改方法,你可以注册自己的中间件并利用本包中包含的 Localizator 类。

此外,它还会创建一个用于存储可用语言的表,并在 FlowControl\Localization\Language 命名空间下有一个 Language 模型。如果你的数据库中有多语言数据,它还提供了一个 Translatable 模型,它扩展了基本 Eloquent 模型并增加了翻译功能。

可翻译模型的使用

你将不得不创建两个表 - 一个用于存储不可翻译的主模型数据,另一个用于存储翻译。

它们可能看起来像这样

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

以及相应的模型

// 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'];
}

然后在你的应用代码中

 // 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();