laravel-lang/attributes
Laravel Lang: Attributes adds PHP attribute helpers for Laravel Lang packages, simplifying localization-related metadata and tooling. Includes documentation, tests, and easy Composer installation for Laravel projects.
<x-input :label="$field->getLabel()" />).rules()).[Label('Email Address')]). This contrasts with traditional __('attributes.email') calls, offering a more maintainable alternative.[Placeholder], [Tooltip]) and fallback logic, making it adaptable to:
composer require laravel-lang/attributes
and registering the service provider in config/app.php. No migrations or complex configurations.@label($field))."The first_name must be..." → "El nombre debe...").FormRequest validation rules.__() calls; attributes are optional. Ideal for phased adoption in legacy codebases.getAttributes() and Laravel’s Translatable facades. Example:
$reflection = new ReflectionClass(UserRequest::class);
$label = $reflection->getProperty('email')->getAttributes(Label::class)[0]->newInstance();
$this->assertEquals('Correo Electrónico', $label->value);
| Risk Area | Impact | Mitigation |
|---|---|---|
| PHP 8+ Dependency | Breaking for PHP 7.x apps | Audit app’s PHP version; upgrade if needed (Laravel 9+ requires PHP 8.0+). |
| Attribute Reflection | Minor runtime overhead | Benchmark in high-traffic forms; cache resolved attributes if critical. |
| Translation Conflicts | Overrides vs. __() calls |
Enforce naming conventions (e.g., attributes.validation.first_name) and document migration steps. |
| Blade Caching | Stale views if attributes change | Add php artisan view:clear to deployment scripts or use @once directives in Blade. |
| Package Maturity | Low stars (27) but active maintenance | Validate via GitHub issues/PRs (e.g., recent 2026 releases) and MIT license. |
| Custom Attribute Support | Undocumented extensions | Prototype custom attributes (e.g., [Tooltip]) and contribute back to the package. |
__() calls entirely, or coexist with them? If the latter, how will conflicts be resolved?$start = microtime(true);
$reflection->getAttributes(Label::class);
$time = microtime(true) - $start;
en.json lacks first_name), what’s the fallback? Raw attribute name? Default __() call?__()? Example:
// Before: __('attributes.first_name')
// After: [Label('Nombre')]
__() calls?es.json is missing first_name)?en.json vs. es.json for gaps.<x-input :label="$field->label" />).required|first_name → "El nombre es obligatorio").rules() and messages() dynamically.UserResource::make($user)->additional(['label' => 'Usuario'])).[Label('Email')]) for type-safe metadata, reducing magic strings.trans() helper, supporting:
resources/lang/).spatie/laravel-translation-loader).| Phase | Action | Tools/Examples |
|---|---|---|
| Assessment | Audit existing __() calls for form attributes. Identify high-impact fields (e.g., checkout). |
grep -r "__('attributes\." app/ |
| Pilot | Replace __() in a single form (e.g., login page). Test Blade/validation integration. |
php<br>// Before: __('attributes.email')<br>// After: [Label('Correo')]<br> |
| Incremental Rollout | Migrate forms by module (e.g., user profile → settings → checkout). | Use feature flags to toggle attribute vs. __() usage. |
| Deprecation | Deprecate __() calls in favor of attributes (e.g., PHPStan rules). |
Add @deprecated to __() calls in attributes.php. |
| Optimization | Cache resolved attributes for high-traffic forms. | php<br>Cache::remember('form_attributes', 60, fn() => $reflection->getAttributes(Label::class));<br> |
FormRequest validation.$this->append('label', $field->label)).expect() for translated labels.composer require laravel-lang/attributes
php artisan vendor:publish --provider="LaravelLang\Attributes\AttributesServiceProvider"
config/attributes.php (e.g., default locale, fallback strategy).__() in a single form (e.g., app/Http/Requests/LoginRequest.php):
use LaravelLang\Attributes\Label;
class LoginRequest extends FormRequest {
#[Label('Correo Electrónico')]
public string $email;
}
resources/lang/es/validation.php).es.json lacksHow can I help you explore Laravel packages today?