Installation
Add the bundle to your composer.json:
composer require audioteka/i18n-routing-bundle
Register the bundle in config/bundles.php:
return [
// ...
Audioteka\JMSI18nRoutingBundle\AudiotekaJMSI18nRoutingBundle::class => ['all' => true],
];
Configure Locales
Update config/packages/audioteka_jms_i18n_routing.yaml (or create it):
jms_i18n_routing:
default_locale: en
locales: [en, fr, es]
strategy: prefix_except_default
First Use Case
Define a route with translations in config/routes.yaml:
app_homepage:
path: /{_locale}/home
defaults: { _controller: App\Controller\HomeController::index }
requirements:
_locale: ^(en|fr|es)$
Access routes via:
$router->generate('app_homepage', ['_locale' => 'fr']);
Locale-Aware Routing
_locale as a route parameter (e.g., /{_locale}/products).strategy (prefix, suffix, domain, or custom) to handle locale placement./en/home, /fr/accueil).Dynamic Locale Detection
Request to auto-detect locale from:
/fr/...).fr.example.com).Accept-Language).JMSI18nRoutingBundle\Routing\Router to generate locale-aware URLs.Translation in Controllers
TranslatorInterface to fetch translated content:
public function index(TranslatorInterface $translator)
{
return $this->render('home/index.html.twig', [
'title' => $translator->trans('home.title'),
]);
}
Route Collections
app_locales:
resource: '@AppBundle/Resources/config/routing/locales.yaml'
type: jms_i18n_routing
symfony/routing v5+ with require-dev constraints.JMSI18nRoutingBundle\Twig\RoutingExtension for locale-aware path() in templates.NelmioApiDocBundle for multilingual API docs by overriding _locale in route annotations.Locale Parameter Conflicts
_locale if they clash with existing parameters._lang or rename in requirements.Caching Issues
locales or strategy:
php bin/console cache:clear
Symfony 2.1 Legacy
Router class to adapt to Symfony 4/5 routing.Domain Strategy Quirks
fr.example.com) requires DNS/subdomain setup.jms_i18n_routing.domain config to map locales to subdomains.php bin/console debug:router to inspect locale-aware routes.public function index(Request $request)
{
$locale = $request->get('_locale', $this->container->getParameter('locale'));
// ...
}
Custom Strategies
JMSI18nRoutingBundle\Routing\Strategy\StrategyInterface for unique locale handling (e.g., query params like ?lang=fr).Event Listeners
jms_i18n_routing.locale_resolved to modify locale resolution dynamically.Override Templates
config/packages/twig.yaml:
twig:
globals:
jms_i18n_routing: '@jms_i18n_routing.twig.extension'
fallback_locale to redirect unsupported locales to default_locale.jms_i18n_routing.cache_enabled: true.RouterInterface mocks in PHPUnit to test locale-aware routes:
$router = $this->createMock(RouterInterface::class);
$router->method('generate')->willReturn('/fr/test');
How can I help you explore Laravel packages today?