承接 jetcod/laravel-model-translation 相关项目开发

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

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

jetcod/laravel-model-translation

最新稳定版本:v1.1.0

Composer 安装命令:

composer require jetcod/laravel-model-translation

包简介

Laravel Model Translation simplifies managing model attribute translations in your Laravel applications.

README 文档

README

Actions Status

Latest Stable Version Total Downloads License

Laravel Translation is a package that provides a simple and efficient way to manage translations in your Laravel applications. It allows you to store translations of all your models attributes in database, making it easy to manage and update translations without modifying language files.

Installation

You can install the package via Composer:

composer require jetcod/laravel-model-translation

Configuration

After installing the package, you need to publish the configuration file and migration:

php artisan vendor:publish --tag=translation-config
php artisan vendor:publish --tag=translation-migrations

Then, run the migration to create the translations table:

php artisan migrate

In order to avoid conflictig with other packages and database tables, you can customize your database table name in the config/translation.php config file:

return [
    'database' => [
        'prefix' => env('TRANSLATION_TABLE_PREFIX', 'lt_'),
        'table_name' => env('TRANSLATION_TABLE_NAME', 'translations'),
    ],
];

Usage

Setup model

First, you need to import the Jetcode\Laravel\Translation\Traits\HasTranslations trait in your model and specify the fields you want to translate:

use Jetcode\Laravel\Translation\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    protected const TRANSLATABLE_ATTRIBUTES = ['title', 'content'];
}

Alternatively, you can specify the fields in a method in your model class:

use Jetcode\Laravel\Translation\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    protected function getTranslatableAttributes()
    {
        return ['title', 'content'];
    }
}

NOTE: The TRANSLATABLE_ATTRIBUTES constant or the getTranslatableAttributes method return value can be either a string or an array of the model attribute names.

Defining translatable attributes is optional, but it is recommended to define them.

Create a translation

Now, you can create translations for the model attributes through the defined relations:

// Create a new post with translations
$post = Post::create([
    'title' => 'Hello World',
    'content' => 'This is a post',
]);

// Create a new translation for the post
$post->translation()->saveMany([
    new Translation([
        'locale' => 'fr_FR',
        'key'    => 'title',
        'value'  => 'Bonjour le monde',
    ]),
    new Translation([
        'locale' => 'fr_FR',
        'key'    => 'content',
        'value'  => 'Ceci est un article',
    ]),
]);

Retrieve translated model

This package is compatible with Laravel localization system, so the models are translated according to the current locale. All you need to do is to set the appl locale and your model will be translated automatically:

$post = Post::find(123);
var_dump($post->title);     // "Hello World"

app()->setLocale('fr_FR');
var_dump($post->title);     // "Bonjour le monde"

Alternatively, you can use the withLocale method to retrieve the translated model for a specific locale:

var_dump($post->withLocale('fr_FR')->title); // "Bonjour le monde"

// Current locale is reset to default afterwards
var_dump($post->title); // "Hello World"

Testing

The package includes tests that you can run using PHPUnit:

composer test

You can also run static analysis with PHPStan:

composer phpstan

License

This package is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固