zai/laravel-eloquent-multilingualization

为laravel eloquent模型添加多语言支持

v1.0.2 2017-09-04 21:07 UTC

This package is not auto-updated.

Last update: 2024-09-25 02:37:17 UTC


README

轻松为您的laravel eloquent模型添加多语言支持

安装

步骤 1: 安装包

执行以下命令将包添加到您的composer.json中

composer require zai/laravel-eloquent-multilingualization

对于laravel 5.4,将服务提供者添加到app/config/app.php中

 Zai\Translate\TranslationServiceProvider::class,

对于laravel 5.5,由于包自动发现,无需将服务提供者添加到app/config/app.php中

步骤 2: 迁移

执行以下命令以添加翻译表。翻译任何Eloquent模型只需要一个表

php artisan translations:table

php artisan migrate

使用方法

在您想要翻译的模型中使用可翻译特质

假设模型名为Article。在Article模型中

use Zai\Translate\Translatable;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use Translatable;
}

在模型中定义可翻译属性

以相同的Article模型为例。我们想要翻译文章的标题和正文

protected $translatables = [
    'title',
    'body'
];

添加翻译

使用方法:addTranslation。它接受一个关联数组作为参数。只需将翻译数据放入数组中,包括它使用的语言键locale

// create a new article
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// add translation to the article
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

此方法仅添加模型可翻译属性中存在的键。任何其他键都将被忽略。如果可翻译属性中的键在参数中缺失,它仍然会被插入,但值将成为一个空字符串

如果您通过表单提交翻译数据。在您的ArticleTranslationsController中,简单使用

public function store(Article $article)
{
    $article->addTranslation(request()->all();
}

请记住在您的表单中包含一个locale输入字段,以便它可以在请求中提交。如果将addTranslation方法应用于一个现有的翻译,它将使用参数中的值来更新现有的翻译。

显示翻译

使用特质提供的translation属性,例如,$article->translation->title。它将返回基于当前区域设置(由App::getLocale()返回的值)的正确翻译

对于默认区域设置或如果某个区域设置的翻译缺失,它将返回模型中的值。

// create a new title
$article = Article::create([
    'title' => 'Hello',
    'body' => 'laravel is awesome!'
]);

// for default locale, or no translations existing
$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

// after adding translation
$article->addTranslation([
    'locale' => 'fr',
    'title' => 'Bonjour',
    'body' => 'laravel est génial!'
]);

// set the locale to fr
App::setLocale('fr');

$article->translation->title; // the output is Bonjour
$article->translation->body;  // the out is laravel est génial!

// for a no existing locale
App::setLocale('zh');

$article->translation->title; // the output is Hello
$article->translation->body;  // the out is laravel is awesome!

更新翻译

使用方法:updateTranslation。它接受一个关联数组作为参数。只需将更新的翻译数据放入数组中,包括它使用的语言。

// add translation to the article
$article->updateTranslation([
    'locale' => 'fr',
    'title' => 'updated title',
    'body' => 'updated body content here'
]);

如果您使用表单更新翻译,在您的ArticleTranslationsController中

public function update(Article $article)
{
    $article->updateTranslation(request()->all());
}

如果您更新到一个不存在的翻译,它将插入一个新的翻译。

删除翻译

使用方法 deleteTranslation。它接受一个字符串作为参数,指定您想要删除的翻译的语言。

$article->deleteTranslation('fr');

删除所有翻译

使用方法 deleteTranslations,它不接受任何参数

$article->deleteTranslations();

检查翻译是否存在

使用方法 hasTranslations,它不接受任何参数。此方法返回布尔值,表示记录是否有翻译。

 $article->hasTranslations();

检查特定区域设置的翻译是否存在

使用方法 hasTranslation。它接受一个字符串作为参数,指定您正在检查的区域设置。此方法返回布尔值,表示特定区域设置的翻译是否存在。

$article->hasTranslation();

防止N+1问题

为了防止N+1问题,在模型中预加载翻译

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $translatables = [
        'title',
        'body'
    ];
}

将值追加到JSON中

将属性 translation 添加到模型的appends属性中

class Article extends Model
{
    use Translatable;

    protected $with = ['translations'];

    protected $appends = ['translation'];

    protected $translatables = [
        'title',
        'body'
    ];
}