dovc/translation-route-bundle
Installation
composer require dovc/translation-route-bundle
Add to config/app.php under providers:
Dovc\TranslationRouteBundle\TranslationRouteServiceProvider::class,
Publish the config:
php artisan vendor:publish --provider="Dovc\TranslationRouteBundle\TranslationRouteServiceProvider" --tag="config"
Basic Configuration
Edit config/translation-route.php:
'locales' => ['en', 'fr', 'de'],
'default_locale' => 'en',
'prefix' => 'lang',
'route_middleware' => ['web'],
First Use Case
Add a route in routes/web.php:
Route::get('/hello', [HelloController::class, 'index'])->name('hello');
Access translated routes via:
/lang/fr/hello
Dynamic Locale Handling
Use TranslationRoute::getLocale() in controllers to fetch the current locale:
public function index()
{
$locale = TranslationRoute::getLocale();
return "Hello in {$locale}";
}
Middleware Integration Apply middleware to locale routes:
Route::group(['middleware' => ['auth', 'locale']], function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
});
Fallback Logic Implement fallback locales in config:
'fallback_locales' => ['fr' => 'en'],
Locale-Specific Assets Use Blade directives to load locale-aware assets:
@lang('en') <link href="{{ asset('css/en.css') }}" rel="stylesheet"> @endlang
@lang('fr') <link href="{{ asset('css/fr.css') }}" rel="stylesheet"> @endlang
Dynamic Language Switcher
Create a helper in app/Helpers/LocaleHelper.php:
use Dovc\TranslationRouteBundle\Facades\TranslationRoute;
function getCurrentLocale()
{
return TranslationRoute::getLocale();
}
return response()->json([
'data' => $data,
'locale' => TranslationRoute::getLocale()
]);
Route Caching Conflicts Clear route cache after adding locale routes:
php artisan route:clear
Locale Validation
Ensure locale codes match those in config/translation-route.php to avoid 404s.
Middleware Order
Place locale middleware after web but before auth middleware to avoid redirect loops.
Check Locale Resolution Use Tinker to debug:
php artisan tinker
>>> \Dovc\TranslationRouteBundle\Facades\TranslationRoute::getLocale();
Route Debugging List all locale routes:
php artisan route:list | grep lang
Custom Locale Detection
Override LocaleDetector in app/Providers/TranslationRouteServiceProvider.php:
$this->app->bind('locale.detector', function () {
return new CustomLocaleDetector();
});
Locale-Specific Middleware Extend the bundle’s middleware:
namespace App\Http\Middleware;
use Dovc\TranslationRouteBundle\Http\Middleware\LocaleMiddleware;
class CustomLocaleMiddleware extends LocaleMiddleware
{
protected $customLogic = true;
}
Fallback Logic Extend fallback locales dynamically:
TranslationRoute::extend('fallback_locales', function ($locales) {
return array_merge($locales, ['es' => 'en']);
});
How can I help you explore Laravel packages today?