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.
en_US, en-us) with a strict Locale value object, reducing runtime errors in multilingual features.Accept-Language headers) and form validation.App::setLocale(), trans(), and packages like laravel-translatable by providing normalized inputs.User::locale as Locale instead of string), improving query safety and IDE support.fromString(), isValid()) allow incremental adoption without refactoring existing locale usage.FormRequest or Validator:
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).');
}
});
public function handle(Request $request, Closure $next) {
$locale = Locale::fromString($request->header('Accept-Language', config('app.locale')));
app()->setLocale($locale->toString());
return $next($request);
}
CHECK constraints.sr-Latn-RS for Serbian-Latin). Mitigation: Extend the enum or wrap calls in application-specific logic.Str::of() or helper methods to bridge old/new formats.$locale = Locale::tryFromString($input) ?? Locale::fromString(config('app.fallback_locale'));
shop_en) that require mapping to BCP 47?und for "undetermined") be tested in CI?Accept-Language headers into Locale objects.Locale::isValid()).trans() or Str::of().Locale typing (e.g., getLocaleAttribute()).en-US → PHP normalizes to en_US).| Phase | Goal | Example Changes | Effort | Dependencies |
|---|---|---|---|---|
| Validation | Replace ad-hoc checks | Replace preg_match() with Locale::isValid() in form requests. |
Low | None |
| Domain Modeling | Enforce typed locales | Update User model to use Locale accessors/mutators. |
Medium | Validation layer |
| System Integration | Centralize locale parsing | Add middleware to set Locale from headers. |
Low | Validation layer |
| API/Input | Validate API payloads | Use Locale::fromString() in GraphQL input types or API validation. |
Medium | Validation layer |
| Frontend | Sync client/server locales | Ensure JavaScript sends BCP 47-compliant strings (e.g., en-US) to PHP. |
Low | Backend normalization |
laravel-localization or spatie/laravel-translatable. Can coexist by treating this as a normalization layer.CHECK constraints to locale columns if using raw strings:
ALTER TABLE users ADD CONSTRAINT valid_locale CHECK (locale ~ '^[a-z]{2}(_[A-Z]{2})?$');
Locale accessors/mutators.Locale objects simplify updates (e.g., adding a new region).Locale::parse() simplify troubleshooting.| 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 | Use Eloquent accessors to validate before saving |
Locale facade or helper methods to simplify usage (e.g., Locale::current()).und, private-use tags).How can I help you explore Laravel packages today?