- How do I install and configure codezero/laravel-localizer?
- Run composer require codezero/laravel-localizer, then publish the package config with php artisan vendor:publish so config/localizer.php is available. In that file you whitelist the locales your app supports and decide whether locale comes from the URL prefix or the session. Register the middleware in the web group in app/Http/Kernel.php, or in bootstrap/app.php on Laravel 11+, placing it after the session middleware and before route model binding and controllers.
- Which Laravel and PHP versions does laravel-localizer support?
- Check the composer.json constraints in the repository before committing, because the last tagged release was March 2024 and its supported Laravel range may lag behind the newest major. The package relies on standard service provider and HTTP kernel APIs, so it generally runs on the Laravel versions current at that time. If you are planning an upgrade to a newer Laravel major, verify the constraint set against your target Laravel and PHP matrix first.
- How does laravel-localizer detect a visitor's locale automatically?
- The middleware inspects the incoming request, primarily the Accept-Language header, and matches it against the locales you have whitelisted in config/localizer.php. If nothing matches it falls back to your configured default locale. Test q-values, wildcards, and malformed headers explicitly, since those edge cases determine what your fallback actually does in practice.
- How do I create localized routes with a locale prefix?
- The package provides route macros that prepend an optional {locale?} segment to your route definitions, so one controller can serve both /en/products and /nl/products. Because the segment is optional, URLs without a prefix still resolve using the detected or default locale. Register these macros before running route:cache, and clear the route cache whenever your locale configuration changes.
- Does laravel-localizer support GeoIP-based locale detection?
- GeoIP detection is optional and pulls in a separate IP-locator dependency, so it is not part of the core install. If you enable it, you take on an external database or service whose staleness, cost, and GDPR or data-residency implications need review. For most applications Accept-Language detection is sufficient and avoids that dependency entirely.
- How do I stop a CDN from serving the wrong language with laravel-localizer?
- Locale varies by request header, so responses must include Vary: Accept-Language, plus Vary: Cookie if locale is stored in the session, or your CDN and reverse proxy will cache one language and serve it to every visitor. Alternatively, keep locale in the URL prefix so each language has a distinct cache key. This is the most likely production incident with header-based detection, so verify it against your actual caching topology in staging.
- Should I use laravel-localizer or mcamara/laravel-localization?
- mcamara/laravel-localization is the older, more widely adopted option with a larger feature surface, while laravel-localizer is a thinner package focused on detection plus optional {locale} route macros. Choose based on whether you need the extra features or prefer a minimal component you can reason about quickly. Do not run both in the same application, since both hook into routing and locale resolution and will conflict.
- How do I build a language switcher with laravel-localizer?
- The package ships helper methods you can call from Blade to generate a link to the current route in another locale. A typical switcher loops over your supported locales and outputs one link per locale, preserving the current path. Because the generated URLs carry the locale prefix, they are also shareable and indexable, which is exactly what you want for SEO.
- What happens when a visitor requests an unsupported locale?
- Requests with a locale that is not in your whitelist fall back to the default locale configured in config/localizer.php rather than throwing an error. Missing translation keys are handled by Laravel's own translator fallback, not by this package, so you control that through your lang files and fallback_locale setting. Decide deliberately whether you want a silent fallback or a visible 404 for unknown locale prefixes.
- Is codezero/laravel-localizer safe to use in production?
- It is a small, single-maintainer package with a modest install base and a last release in March 2024, so treat the bus factor as a real risk and pin the version in composer.json. The code itself is a thin middleware and route macro layer, which makes it relatively easy to fork or replace with an in-house resolver if maintenance stops. Run it through your normal vendor review, checking composer constraints, CI status, and open issues before adopting.