Download the PHP package lundo/laravel-translations without Composer
On this page you can find all versions of the php package lundo/laravel-translations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lundo/laravel-translations
More information about lundo/laravel-translations
Files in lundo/laravel-translations
Package laravel-translations
Short Description Transparent polymorphic translations for Eloquent models
License MIT
Informations about the package laravel-translations
laravel-translations
Transparent polymorphic translations for Eloquent models. The default locale is stored directly in model columns; all other locales are stored in a shared translations table — no JSON columns, no extra casts.
Requirements
- PHP 8.3+
- Laravel 11 or 12
Installation
Run the migration:
The migration is loaded automatically by the package — no publish step needed. If you prefer to customise it, you can publish it first:
Optionally publish the config:
Setup
Add the HasTranslations trait to your model and define a $translatable array:
That's it. The default locale (nl by default) is read from and written to the model columns as normal. Non-default locales are transparently read from and written to the translations table.
Reading translations
Accessing a translatable attribute always returns the value for the active locale, falling back to the model column if no translation exists:
Get a specific locale explicitly:
Check existence:
Get all locales the model has a value for:
Writing translations
Setting an attribute while a non-default locale is active buffers the value and persists it when the model is saved:
Set a specific locale directly (bypasses the active locale):
Set multiple locales at once:
Removing translations
Validation rules
Two rules are provided for validating locale-map payloads (e.g. { "nl": "Hallo", "en": "Hello" }).
Register the macros once (e.g. in a service provider):
Rule::localized() — accepts a plain string (treated as the default locale) or an array whose keys are all valid locales and whose values are strings or null. Use on update endpoints where partial input is acceptable.
Rule::localeRequired() — like localized but additionally requires every configured locale to be present with a non-empty value. Use on create endpoints.
Localized media (optional — requires spatie/laravel-medialibrary)
HasLocalizedMedia adds a getLocalizedMedia() method that returns media for the active locale, falling back to the fallback locale, then to untagged (legacy) media. Requires a locale custom property on each media item.
Middleware
SetLocaleFromUser reads the authenticated user's preferred locale and sets it for the request. Register it in your route middleware:
The attribute it reads from is configurable:
Performance scope
WithActiveLocaleScope is an opt-in global scope that eager-loads only the active locale's translations instead of all locales. Add it to a model when you have many locales and want to avoid loading all rows:
Configuration
| Key | Default | Description |
|---|---|---|
default_locale |
env('APP_LOCALE', 'nl') |
Locale stored in model columns |
fallback_locale |
env('APP_FALLBACK_LOCALE', 'nl') |
Fallback when translation is missing |
model |
Translation::class |
Eloquent model for the translations table |
user_locale_attribute |
preferred_locale |
User attribute read by the middleware |
License
MIT