PHP code example of justijndepover / laravel-localized-routes

1. Go to this page and download the library: Download justijndepover/laravel-localized-routes 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/ */

    

justijndepover / laravel-localized-routes example snippets


return [

    /**
     * This global setting can enable / disable the entire localization package.
     */
    'enable_localized_routes' => true,

    /**
     * This list contains all the available locales.
     * Simply add your own locale and thats it!
     */
    'locales' => [
        'en' => 'English',
        'nl' => 'Nederlands',
    ],

    /**
     * Automatically detect locales
     *
     * With this setting enabled, you can automatically detect locales.
     * The middleware to do so will check the request for a "locale" header
     *
     * useful for api's, where you don't want the locale prefix,
     * but still want to set the application locale
     */
    'auto_detect_locales' => true,

    /**
     * Automatically redirect requests if the localized version exists
     *
     * With this setting enabled, your requests will automatically redirect
     * to their localized counterpart.
     *
     * For example: /home => /en/home
     */
    'auto_redirect_to_localized_route' => true,

];

Route::localized(function () {
    // Every route in here is localized
});

Route::localized(function () {
    Route::get('home', HomeController::class)->name('home');
});

switchLanguage('fr'); // will return the current route, but the localized part is substituted with the new language
sh
php artisan vendor:publish --tag="laravel-localized-routes-config"
blade.php
<!-- in your blade view -->
<a href="{{ route('home') }}">Home</a>
<!-- will return /{locale}/home -->