PHP code example of kingscode / laravel-localize

1. Go to this page and download the library: Download kingscode/laravel-localize library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kingscode / laravel-localize example snippets




namespace App\Http;

class Kernel extends HttpKernel
{
    ...

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ...
            LocaleSelector::class,     // <<<<<< Here it is.
            SubstituteBindings::class, // Its best to register before substitute bindings.
            ...
        ],
    ];
    
    ...
}



use Illuminate\Contracts\Config\Repository;
use Illuminate\Routing\Router;
use KingsCode\LaravelLocalize\Localize;

class RouteServiceProvider extends ServiceProvider
{
    public function map()
    {
        // Web routes without localization, like a "/file/{file}" route or such.
        $this->mapWebRoutes();
        
        // And as the last method call you'll do the localized web routes.
        $this->mapLocalizedWebRoutes();
    }
    
    /**
     * Define the localized "web" routes for the application.
     *
     * @return void
     */
    protected function mapLocalizedWebRoutes()
    {
        // We'll take stuff from the config to keep things easily configurable.
        // Not a must, but it prevents you from having to override stuff.
        $config = $this->app->make(Repository::class);
        
        // We'll need a router to register routes duh.
        $router = $this->app->make(Router::class);
        
        /** @var Localize $localize */
        $localize = $this->app->make(Localize::class);
        
        // Okay so here is an IMPORTANT part.
        // Register the {locale} routes first otherwise {locale}/{any} will not be reachable and {any} will catch everything.
        $router->middleware('web')
            ->namespace($this->namespace)
            ->prefix('{' . $config->get('localize.route_parameter_key') . '}') // We add the prefix.
            ->where([$config->get('localize.route_parameter_key') => $localize->getRouteRegex()])
            ->name($config->get('localize.route_name_prefix') . '.') // And the name prefix.
            ->group(base_path('routes/localized.web.php'));
        
        $router->middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/localized.web.php'));
    }
}
sh
php artisan vendor:publish --provider="KingsCode\LaravelLocalize\LocalizeServiceProvider"