Download the PHP package bambolee-digital/translatable-resource-kit without Composer
On this page you can find all versions of the php package bambolee-digital/translatable-resource-kit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bambolee-digital/translatable-resource-kit
More information about bambolee-digital/translatable-resource-kit
Files in bambolee-digital/translatable-resource-kit
Package translatable-resource-kit
Short Description Enhanced translation capabilities for Laravel models with dynamic JSON handling, optimized for app development
License MIT
Informations about the package translatable-resource-kit
Bambolee Translatable Resource Kit
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:
Configuration
After installation, publish the configuration file:
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:
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:
- Open your
routes/api.php
file. - 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:
2. Create a Resource for your model:
3. Create a Collection for your model:
4. Use in your controller:
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:
-
In your
app/Providers/AppServiceProvider.php
: -
In your
routes/api.php
: - For Laravel versions with
app/Http/Kernel.php
:
Example
Here's an example of how the JSON response changes when using this package:
Before:
After (assuming the current locale is 'pt', set via ?lang=pt
):
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:
Handling Nested Relations
The TranslatesAttributes
trait automatically handles nested relations. Make sure your relations are properly defined in your model's $with
property:
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:
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 [email protected] instead of using the issue tracker.
Credits
- Bambolee Digital
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of translatable-resource-kit with dependencies
illuminate/support Version ^9.0|^10.0|^11.0
spatie/laravel-translatable Version ^5.0|^6.0