Search code examples
phpmodulelaravel-5.5laravel-routing

LaravelLocalization failing to translate routes of nwidart laravel modules


I can't figure out why LaravelLocalization cannot translate the routes which I'm declaring in routes.php (Modules/ModuleName/Http/routes.php) file of a module which I create with nwidart laravel-modules package for laravel, 'localize' (\Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class) middleware is also present in the app's Kernel.php and in the route group as stated in here. The route is not translated and appears as /en/booking::all.companies.index instead of /en/companies (or /ru/kompanii):

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => ['web', 'auth', 'localize', 'optimizeImages'],
    'namespace' => 'Modules\Booking\Http\Controllers',
],
function() {
    Route::get(LaravelLocalization::transRoute('booking::all.companies.index'), 'CompanyController@index')->name('booking.companies.index');
});

but when module namespace prefix "booking::" is removed from translation string (LaravelLocalization::transRoute('all.companies.index') instead of LaravelLocalization::transRoute('booking::all.companies.index')) it CAN translate the route.

Please help me to resolve the issue, thanks.

(My installation if it helps: Laravel Framework 5.5.43, "mcamara/laravel-localization": "1.3", "nwidart/laravel-modules": "2.7". No other localization package is installed besides mcamara/laravel-localization)


Solution

  • I had the same problem and I didn't find the solution online. Here's how I solved the problem:

    In Modules\[MyModule]\Providers\RouteServiceProvider:

    /**
     * Register translations.
     *
     * @return void
     */
    protected function registerTranslations()
    {
        $module_path = 'MyModule';
        $module_slug = 'my-module';
    
        $langPath = resource_path('lang/modules/'.$module_slug);
    
        if (is_dir($langPath)) {
            $this->loadTranslationsFrom($langPath, $module_slug);
        } else {
            $this->loadTranslationsFrom(module_path(module_path, 'Resources/lang'),$module_slug);
        }
    }
    
    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->registerTranslations();
        $this->mapApiRoutes();
        $this->mapWebRoutes();
    }