Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Translation Route Bundle Laravel Package

dovc/translation-route-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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"
    
  2. Basic Configuration Edit config/translation-route.php:

    'locales' => ['en', 'fr', 'de'],
    'default_locale' => 'en',
    'prefix' => 'lang',
    'route_middleware' => ['web'],
    
  3. 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
    

Implementation Patterns

Route Integration

  • 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'],
    

View Integration

  • 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();
    }
    

API Usage

  • Locale in API Responses Attach locale to API responses:
    return response()->json([
        'data' => $data,
        'locale' => TranslationRoute::getLocale()
    ]);
    

Gotchas and Tips

Common Pitfalls

  • 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.

Debugging

  • 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
    

Extension Points

  • 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']);
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui