Installation:
composer require sylius/locale-bundle
Add the bundle to config/bundles.php:
return [
// ...
Sylius\Bundle\LocaleBundle\SyliusLocaleBundle::class => ['all' => true],
];
Configuration:
Override default locales in config/packages/sylius_locale.yaml:
sylius_locale:
locales: ['en_US', 'fr_FR', 'es_ES'] # Customize as needed
default_locale: 'en_US'
First Use Case:
Route-based locale switching:
Enable the locale router requirement in config/routes.yaml:
sylius_locale:
resource: "@SyliusLocaleBundle/Resources/config/routing.yaml"
Access routes like /en_US/products or /fr_FR/products.
Twig integration:
Use {{ app.request.locale }} in templates to dynamically render locale-specific content.
Locale-Aware Routing:
locale router requirement to enforce locale prefixes (e.g., /{_locale}/...).sylius_locale.locale event listeners to modify behavior per locale (e.g., redirect to default locale if unsupported).Dynamic Locale Switching:
sylius_locale.switch_locale route:
<a href="{{ path('sylius_locale_switch', {'_locale': 'fr_FR'}) }}">Français</a>
Translation Integration:
symfony/translation for locale-specific messages:
# config/packages/translation.yaml
framework:
translator:
paths: ['%kernel.project_dir%/translations']
default_path: '%kernel.project_dir%/translations'
{{ 'message.key'|trans }} in Twig with automatic locale fallback.Database-Driven Locales:
Locale entity) by overriding the LocaleProvider service.API Locale Handling:
Accept-Language header to set the locale:
// src/EventListener/LocaleListener.php
use Sylius\Component\Locale\Context\LocaleContextInterface;
public function onKernelRequest(GetResponseEvent $event): void
{
$request = $event->getRequest();
$locale = $request->headers->get('Accept-Language', $this->localeContext->getLocale());
$this->localeContext->setLocale($locale);
}
Locale Prefix Conflicts:
{_locale} requirement. Use requirements: _locale: \w+ in route definitions to enforce valid locale patterns.Caching Issues:
sylius_locale.locales:
php bin/console cache:clear
cache:pool:clear sylius_locale to avoid full cache rebuilds.Fallback Locale Misconfiguration:
en_US. Override this in the LocaleProvider or configure a custom fallback:
sylius_locale:
fallback_locale: 'en_US'
Twig Auto-Reloading:
debug: true is set in framework config and restart the dev server.// In a controller or Twig
dump($this->get('sylius.locale.context')->getLocale());
en_US, not en). Use Locale::getPrimaryLanguage() to validate.Custom Locale Provider: Override the default provider to fetch locales dynamically:
# config/services.yaml
Sylius\Component\Locale\Provider\LocaleProviderInterface sylius.locale.provider:
class: App\Locale\CustomLocaleProvider
arguments:
- '@your_database_service'
Event Listeners:
Subscribe to sylius.locale.switch to log locale changes or trigger side effects:
use Sylius\Component\Locale\Event\LocaleSwitchEvent;
public function onLocaleSwitch(LocaleSwitchEvent $event): void
{
// Example: Update user's preferred locale in DB
$event->getUser()->setPreferredLocale($event->getLocale());
}
Route Overrides:
Extend or replace the default routing by copying SyliusLocaleBundle/Resources/config/routing.yaml to config/routes/sylius_locale.yaml and modifying it.
sylius_locale.locales to avoid runtime lookups.How can I help you explore Laravel packages today?