tingo-gmbh/eloquent-translatable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tingo-gmbh/eloquent-translatable

最新稳定版本:v1.1.0

Composer 安装命令:

composer require tingo-gmbh/eloquent-translatable

包简介

Store translation items in your local database using Eloquent

README 文档

README

Store translation items in your local database using Eloquent.

Install

First install the latest version of our package.

composer require tingo-gmbh/eloquent-translatable

Next we publish the migration and config files.

php artisan vendor:publish --provider="Tingo\Translatable\TranslatableServiceProvider" --tag="migrations"

Usage

Model

Add the Translatable trait to your Eloquent model and specify all translatable attributes.

<?php

namespace Tingo\Translatable\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Tingo\Translatable\Translatable;

class Entity extends Model
{
    use Translatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'category',
        'description',
        'unit',
        'price',
    ];

    /**
     * The attributes that are translatable.
     *
     * @var array<string>
     */
    protected array $translatable = [
        'name',
        'category',
        'description',
    ];
}

Create

Create a new translation:

$entity = Entity::create([
    'name' => 'Foo Entity',
    'category' => 'foo',
    'description' => 'This is my awesome entity!',
]);
$entity->createTranslation('name', 'Foo Entität', 'de');
$entity->createTranslation('name', 'L\'entité foo', 'fr');

Update

A locale must always be passed as an argument, otherwise nothing will be updated.

$entity->updateTranslation('name', 'Aktualisierte Foo Entität', 'de');

Get

Finally, can return your translations in your Resources or anywhere else in your code.

echo $entity->getTranslation('name', 'de');
// Foo Entität
echo $entity->getTranslation('name', 'fr');
// L\'entité foo

Get all

If you do not provide a language, the package will use the default locale of your Laravel app. This is especially useful when passing locales as a request header to your API end points.

App::setLocale('it');
$entity->createTranslation('name', 'Entità di Foo');
echo $entity->getTranslation('name');
// Entità di Foo

If you want to get all translations for a specific model, use the getTranslations() method. This will return translations of all attributes defined in the $translatable array.

var_dump($entity->gatTranslations())
//  [
//      'name' =>  [
//          'default' => 'Foo Entity',
//          'de' => 'Foo Entität',
//          'fr' => 'L\'entité foo',
//      ], 
//      ...
//  ]

Delete

A locale must always be passed as an argument, otherwise nothing will be deleted.

$entity->deleteTranslation('name', 'de');

Spatie

There is very similar package made by Spatie. https://github.com/spatie/laravel-translatable

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固