Download the PHP package jeromejhipolito/laravel-translation-middleware without Composer
On this page you can find all versions of the php package jeromejhipolito/laravel-translation-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jeromejhipolito/laravel-translation-middleware
More information about jeromejhipolito/laravel-translation-middleware
Files in jeromejhipolito/laravel-translation-middleware
Package laravel-translation-middleware
Short Description Automatic locale detection middleware and translation traits for Laravel APIs. Detects Accept-Language header and provides HasTranslations trait for models.
License MIT
Informations about the package laravel-translation-middleware
Laravel Translation Middleware
Automatic locale detection middleware and translation traits for Laravel APIs. Detects Accept-Language header and provides HasTranslations trait for models with JSON-stored translations.
Features
- Automatically detects user's preferred language from
Accept-Languageheader - Sets Laravel's app locale based on the detected language
- Provides
HasTranslationstrait for models with JSON translation columns - Provides
TranslatableResourcetrait for API resources - Supports locale fallbacks
- Configurable supported locales
- Excludes specific routes from locale detection
Requirements
- PHP 8.2+
- Laravel 11.0+ or 12.0+
Installation
The package will auto-register its service provider.
Configuration
Publish the configuration file:
This will create config/translation.php:
Usage
1. Register the Middleware
Add the middleware to your routes in bootstrap/app.php:
2. Add HasTranslations Trait to Models
3. Database Migration
4. Store Translations
5. Retrieve Translations
6. Use TranslatableResource in API Resources
Client-Side Usage
Send the preferred language in the Accept-Language header:
API Reference
HasTranslations Trait Methods
| Method | Description |
|---|---|
getTranslation($attribute, $locale = null) |
Get translated value |
setTranslation($attribute, $locale, $value) |
Set translation for locale |
setTranslations($attribute, $translations) |
Set multiple translations |
getTranslations($attribute) |
Get all translations array |
hasTranslation($attribute, $locale) |
Check if translation exists |
removeTranslation($attribute, $locale) |
Remove translation |
getAvailableLocales($attribute) |
Get available locale codes |
getAllAvailableLocales() |
Get all locales across attributes |
getTranslatableAttributes() |
Get list of translatable attributes |
translated($locale = null) |
Get all translated attributes |
toArrayTranslated($locale = null) |
Convert model to array with translations |
TranslatableResource Trait Methods
| Method | Description |
|---|---|
getLocale() |
Get current locale |
translated($attribute) |
Get translated value |
translatedWhen($condition, $attribute) |
Conditional translation |
availableTranslations($attribute) |
Get available locales |
allAvailableTranslations() |
Get all available locales |
withTranslationMetadata() |
Get metadata array |
mergeTranslationMetadataWhen($condition) |
Conditional metadata |
Testing
License
The MIT License (MIT). Please see License File for more information.
Credits
All versions of laravel-translation-middleware with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/http Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0