larasup / localization
Model-level localization for Laravel via translatable attributes
2.0.0
2026-04-09 15:15 UTC
Requires
- php: ^8.2
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- larasup/reference: ^1.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpunit/phpunit: ^10.0|^11.0
README
Model-level localization for Laravel. Add translatable attributes to any Eloquent model without modifying its table — translations are stored in a separate table.
Installation
composer require larasup/localization
The package uses Laravel auto-discovery. Run migrations:
php artisan migrate
Publish the config (optional):
php artisan vendor:publish --tag=localize-config
Configuration
// config/localize.php
return [
'table_prefix' => 'localize_', // customize table names prefix
'classes' => [
// Map model classes to fixed integer IDs (optional).
// If not mapped, IDs are auto-generated via larasup/reference.
// App\Models\Product::class => 10,
],
];
Usage
1. Add the trait to your model
use Larasup\Localization\Traits\Localizable;
class Product extends Model
{
use Localizable;
const LOCALIZABLE = ['name', 'description'];
}
2. Write translations
Translations are saved for the current app locale:
App::setLocale('en');
$product->name = 'Laptop';
App::setLocale('ru');
$product->name = 'Ноутбук';
3. Read translations
App::setLocale('ru');
echo $product->name; // "Ноутбук"
App::setLocale('en');
echo $product->name; // "Laptop"
4. Query relations
// Localizations for the current locale
$product->localizations;
// All localizations across all languages
$product->all_localizations;
Languages
The package ships with a localize:import command that seeds common languages:
php artisan localize:import
Default languages: English (en), Russian (ru), Kazakh (kk), Ukrainian (uk), German (de), French (fr), Spanish (es), Italian (it), Arabic (ar), Chinese (zh).
English and Russian are enabled by default (status=1), others are disabled (status=0).
How it works
- The
Localizabletrait intercepts__get/__setfor attributes listed in the model'sLOCALIZABLEconstant. - Translations are stored in the
localize_localizationstable, keyed by a class ID, model primary key, language code, and attribute key. - Class IDs are resolved via config or auto-generated using
larasup/reference(Book "models" + Item per class). - Class IDs are cached indefinitely via
Cache::rememberForever().
Dependencies
- larasup/reference — for automatic class ID management.
License
MIT