phospr/locale-bundle
A Symfony bundle for locale management in PHP applications. It centralizes locale configuration and provides services and helpers for handling available locales, making it easier to build consistent multilingual Symfony projects. Use it to organize locale settings and expose locale data to services...
composer.json and confirm the php/symfony/* requirements match your project. On Symfony 6/7 you will likely need to fork.composer require phospr/locale-bundle. With Flex it should auto-register; otherwise add Phospr\LocaleBundle\PhosprLocaleBundle::class => ['all' => true] to config/bundles.php.config/packages/phospr_locale.yaml. Start from the bundle's DependencyInjection/Configuration.php — with 0 stars the README is thin, and that file is the authoritative list of keys (available locales, default/fallback locale, session persistence toggles).src/EventListener/ for the kernel.request subscriber (that's where locale is actually set on the Request) and src/ for the negotiator service that parses Accept-Language. Those two classes are ~90% of the bundle's behavior.Accept-Language on each request so $request->getLocale() is already correct when your controllers run, falling back to framework.default_locale. Verify with a quick functional test that sends an Accept-Language header and asserts the locale — if that passes, the bundle is doing its one job.kernel.request listener reads Accept-Language, picks the best match from your allowlist, and writes it to the session so it survives redirects and subsequent requests. Rely on this rather than calling the negotiator by hand in controllers./locale/{locale} with a controller that validates locale against your configured allowlist, writes it to the session, and redirects back to the referer. The listener then honors it on the next request. Never trust the path segment without the allowlist check.available_locales array in config and inject it into both the bundle and any validator/switcher form. Drift between "what the listener accepts" and "what your UI offers" is the most common bug in this pattern.symfony/translation. Make sure the bundle's chosen locale lands on $request->setLocale(); the translator reads that, and falls back to framework.default_locale otherwise. If translations are stuck on the default, this link is broken.Accept-Language negotiation doesn't change URLs, which is bad for SEO and sharing. Day-to-day, prefer Symfony's built-in /{_locale}/... route prefix with a _locale requirement, and use this bundle only to guess the locale for first-time visitors before redirecting them to a prefixed URL.Accept-Language headers (fast, no kernel). Add one functional test per locale through $client->request(..., server: ['HTTP_ACCEPT_LANGUAGE' => ...]) to catch listener-priority regressions.LocaleListener, and Accept-Language parsing is a few lines with AcceptHeader/getLanguages() from symfony/http-foundation. For a single-locale-plus-fallback setup, hand-rolling is often less risk than an unmaintained bundle.kernel.request listener has higher/lower priority than the bundle's — inspect with bin/console debug:event-dispatcher kernel.request; (2) session isn't started or the cookie is dropped; (3) stale cache — run cache:clear; (4) a CDN/proxy strips or ignores Accept-Language.Vary: Accept-Language (and not cache session-dependent locales in a shared cache), or users will see each other's language.services.yaml rather than forking the repo. If a knob isn't exposed in config, a small compiler pass is cleaner than patching DependencyInjection/.How can I help you explore Laravel packages today?