bambolee-digital/translatable-resource-kit

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

1.0.6 2024-09-13 12:01 UTC

This package is auto-updated.

Last update: 2024-12-13 12:39:34 UTC


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.