chinleung/laravel-multilingual-routes
Register multilingual Laravel routes from a single definition. Automatically generates locale-prefixed URLs based on configured locales, with optional default-locale prefixing. Includes middleware to detect request locale and switch the app locale accordingly.
Pros:
en.home, fr.home), simplifying route references in Blade templates, controllers, and redirects.resources/lang/{locale}/routes.php for URI translations, enabling dynamic and maintainable path structures (e.g., /test in English vs. /teste in French).resource() method to generate multilingual CRUD routes, reducing boilerplate for common patterns.DetectRequestLocale middleware to automatically infer locale from request (e.g., subdomains, path prefixes, or headers), integrating with Laravel’s built-in localization stack.only(), except(), names()), mirroring Laravel’s native route API.Cons:
routes.php files per locale, which could become cumbersome for large applications or frequently changing paths.en) may not be prefixed by default, which could lead to ambiguity if not configured explicitly (e.g., /test vs. /en/test).app.locales and external locale configurations (e.g., chinleung/laravel-locales), accommodating varying project setups.DetectRequestLocale to be the first middleware in the web group, which may conflict with existing middleware order (e.g., authentication or CORS) if not planned.fr.app.com) may require additional configuration or middleware tweaks, depending on the application’s existing setup.php artisan route:cache) may need adjustments to regenerate routes for all locales, potentially impacting performance during deployments.{id}) in translated URIs (e.g., fr/{id} vs. en/{id}) must be explicitly defined in routes.php to avoid 404s./fr/teste) must align with SEO best practices (e.g., hreflang tags, canonical URLs), which may require additional middleware or packages.Locale Strategy:
en or 404)?URI Translation Maintenance:
resources/lang/{locale}/routes.php? How will changes to URIs be managed across locales?Performance:
route:cache) be used? How will it handle multilingual routes?Route Naming:
en) be prefixed (e.g., /en/test) or remain unprefixed (/test)? How will this affect URL consistency?Resource Routes:
{photo}) be translated in all locales?Migration Path:
Route::get('/fr', ...)) be migrated to the new syntax (Route::multilingual('test', ...))?Internationalization (i18n) Stack:
laravel-localization, spatie/laravel-translatable)?Error Handling:
routes.php? Will it fall back to the default locale or return a 404?/xx/test) be handled?API Considerations:
api/v1/photos) be handled? Should they also support multilingual URIs, or remain locale-agnostic?Documentation and Training:
localized_route(), current_route(), etc., to ensure consistency?Laravel Core: Ideal for Laravel applications (9.x–13.x) requiring multilingual support without heavy refactoring.
Middleware Integration: Works seamlessly with Laravel’s middleware stack, especially for locale detection (e.g., DetectRequestLocale).
Route Service Provider: Extends Laravel’s RouteServiceProvider to register multilingual routes, maintaining consistency with existing routing logic.
Blade and URL Helpers: Provides localized_route(), current_route(), and current_route_is() helpers that integrate with Blade templates and URL generation.
Testing Support: Compatible with Laravel’s testing tools (e.g., actingAs(), followingRedirects()), though additional assertions may be needed for locale-specific routes.
Non-Fit:
Assessment Phase:
/fr/, /es/ prefixes).routes.php.Configuration Setup:
php artisan vendor:publish --tag=config).config/app.php or via chinleung/laravel-locales.DetectRequestLocale middleware in bootstrap/app.php (must be first in web group).Route Migration:
Route::multilingual().
// Before
Route::get('/test', 'TestController')->name('test');
Route::get('/fr/teste', 'TestController')->name('fr.test');
// After
Route::multilingual('test', 'TestController')->name('test');
Route::multilingualResource().
// Before
Route::resource('/photos', 'PhotoController');
Route::resource('/fr/photos', 'PhotoController');
// After
Route::multilingualResource('photos', 'PhotoController');
routes.php files for URI translations.
// resources/lang/fr/routes.php
return [
'test' => 'teste',
'test/{test}' => 'teste/{test}',
];
localized_route().
// Before
route('test');
// After
localized_route('test');
Middleware Adjustments:
DetectRequestLocale is the first middleware in theHow can I help you explore Laravel packages today?