- Can I use this bundle directly in a vanilla Laravel project without Symfony?
- No, this bundle is Symfony-centric and relies on Symfony’s Router, RequestContext, and HttpFoundation components. For Laravel, you’d need to abstract or replace these dependencies, which may not be worth the effort compared to native Laravel solutions like `Route::prefix('{locale}')` or middleware-based locale handling.
- What Laravel versions does this bundle support?
- The bundle itself targets Symfony, not Laravel, but if adapted, it could theoretically work with Laravel 8.x–11.x. However, compatibility depends on how well you abstract Symfony’s routing stack to match Laravel’s Illuminate/Routing system. Always test thoroughly in your target Laravel version.
- How do I install this in Laravel if it’s Symfony-based?
- Install via Composer (`composer require alvadi-it/i18n-routing-bundle`), but you’ll need to manually bridge Symfony components (e.g., Router, RequestContext) to Laravel’s equivalents. This typically involves creating middleware, overriding Laravel’s Router, and replacing Symfony’s URLGenerator with Laravel’s `route()` or `url()`.
- Does this bundle support subdomain-based locales (e.g., en.example.com)?
- Yes, but only if you configure it to use Symfony’s `RequestContext` for subdomain resolution. In Laravel, you’d need to replicate this logic via middleware (e.g., checking `Request::getHost()`) or a custom `BootstrapServiceProvider` to set the locale before routes are resolved.
- Will this bundle conflict with Laravel’s built-in routing or middleware?
- Yes, potential conflicts arise because the bundle injects Symfony’s routing logic into the stack. Laravel’s `Illuminate/Routing` and middleware pipeline may clash with Symfony’s `Router` and `LocaleListener`. You’ll need to decorate or replace Laravel’s Router and ensure middleware like `locale` doesn’t duplicate Symfony’s locale resolution.
- Are there Laravel-native alternatives to this bundle?
- Yes. For basic i18n routing, use Laravel’s built-in `Route::prefix('{locale}')` with middleware to set the locale. For advanced use cases, consider `spatie/laravel-translatable-urls` (for translatable models) or custom middleware to handle locale negotiation from subdomains, path segments, or headers.
- How do I generate locale-aware URLs (e.g., /en/posts) with this bundle in Laravel?
- The bundle uses Symfony’s `UrlGenerator`, which doesn’t natively integrate with Laravel’s `route()` or `url()` helpers. You’d need to override Laravel’s URL generator to inject the locale parameter (e.g., `route('posts', [], true)` with a custom `LocaleAwareUrlGenerator`). Alternatively, use query strings (`?locale=en`) as a fallback.
- Does this bundle work with API routes or JSON responses?
- The bundle supports locale-aware routing, but for APIs, you’ll need to manually handle `Content-Language` headers or query parameters. Laravel’s `AcceptLanguage` middleware or custom logic (e.g., parsing `Accept-Language` headers) may be simpler for APIs than integrating Symfony’s `LocaleContext`.
- How do I handle fallback locales (e.g., redirect /fr to /en if French is missing)?
- Configure the bundle’s `fallback_locale` in Symfony’s `routing.yml` or replicate this in Laravel via middleware. Check if the requested locale exists in your supported locales array, then redirect to the default locale (e.g., `abort(404)` or `redirect()->route('home', ['locale' => 'en'])`).
- What’s the performance impact of using this bundle in Laravel?
- The bundle adds Symfony’s routing stack, which is heavier than Laravel’s native router. Route caching may be less efficient, and dependency bloat could slow autoloading. For performance-critical apps, consider a lightweight alternative like custom middleware or a Laravel-specific package.