- How does this package improve Laravel’s built-in localization compared to using strings like 'en_US' directly?
- This package replaces loose string handling with a strict `Locale` enum, ensuring type safety and preventing runtime errors from invalid or inconsistent locale formats. It integrates with Laravel’s `trans()`, `setLocale()`, and validation systems while enforcing consistency across your app. For example, `Locale::fromString('en-us')` normalizes to `en_US`, reducing edge cases in multilingual features.
- Can I use this in Laravel middleware to parse Accept-Language headers?
- Yes. The package provides static methods like `Locale::fromString()` to parse headers or cookies into typed `Locale` objects. You can create middleware to normalize `Accept-Language` headers (e.g., `en-US` → `en_US`) and set the app locale dynamically. Example: `$locale = Locale::fromString($request->header('Accept-Language')); app()->setLocale($locale->toString());`
- Does this work with Laravel’s Eloquent models for typed locale fields?
- Absolutely. Store locale fields as `Locale` enums in your models (e.g., `User::locale`) instead of strings. Use accessors to enforce typing: `public function getLocaleAttribute(): Locale { return Locale::fromString($this->attributes['locale']); }`. This improves IDE autocompletion, query safety, and validation while reducing manual checks.
- What Laravel versions and PHP versions are supported?
- The package requires PHP 8.1+ (for enums) and is designed for Laravel 9+. It leverages modern PHP features like typed properties and strict mode, so ensure your project meets these requirements. Check the [documentation](https://php-standard-library.dev) for exact version constraints.
- How do I validate user input (e.g., form submissions) for valid locales?
- Use the `Locale::isValid()` method in Laravel’s `FormRequest` or `Validator`. Example: `$validator->rule(function ($attribute, $value) { if (!Locale::isValid($value)) { $fail('Invalid locale format.'); } });`. This replaces regex or manual checks with a type-safe, maintainable solution.
- What if a user submits an invalid locale like 'xyz_123'? Should it throw an error or fall back?
- The package offers both strict and lenient approaches. Use `Locale::fromString()` for exceptions or `Locale::tryFromString()` for fallback to a default (e.g., `config('app.fallback_locale')`). Example: `$locale = Locale::tryFromString($input) ?? Locale::fromString('en_US');`
- Does this package handle niche BCP 47 locale tags (e.g., 'sr-Latn-RS')?
- The package covers 700+ standard locales but may not include all niche BCP 47 variants. For custom tags, extend the enum or wrap calls in application logic. Check the [GitHub issues](https://github.com/php-standard-library/php-standard-library/issues) for updates or contribute missing tags.
- How does this integrate with Laravel’s `trans()` helper for translations?
- Normalize locales before passing them to `trans()`. Example: `trans('messages.welcome', [], Locale::fromString($user->locale)->toString())`. This ensures consistency between your typed `Locale` objects and Laravel’s translation system, which expects strings like `en_US`.
- Are there performance concerns for bulk locale validation (e.g., 10K user profiles)?
- The package is optimized for low overhead, but bulk operations should be tested. Cache normalized locales in middleware or services to avoid repeated parsing. Example: Cache `Locale::fromString()` results in a static array or Redis for high-throughput scenarios.
- What’s the migration path if my app already uses hardcoded locale strings in views/config?
- Use Laravel’s `Str::of()` or helper methods to bridge old and new formats. Example: Replace `{{ $user->locale }}` with `{{ Str::of($user->locale)->upper() }}` temporarily, then refactor to typed `Locale` objects. For config, update `app.locale` to a string but normalize inputs via the package.