letkode / locale-bundle
Locale provider, listener and translatable field applier for Symfony applications
Package info
github.com/letkode/locale-bundle
Type:symfony-bundle
pkg:composer/letkode/locale-bundle
Requires
- php: ^8.4
- symfony/config: ^7.0 || ^8.0
- symfony/dependency-injection: ^7.0 || ^8.0
- symfony/http-foundation: ^7.0 || ^8.0
- symfony/http-kernel: ^7.0 || ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2
- phpunit/phpunit: ^11
README
Locale provider, listener and translatable field applier for Symfony applications.
Installation
composer require letkode/locale-bundle
Symfony Flex will register the bundle automatically. If not using Flex, add it manually:
// config/bundles.php return [ Letkode\LocaleBundle\LetkodeLocaleBundle::class => ['all' => true], ];
Configuration
Create config/packages/letkode_locale.yaml:
letkode_locale: default_locale: '%env(APP_DEFAULT_LOCALE)%' supported_locales: '%env(json:APP_SUPPORTED_LOCALES)%'
Add the corresponding .env variables:
APP_DEFAULT_LOCALE=en APP_SUPPORTED_LOCALES=["en","es"]
Contents
LocaleProvider
Resolves the active locale for the current request. Inject it in services that need locale-awareness.
public function __construct(private readonly LocaleProvider $localeProvider) {} public function doSomething(): void { $locale = $this->localeProvider->getLocale(); // 'en' | 'es' | ... }
LocaleListener
Kernel event listener (priority 20) that sets $request->setLocale() on every request. Reads from:
_localequery parameterAccept-Languageheader (first matching supported locale)- Configured
default_localeas fallback
TranslatableFieldApplier
Applies translated values from a translations jsonb map to entity fields for the active locale.
// Entity has: public array|null $translations = null; (via HasTranslationsTrait) // translations = {"en": {"name": "Module"}, "es": {"name": "Módulo"}} $this->translatableFieldApplier->apply($entity, ['name']); // $entity->name is now set to the locale-specific value
Requirements
- PHP
^8.4 - Symfony
^7.0 || ^8.0
License
MIT — see LICENSE.