larasofthu/localized-routes-plus
Installation:
composer require larasofthu/localized-routes-plus
Publish the config:
php artisan vendor:publish --provider="LarasoftHU\LocalizedRoutesPlus\LocalizedRoutesServiceProvider" --tag="config"
Configure Locales:
Edit config/localized-routes-plus.php to define supported locales (e.g., ['en', 'hu']) and their routing strategies (prefix/subdomain).
First Use Case:
Define a localized route in routes/web.php:
Route::localized('home', function () {
return view('welcome');
})->middleware('web');
This generates routes for all configured locales (e.g., /en/home, /hu/home or en.example.com/home).
config/localized-routes-plus.php (defines locales, strategies, and defaults).localized() to Laravel’s Route facade for fluent syntax.App middleware to handle locale detection (e.g., from subdomains or URL prefixes).Locale-Agnostic Route Definitions:
Use Route::localized() to define routes once, letting the package generate locale-specific variants:
Route::localized('products/{category}', 'ProductController@index')
->name('products.index');
Generates:
/en/products/electronics (prefix strategy)hu.example.com/products/electronics (subdomain strategy)Dynamic Locale Detection: Leverage middleware to auto-detect locales from:
subdomain_locales in the config (e.g., ['en' => 'en', 'hu' => 'hu']).prefix_locales (e.g., ['en' => 'en', 'hu' => 'hu']).?locale=hu if other methods fail.Country-Specific Routing:
Extend locales with country codes (e.g., en-us, hu-hu) in the config:
'locales' => [
'en-us', 'en-ca', 'hu-hu'
],
Routes like /en-us/home are auto-generated.
Middleware Integration:
Use the package’s middleware (Localize) in your app/Http/Kernel.php:
protected $middlewareGroups = [
'web' => [
// ...
\LarasoftHU\LocalizedRoutesPlus\Http\Middleware\Localize::class,
],
];
This sets the app()->currentLocale dynamically based on the request.
Locale-Specific Controllers:
Override controller logic per locale by checking app()->getLocale():
public function index()
{
if (app()->getLocale() === 'hu') {
return view('hu.welcome');
}
return view('welcome');
}
Fallback Locales:
Configure a fallback locale in the config (e.g., default_locale = 'en') to handle unsupported locales gracefully.
Custom Route Generation:
Extend the package’s route generator by publishing and modifying the RouteGenerator class:
php artisan vendor:publish --provider="LarasoftHU\LocalizedRoutesPlus\LocalizedRoutesServiceProvider" --tag="routes"
API Localization:
Use the same localized() macro for API routes in routes/api.php:
Route::localized('api/v1/users', 'UserController@index');
Ensures consistency between web and API routes.
Locale Detection Order:
The package checks subdomains → URL prefixes → query params → fallback. Override this in config/localized-routes-plus.php with locale_detection_order.
Subdomain Conflicts:
Ensure your DNS supports subdomain routing (e.g., *.example.com). Wildcard subdomains may require server config (e.g., Nginx/Apache).
Route Caching: Clear route cache after changing locales or strategies:
php artisan route:clear
Middleware Collisions:
If using other localization packages (e.g., laravel-localization), disable their middleware to avoid conflicts.
Check Current Locale: Add this to a route to debug:
Route::get('/debug-locale', function () {
return "Current locale: " . app()->getLocale();
});
Route Listing:
Use php artisan route:list to verify generated routes. Filter by locale-specific paths (e.g., grep "hu/").
Log Locale Detection:
Temporarily add logging to the Localize middleware:
public function handle($request, Closure $next)
{
\Log::info('Detected locale:', ['locale' => $request->locale]);
return $next($request);
}
Custom Locale Providers:
Implement LarasoftHU\LocalizedRoutesPlus\Contracts\LocaleProvider to add new detection logic (e.g., cookies or headers).
Route Naming:
Override the default naming convention by extending the RouteGenerator:
public function generateLocalizedRouteName($name, $locale)
{
return "{$locale}.{$name}";
}
Locale-Specific Assets:
Use the localized-asset helper (if included) or middleware to serve locale-specific JS/CSS:
// In a blade template
<link href="{{ localized_asset('css/app.hu.css') }}" rel="stylesheet">
Testing: Mock locale detection in tests:
$request = new Request(['locale' => 'hu']);
$response = $this->actingAs($user)->withHeaders(['X-Locale' => 'hu'])->get('/');
How can I help you explore Laravel packages today?