定制 labrodev/laravel-translatable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

labrodev/laravel-translatable

最新稳定版本:v1.0.1

Composer 安装命令:

composer require labrodev/laravel-translatable

包简介

Laravel package providing a reusable trait for multilingual search in Eloquent JSON fields.

README 文档

README

A Laravel package that adds JSON-based multilingual support to your Eloquent models. It provides:

  • QueryFieldLocalizer: Utility to localize JSON fields to the current locale.
  • SearchQueryBuilder: Trait for case-insensitive, multi-locale JSON field searching.
  • LocaleResolver: Default resolver that returns app.locale and app.fallback_locale.

Installation

Require the package via Composer:

composer require labrodev/laravel-translatable

Optionally, publish the configuration (if you add a config file later):

php artisan vendor:publish --provider="Labrodev\Translatable\TranslatableServiceProvider"

Note: No configuration file is required out of the box—this is here for future customization.

Usage

1. Localizing JSON Fields

QueryFieldLocalizer helps you build locale-specific JSON paths for query fields:

use Labrodev\Translatable\Utilities\QueryFieldLocalizer;

// Assume the `title` column contains JSON:
// { "en": "Hello", "es": "Hola" }
$localized = QueryFieldLocalizer::translatableField('title');
// On locale `es`, returns: "title->es"

// Use in query:
$posts = Post::whereRaw("{$localized} = ?", ['Hola'])->get();

2. Multilingual Search on JSON Columns

Add the SearchQueryBuilder trait to your Eloquent model:

use Illuminate\Database\Eloquent\Model;
use Labrodev\Translatable\Base\Traits\SearchQueryBuilder;

class Article extends Model
{
    use SearchQueryBuilder;

    protected $casts = [
        'titles' => 'array',
    ];
}

Perform a case-insensitive search across all configured locales:

// Searches "manzana" in `titles` JSON for locales [en, es]
$results = Article::query()
    ->where(function ($q) {
        $this->searchField($q, 'manzana', 'titles');
    })
    ->get();

Or chain with existing conditions:

$posts = Article::query()
    ->where('published', true)
    ->orWhere(function ($q) {
        $this->searchFieldOr($q, 'orange', 'titles');
    })
    ->get();

3. Customizing Locales

By default, locales come from app.locale and app.fallback_locale. To customize, bind your own LocaleResolver implementation:

use Labrodev\Translatable\Contracts\LocaleResolver;

$this->app->singleton(LocaleResolver::class, function ($app) {
    return new class implements LocaleResolver {
        public function all(): array
        {
            return ['en', 'fr', 'es'];
        }
    };
});

Testing

This package uses Pest with Orchestra Testbench for testing and PHPStan for static analysis. This package uses Pest with Orchestra Testbench for testing and PHPStan for static analysis

  1. Install dependencies:

    composer install
  2. Run static analysis:

    composer analyse
  3. Run tests:

    composer test

Tests cover:

  • QueryFieldLocalizer::translatableField() outputs correct JSON path.
  • SearchQueryBuilder builds proper SQL & bindings for multilingual JSON searches.
  • DefaultLocaleResolverTest resolve locales array.

Security

If you discover any security-related issues, please email contact@labrodev.com instead of using the issue tracker.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

Contributing

Feel free to open issues or submit pull requests. Check Coding Standards:

  • PSR-12
  • Strict types enabled

License

MIT © Labro Dev

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固