aaronadal/trans-routing-bundle
Installation
composer require aaronadal/trans-routing-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
return [
// ...
Aaronadal\TransRoutingBundle\AaronadalTransRoutingBundle::class => ['all' => true],
];
Configuration
Define locales in config/packages/aaronadal_trans_routing.yaml (Symfony) or config/trans_routing.php (Laravel):
locales: [en, fr, es]
default_locale: en
First Use Case
Annotate a route with @TransRouting in a Symfony controller:
use Aaronadal\TransRoutingBundle\Annotation\TransRouting;
/**
* @Route("/{_locale}/home", name="home")
* @TransRouting(locales={"en": "home", "fr": "accueil"})
*/
public function homeAction(Request $request, string $_locale) { ... }
Laravel Alternative: Use route model binding or middleware to inject _locale and map translations in routes/web.php:
Route::get('/{locale}/home', [HomeController::class, 'index'])
->where('locale', implode('|', config('trans_routing.locales')))
->middleware('locale');
Route Translation Mapping
@TransRouting (Symfony) or route groups (Laravel) to define locale-specific route names.# config/routes/trans_routes.yaml
home:
path: /{_locale}/home
defaults: { _controller: App\Controller\HomeController::home }
requirements:
_locale: en|fr|es
trans_routing:
en: home_en
fr: accueil
es: inicio
Locale Detection
RequestStack or Laravel’s AppServiceProvider to auto-detect locale from:
{_locale}).app()->getLocale()).Accept-Language).public function handle($request, Closure $next) {
$locale = $request->route('_locale') ?? session('locale') ?? config('app.locale');
app()->setLocale($locale);
return $next($request);
}
Dynamic Route Generation
TransRouter service (Symfony) or a custom helper (Laravel) to generate locale-aware URLs:
// Laravel: Add to AppServiceProvider
$router = new \Aaronadal\TransRoutingBundle\Router\TransRouter($this->app['router']);
$router->setLocales(config('trans_routing.locales'));
// Generate URL
route('home', ['_locale' => 'fr']); // /fr/accueil
Fallback Logic
config/trans_routing.php:
'fallback_locale' => 'en',
'fallback_routes' => [
'fr' => 'en', // Redirect fr/* to en/* if no translation exists
],
Locale Parameter Conflicts
{_locale} doesn’t clash with other route parameters. Use underscores or reserved namespaces.{lang} or {l} if needed, and update middleware/config accordingly.Caching Issues
@TransRouting annotations immediately.php bin/console cache:clear
php artisan route:clear or php artisan config:clear.Missing Translations
default_locale. Test edge cases:
// Laravel: Add to AppServiceProvider
$this->app->afterResolving('router', function ($router) {
$router->missing(function ($request) {
return redirect()->route('home', ['_locale' => config('trans_routing.default_locale')]);
});
});
Case Sensitivity
@TransRouting are case-sensitive. Use consistent casing (e.g., home vs. Home).Dump Route Collection
$router->getRouteCollection()->get('home')->getPath();
dd(Route::getRoutes()->getByName('home')->uri());
Log Locale Resolution
// Symfony EventSubscriber
public function onKernelRequest(GetResponseEvent $event) {
$request = $event->getRequest();
$this->logger->info('Locale resolved:', ['locale' => $request->get('_locale')]);
}
Test Locale-Specific Routes
public function testLocaleRoutes() {
foreach (config('trans_routing.locales') as $locale) {
$url = route('home', ['_locale' => $locale]);
$this->assertStringContainsString($locale, $url);
}
}
Custom Route Translator
TransRouter to add logic (e.g., SEO-friendly slugs):
class CustomTransRouter extends \Aaronadal\TransRoutingBundle\Router\TransRouter {
public function generate($name, $parameters = [], $absolute = false) {
$parameters['_locale'] = $this->generateSlug($parameters['_locale']);
return parent::generate($name, $parameters, $absolute);
}
}
services.yaml (Symfony) or AppServiceProvider (Laravel).Database-Backed Translations
route_translations table and fetch them dynamically:
// Laravel: Add to TransRouter
public function getTranslatedName($name, $locale) {
return DB::table('route_translations')
->where('route_name', $name)
->where('locale', $locale)
->value('translated_name') ?? $name;
}
API Integration
Route::get('/api/routes', function () {
return collect(config('trans_routing.locales'))
->mapWithKeys(fn($locale) => [
$locale => route('home', ['_locale' => $locale])
]);
});
How can I help you explore Laravel packages today?