php-standard-library/locale
PHP Standard Library Locale component providing locale-aware formatting and parsing utilities. Helps handle language/region settings, localized dates, numbers, and other internationalization tasks in PHP apps with a lightweight, straightforward API.
Locale) aligns well with Laravel’s domain modeling patterns, especially in applications requiring strict i18n validation (e.g., multilingual e-commerce, global SaaS platforms). It enforces consistency in locale handling across services, APIs, and UI layers.AppServiceProvider, middleware, or form requests).Accept-Language headers or form inputs (e.g., request()->input('locale')) with validated Locale objects.trans() and Str::of() methods by providing normalized locale strings.User::where('locale', Locale::fromString('fr_FR'))), reducing SQL injection risks and improving query readability.symfony/polyfill), ensuring compatibility with Laravel’s ecosystem. No database migrations or complex setup required.Locale::parse(), Locale::normalize()) make it easy to adopt incrementally. Example:
use PhpStandardLibrary\Locale\Locale;
// In a controller or service:
$locale = Locale::fromString(request()->input('locale', 'en_US'));
app()->setLocale($locale->toString());
Illuminate\Validation\Rule for locale fields:
use PhpStandardLibrary\Locale\Locale;
$validator->rule(function ($attribute, $value, $fail) {
if (!Locale::isValid($value)) {
$fail('The :attribute must be a valid locale (e.g., en_US).');
}
});
sr-Latn-RS). Mitigation: Extend the package or wrap its methods in application-specific logic.locale() helper) may evolve. Risk: Potential duplication of efforts if Laravel adds similar functionality. Mitigation: Treat as a "batteries-included" layer for now.und for "undetermined" locales). Laravel’s testing tools (e.g., HttpTests) can easily verify integration.Locale type?shop_en) that need mapping to BCP 47?Locale object from headers/cookies.AppServiceProvider to normalize locales before passing to trans() or Str::of().en-US to PHP, which normalizes to en_US).Phase 1: Validation Layer
Locale::isValid().// Before:
if (!preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $locale)) { ... }
// After:
if (!Locale::isValid($locale)) { ... }
Phase 2: Domain Modeling
User::locale) with Locale value objects.Locale methods:
// Before:
User::where('locale', 'en_US')->get();
// After:
User::where('locale', Locale::fromString('en_US')->toString())->get();
public function getLocaleAttribute(string $locale): Locale {
return Locale::fromString($locale);
}
Phase 3: System Integration
Locale object from Accept-Language headers:
public function handle(Request $request, Closure $next) {
$locale = Locale::fromString($request->header('Accept-Language', config('app.locale')));
app()->setLocale($locale->toString());
return $next($request);
}
trans() calls) to rely on the normalized Locale object.laravel-localization, spatie/laravel-translatable). Can coexist by treating this as a "locale normalization" layer.CHECK constraint to locale columns if using raw strings:
ALTER TABLE users ADD CONSTRAINT valid_locale CHECK (locale ~ '^[a-z]{2}(_[A-Z]{2})?$');
| Step | Priority | Effort | Dependencies |
|---|---|---|---|
| Add validation | High | Low | None |
| Update form requests | High | Medium | Validation layer |
| Middleware for headers | Medium | Low | Validation layer |
| Domain model updates | Medium | Medium | Validation layer |
| API/input validation | High | Medium | Validation layer |
| Frontend integration | Low | Low | Backend normalization |
en_US vs. en-us).Locale objects make it trivial to update locale handling (e.g., adding a new region) without searching for strings.Locale::parse() simplify troubleshooting invalid locales.en_US) can be cached in middleware or services to avoid repeated parsing.| Scenario | Impact | Mitigation |
|---|---|---|
| Invalid locale in request | 400 Bad Request | Fallback to default or reject |
| Locale parsing edge case | Silent corruption | Extend package or wrap with try-catch |
| Database constraint violation | Query failure |
How can I help you explore Laravel packages today?