barryvdh / laravel-translation-manager

Manage Laravel translation files
MIT License
1.65k stars 419 forks source link

Dynamical route prefix #350

Closed mafftor closed 4 years ago

mafftor commented 4 years ago

Hello there! I need to use dynamical route prefix but I can't use any executive code inside config file.

I use mcamara/laravel-localization and it must be implemented like:

// routes/web.php

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
    Route::get('/', function()
    {
        return View::make('hello');
    });

    Route::get('test',function(){
        return View::make('test');
    });
});

/** OTHER PAGES THAT SHOULD NOT BE LOCALIZED **/

As you know, barryvdh/laravel-translation-manager register its own routes in boot method and gets route's config.

I tried to do something like this: but I don't know where I should use this code. The routes already registered and nothing changes

$route = Config::get('translation-manager.route');
$route['prefix'] = LaravelLocalization::setLocale() . '/' . $route['prefix'];
Config::set('translation-manager.route', $route);

P.S. I have to get a prefix: {lang}/translations {lang} - [en|fr|de]

Thanks in advance!

mafftor commented 4 years ago

Okay, I've solved the problem like:

  1. copied boot method from package and created custom service provider

    class TranslationManagerServiceProvider extends ManagerServiceProvider
    {
    /**
     * Bootstrap the application events.
     *
     * @param Router $router
     * @return void
     */
    public function boot(Router $router)
    {
        $viewPath = __DIR__ . '/../resources/views';
        $this->loadViewsFrom($viewPath, 'translation-manager');
        $this->publishes([
            $viewPath => base_path('resources/views/vendor/translation-manager'),
        ], 'views');
    
        $migrationPath = __DIR__ . '/../database/migrations';
        $this->publishes([
            $migrationPath => base_path('database/migrations'),
        ], 'migrations');
    
        $config = $this->app['config']->get('translation-manager.route', []);
        $config['namespace'] = 'Barryvdh\TranslationManager';
    
        + $config['prefix'] = LaravelLocalization::setLocale() . '/' . $config['prefix'];
    
        $router->group($config, function ($router) {
            $router->get('view/{groupKey?}', 'Controller@getView')->where('groupKey', '.*');
            + $router->get('settings', '\App\Http\Controllers\Admin\TranslationController@settings')->name('translations.settings');
            $router->get('/{groupKey?}', 'Controller@getIndex')->where('groupKey', '.*');
            $router->post('/add/{groupKey}', 'Controller@postAdd')->where('groupKey', '.*');
            $router->post('/edit/{groupKey}', 'Controller@postEdit')->where('groupKey', '.*');
            $router->post('/groups/add', 'Controller@postAddGroup');
            $router->post('/delete/{groupKey}/{translationKey}', 'Controller@postDelete')->where('groupKey', '.*');
            $router->post('/import', 'Controller@postImport');
            $router->post('/find', 'Controller@postFind');
            $router->post('/locales/add', 'Controller@postAddLocale');
            $router->post('/locales/remove', 'Controller@postRemoveLocale');
            $router->post('/publish/{groupKey}', 'Controller@postPublish')->where('groupKey', '.*');
            $router->post('/translate-missing', 'Controller@postTranslateMissing');
        });
    }
    }
  2. Register new ServiceProvider in config/app.php

  3. Don't forget write in composer.json

    "extra": {
        "laravel": {
            "dont-discover": [
                + "barryvdh/laravel-translation-manager"
            ]
        }
    },

Signed + where I added new lines!

Sadly abandoned repo :(