eb78/custom-i18n-router-bundle
Installation
composer require eb78/custom-i18n-router-bundle
Register the bundle in config/bundles.php:
EB78\CustomI18nRouterBundle\EB78CustomI18nRouterBundle::class => ['all' => true],
Configure Locales
Add available_locales and default_locale to config/packages/eb78_custom_i18n_router.yaml:
parameters:
available_locales: ['en-us', 'fr-fr']
default_locale: 'en-us'
Define Route Mappings
Create a YAML file (e.g., config/i18n_en-us.yaml) with locale-specific routes:
parameters:
i18n_en-us:
prefix: "" # Optional URL prefix (e.g., "/en")
name: "english" # Display name
localeUId: 1 # Unique ID for the locale
locale: "en_US" # Symfony locale code
host: "example.com" # Domain for this locale
routes:
home: /home/
contact: /contact/
First Use Case Generate a locale-aware URL in a controller:
use EB78\CustomI18nRouterBundle\Router\I18nRouter;
public function index(I18nRouter $router)
{
return $router->generate('home', ['_locale' => 'fr-fr']);
}
Dynamic Route Generation
Use the I18nRouter service to generate locale-specific URLs:
$url = $router->generate('contact', ['_locale' => 'fr-fr']);
// Outputs: "https://yourdomain.fr/contactez-nous/"
Locale Switching Redirect users to a different locale while preserving the current route:
$router->redirectToLocale('en-us', $request->get('_route'));
Host-Based Routing
Configure host in the locale YAML to enforce domain-level routing:
host: "fr.example.com"
The router will automatically redirect to the correct host.
Prefix-Based Routing
Use the prefix parameter to add a path segment (e.g., /fr/):
prefix: "/fr"
routes:
home: "/accueil/"
Generated URL: /fr/accueil/.
Integration with Symfony’s Router Extend the default router to include locale logic:
// In services.yaml
services:
App\Router\CustomRouter:
decorates: router.default
arguments: ['@.inner']
user_profile).default_locale for missing translations.Locale File Naming
i18n_{locale}.yaml (e.g., i18n_fr-fr.yaml).find config -name "i18n_*.yaml"
Host vs. Prefix Conflicts
host and prefix for the same locale. Example:
# Bad: Both host and prefix will override each other.
host: "fr.example.com"
prefix: "/fr"
Route Parameter Mismatches
routes:
user_show: "/users/{id}" # Must match `@Route("/users/{id}", name="user_show")`
Locale Parameter Overrides
_locale parameter in generate() takes precedence over the default locale. Example:
$router->generate('home', ['_locale' => 'fr-fr']); // Forces French, even if default is 'en-us'.
Symfony 4+ Compatibility
// In a Symfony 5 project, create a bridge bundle:
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CustomI18nRouterBridgeBundle extends Bundle {}
Check Generated URLs Dump the router’s internal mappings:
$router->getRouteCollection()->getResources();
Validate Locale Config
Use the EB78\CustomI18nRouterBundle\Manager\LocaleManager service to list available locales:
$locales = $this->get('eb78_custom_i18n_router.locale_manager')->getAvailableLocales();
Enable Debug Mode
Add this to config/packages/dev/eb78_custom_i18n_router.yaml:
parameters:
debug: true
This logs route generation and locale resolution.
Handle Missing Routes
Catch EB78\CustomI18nRouterBundle\Exception\LocaleRouteNotFoundException:
try {
$url = $router->generate('nonexistent_route');
} catch (LocaleRouteNotFoundException $e) {
return new Response('Route not found', 404);
}
Custom Route Loaders
Extend EB78\CustomI18nRouterBundle\Loader\YamlFileLoader to support additional formats (e.g., JSON):
class JsonFileLoader extends YamlFileLoader {
public function load($resource, $type = null) { /* ... */ }
}
Dynamic Locale Resolution
Override EB78\CustomI18nRouterBundle\Resolver\LocaleResolver to add logic (e.g., user preferences):
class CustomLocaleResolver extends LocaleResolver {
public function getLocale(Request $request) {
$userLocale = $request->getSession()->get('user_locale');
return $userLocale ?: parent::getLocale($request);
}
}
Add Locale-Specific Middleware Use Symfony’s middleware pipeline to modify requests/responses:
// In config/packages/dev/http_client.yaml
http_client:
middleware:
- EB78\CustomI18nRouterBundle\Middleware\LocaleMiddleware
How can I help you explore Laravel packages today?