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.
Accept-Language headers or form inputs into Locale objects).trans() and Str::of() methods by providing normalized, validated locale strings.Illuminate\Validation\Rule for locale fields, reducing boilerplate.User::where('locale', Locale::fromString('fr_FR'))).fromString(), isValid(), normalize()) ensures it can be adopted incrementally without disrupting existing workflows. For example:
Locale::isValid().Locale types.spatie/laravel-translatable, laravel-localization). Can coexist by treating it as a "locale normalization" layer.und or private-use tags). Laravel’s testing tools (e.g., HttpTests) can easily verify integration.sr-Latn-RS or custom internal codes). Mitigation: Extend the package or wrap its methods in application-specific logic to handle niche cases.shop_en) may need mapping logic. Mitigation: Create a wrapper class to translate between internal and BCP 47 formats.Locale value object instead of raw strings?Locale objects from headers/cookies/form inputs, replacing ad-hoc logic.Locale::isValid($request->locale)).AppServiceProvider to normalize locales before passing to trans() or Str::of().getLocaleAttribute()).en-US to PHP, which normalizes to en_US).laravel-translatable) by providing a standardized locale layer.Phase 1: Validation Layer (Low Risk)
Locale::isValid().// Before:
if (!preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $locale)) { ... }
// After:
if (!Locale::isValid($locale)) { ... }
Phase 2: Domain Modeling (Medium Risk)
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 (High Impact)
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 normalized Locale objects.Phase 4: Advanced Use Cases (Optional)
CHECK constraints 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 to form requests | High | Low | None |
| Update API input validation | High | Medium | Validation layer |
| Create middleware for headers | Medium | Low | Validation layer |
| Update domain models | Medium | Medium | Validation layer |
| Integrate with localization services | Medium | Medium | Middleware/validation |
| Frontend integration | Low | Low | Backend normalization |
| Extend for custom locales | Low | High | Core integration |
Locale objects catch invalid values at compile time (PHP 8.1+) or runtime, reducing bugs.Locale objects improves code readability and onboarding for new developers.Locale::parse() simplify troubleshooting invalid locales.How can I help you explore Laravel packages today?