Installation:
composer require besimple/i18n-routing-bundle
Register the bundle in AppKernel.php:
new BeSimple\I18nRoutingBundle\BeSimpleI18nRoutingBundle(),
Configure:
Add to config.yml:
be_simple_i18n_routing: ~
Define Routes:
Create a dedicated routing file (e.g., src/MyBundle/Resources/config/routing/i18n.yml) and import it with the be_simple_i18n type:
my_i18n_routes:
resource: "@MyBundle/Resources/config/routing/i18n.yml"
type: be_simple_i18n
prefix:
en: /website
fr: /site
de: /webseite
Define I18n Routes:
In i18n.yml, use the i18n prefix for routes:
home:
path: /{_locale}/home
defaults: { _controller: MyBundle:Default:index }
requirements:
_locale: en|fr|de
First Use Case:
Access /en/home or /fr/accueil (if translated). The locale (_locale) is automatically detected and updated in the URL.
Locale-Aware Routing:
{_locale} as a route parameter to enforce locale detection.product:
path: /{_locale}/product/{id}
defaults: { _controller: MyBundle:Product:show }
/en/product/123 or /fr/produit/123.Prefixes for Language Segments:
prefix key during route import:
my_routes:
resource: "@MyBundle/Resources/config/routing/i18n.yml"
type: be_simple_i18n
prefix:
en: /en
fr: /fr
/en/home or /fr/accueil are generated automatically.Dynamic Locale Switching:
be_simple_i18n_routing.switch_locale route to switch locales dynamically:
_switch_locale:
path: /{_locale}
defaults: { _controller: BeSimpleI18nRoutingBundle:Routing:switchLocale }
/fr to switch from /en/home to /fr/accueil.Parameter Translation:
id) between locales using a translator or Doctrine DBAL backend.config.yml:
be_simple_i18n_routing:
translator: ~ # Uses Symfony Translator
# OR
dbal:
driver: pdo_mysql
host: localhost
dbname: my_db
user: root
password: secret
Integration with Controllers:
Request object:
$locale = $request->get('_locale');
UrlGenerator:
$url = $this->generateUrl('home', ['_locale' => 'fr']);
Fallback Locales:
be_simple_i18n_routing:
fallback_locale: en
Locale Parameter Conflicts:
{_locale} does not conflict with other route parameters. Use _locale (with underscore) to avoid naming collisions.# Bad: 'locale' conflicts with other parameters
path: /{locale}/home
path: /{_locale}/home
Caching Issues:
php bin/console cache:clear
php bin/console cache:pool:clear cache.app
Missing Locale Requirements:
_locale requirements to avoid 404 errors:
requirements:
_locale: en|fr|de
/xyz/home may fail silently.Prefix Overrides:
type: be_simple_i18n block override individual route paths. Ensure consistency:
# Prefix /en/ overrides the path in the route
my_routes:
prefix:
en: /custom-en
# Route path becomes /custom-en/home, not /en/home
home:
path: /{_locale}/home
Parameter Translation Quirks:
CREATE TABLE route_parameters (
locale VARCHAR(10),
parameter VARCHAR(50),
value VARCHAR(255),
PRIMARY KEY (locale, parameter)
);
php bin/console cache:clear
Symfony 4+ Compatibility:
auto-import feature in config/packages/routing.yml:
imports:
- resource: ../src/MyBundle/Resources/config/routing/i18n.yml
type: be_simple_i18n
Route Dumping: Use the debug:router command to inspect generated routes:
php bin/console debug:router | grep i18n
Locale Switching Debug:
_switch_locale route is registered:
php bin/console debug:router | grep switch_locale
BeSimpleI18nRoutingBundle:Routing:switchLocale is accessible.Parameter Translation Debug:
config.yml.php bin/console doctrine:query:sql "SELECT * FROM route_parameters"
URL Generation Issues:
UrlGeneratorInterface to debug URL generation:
$generator = $this->container->get('router');
$url = $generator->generate('home', ['_locale' => 'fr']);
Custom Locale Provider:
Override the default locale detection by implementing BeSimple\I18nRoutingBundle\Locale\LocaleProviderInterface:
class CustomLocaleProvider implements LocaleProviderInterface {
public function getLocale(Request $request) {
return $request->headers->get('X-Locale', 'en');
}
}
Register as a service:
services:
besimple_i18n_routing.locale_provider:
class: AppBundle\Locale\CustomLocaleProvider
tags:
- { name: besimple_i18n_routing.locale_provider }
Dynamic Prefixes: Use a service to generate dynamic prefixes based on runtime logic:
class DynamicPrefixGenerator {
public function getPrefix($locale) {
return '/dynamic-'.$locale;
}
}
Configure in config.yml:
be_simple_i18n_routing:
prefix_generator: service_id
Event Listeners:
Listen to be_simple_i18n_routing.locale_changed events to react to locale switches:
class LocaleChangeListener {
public function onLocaleChanged(LocaleChangedEvent $event) {
// Custom logic (e.g., update session, redirect)
}
}
Register as a service:
services:
app.locale_change_listener:
class: AppBundle\EventListener\LocaleChangeListener
tags:
- { name: kernel.event_listener, event: be_simple_i18n_routing.locale_changed }
How can I help you explore Laravel packages today?