承接 bambolee-digital/translatable-resource-kit 相关项目开发

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

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

bambolee-digital/translatable-resource-kit

Composer 安装命令:

composer require bambolee-digital/translatable-resource-kit

包简介

Enhanced translation capabilities for Laravel models with dynamic JSON handling, optimized for app development

README 文档

README

Latest Version on Packagist Total Downloads License

The Bambolee Translatable Resource Kit is a powerful extension for Laravel applications using Spatie's Laravel Translatable package. It simplifies the handling of translated attributes in API responses and dynamic JSON structures, making it easier to develop multilingual applications.

Features

  • Seamless integration with Spatie's Laravel Translatable
  • Dynamic handling of translated attributes in JSON responses
  • Support for nested relations and deep translation
  • Customizable recursion depth for nested translations
  • Easy-to-use Resource and Collection classes for API responses
  • Compatible with Laravel 8.x, 9.x, 10.x, and 11.x

Installation

You can install the package via composer:

composer require bambolee-digital/translatable-resource-kit

Configuration

After installation, publish the configuration file:

php artisan vendor:publish --provider="BamboleeDigital\TranslatableResourceKit\TranslatableResourceKitServiceProvider" --tag="config"

This will create a config/translatable-resource-kit.php file where you can customize the behavior of the package.

You can customize the middleware behavior in this configuration file:

return [
    // disable debug 
    'debug' => false,
    // Disable automatic middleware registration
    'disable_middleware' => false,
    // Specify the middleware group (default is 'api')
    'middleware_group' => 'api',
    // ... other configurations
];

Added set_locale

To apply the SetLocale middleware to your API routes, you can add it to your routes/api.php file. Here's a comprehensive example of how to do this:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\ProductController;
use App\Http\Controllers\API\CategoryController;

Route::middleware(['set_locale', SetLocale::class])->group(function () {
    // Product routes
    Route::prefix('products')->group(function () {
        Route::get('/', [ProductController::class, 'index']);
        Route::get('/{product}', [ProductController::class, 'show']);
        Route::post('/', [ProductController::class, 'store']);
        Route::put('/{product}', [ProductController::class, 'update']);
        Route::delete('/{product}', [ProductController::class, 'destroy']);
    });

    // Category routes
    Route::prefix('categories')->group(function () {
        Route::get('/', [CategoryController::class, 'index']);
        Route::get('/{category}', [CategoryController::class, 'show']);
        Route::post('/', [CategoryController::class, 'store']);
        Route::put('/{category}', [CategoryController::class, 'update']);
        Route::delete('/{category}', [CategoryController::class, 'destroy']);
    });

    // Add more route groups as needed
});
  1. Open your routes/api.php file.
  2. Add the middleware to your API route group like this:

Disabling the Middleware

If you want to disable the automatic registration of the middleware, set the disable_middleware option to true in your configuration file.

Usage

1. Use the TranslatesAttributes trait in your model:

use BamboleeDigital\TranslatableResourceKit\Http\Traits\TranslatesAttributes;
use Spatie\Translatable\HasTranslations;

class Product extends Model
{
    use HasTranslations, TranslatesAttributes;

    public $translatable = ['name', 'description'];

    // ...
}

2. Create a Resource for your model:

use BamboleeDigital\TranslatableResourceKit\Http\Resources\TranslatableResource;

class ProductResource extends TranslatableResource
{
    // You can add custom logic here if needed
}

3. Create a Collection for your model:

use BamboleeDigital\TranslatableResourceKit\Http\Resources\TranslatableCollection;

class ProductCollection extends TranslatableCollection
{
    // You can add custom logic here if needed
}

4. Use in your controller:

public function index()
{
    $products = Product::all();
    return new ProductCollection($products);
}

public function show(Product $product)
{
    return new ProductResource($product);
}

5. Using the middleware:

The middleware is automatically registered by the package in the group specified in the configuration (default is 'api'). You can set the locale by adding a lang query parameter to your API requests, e.g., ?lang=pt for Portuguese.

If you've disabled the automatic middleware registration, you can manually register it using one of these methods:

  1. In your app/Providers/AppServiceProvider.php:
use Illuminate\Support\Facades\Route;
use BamboleeDigital\TranslatableResourceKit\Http\Middleware\SetLocale;

public function boot()
{
    Route::aliasMiddleware('set_locale', SetLocale::class);
    Route::pushMiddlewareToGroup('api', 'set_locale');
}
  1. In your routes/api.php:
use BamboleeDigital\TranslatableResourceKit\Http\Middleware\SetLocale;

Route::middleware([SetLocale::class])->group(function () {
    // Your API routes here
});
  1. For Laravel versions with app/Http/Kernel.php:
protected $middlewareGroups = [
    'api' => [
        // ...
        \BamboleeDigital\TranslatableResourceKit\Middleware\SetLocale::class,
    ],
];

Example

Here's an example of how the JSON response changes when using this package:

Before:

{
  "id": 1,
  "name": {
    "en": "Laptop",
    "es": "Portátil",
    "pt": "Notebook"
  },
  "description": {
    "en": "Powerful laptop for professionals",
    "es": "Portátil potente para profesionales",
    "pt": "Notebook potente para profissionais"
  },
  "price": 999.99
}

After (assuming the current locale is 'pt', set via ?lang=pt):

{
  "id": 1,
  "name": "Notebook",
  "description": "Notebook potente para profissionais",
  "price": 999.99
}

As you can see, the translated fields are automatically resolved to the current locale, simplifying the structure and making it easier to work with in frontend applications.

Advanced Usage

Customizing Recursion Depth

You can customize the maximum recursion depth for nested translations in the config/translatable-resource-kit.php file:

return [
    'max_recursion_depth' => 3, // Default is 5
];

Handling Nested Relations

The TranslatesAttributes trait automatically handles nested relations. Make sure your relations are properly defined in your model's $with property:

class Product extends Model
{
    use HasTranslations, TranslatesAttributes;

    protected $with = ['category', 'tags'];

    // ...
}

Customizing the Query Parameter

If you want to use a different query parameter instead of lang, you can modify the SetLocale middleware. You'll need to publish the middleware to your application:

php artisan vendor:publish --provider="BamboleeDigital\TranslatableResourceKit\TranslatableResourceKitServiceProvider" --tag="middleware"

Then, edit the published middleware file in app/Http/Middleware/SetLocale.php to change the query parameter name.

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email security@bambolee.digital instead of using the issue tracker.

Credits

License

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

bambolee-digital/translatable-resource-kit 适用场景与选型建议

bambolee-digital/translatable-resource-kit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bambolee-digital/translatable-resource-kit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bambolee-digital/translatable-resource-kit 我们能提供哪些服务?
定制开发 / 二次开发

基于 bambolee-digital/translatable-resource-kit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-08