DEV Community

loading...

Easy Locale Routing with Laravel-Localization

dendihandian profile image Dendi Handian ・2 min read

Localization (l10n) and Internationalization (i18n) are essential if your website want to be stand-out globally and attract more users to come and stay longer on your website. But localizing a website could be not as simple as only translating the text on the web. There are some considerations like filtering the accepted languages/locale, redirecting existing URLs to use currently used locale, and more.

We could build it from the ground up in Laravel, but let's make it easier to implement by using laravel-localization package.

Prerequisites To Code Along

Prepare your own existing or new ready-to-be-developed laravel app.

Installation and Setup

Install it using composer:

composer require mcamara/laravel-localization
Enter fullscreen mode Exit fullscreen mode

You may want to generate the respective config for this package by this command:

php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
Enter fullscreen mode Exit fullscreen mode

And then register the package's middleware to app\Http\Kernel.php:

protected $routeMiddleware = [

    ...

    /**** OTHER MIDDLEWARE ****/
    'localize'                => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
    'localizationRedirect'    => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
    'localeSessionRedirect'   => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
    'localeCookieRedirect'    => \Mcamara\LaravelLocalization\Middleware\LocaleCookieRedirect::class,
    'localeViewPath'          => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class

    ...

];
Enter fullscreen mode Exit fullscreen mode

Grouping the Routes for Localization

Now let's say we have these existing route in the app, specifically in routes\web.php file:

Auth::routes();

Route::prefix('products')->name('products.')->middleware('auth')->group(function () {
    Route::get('/', [ProductController::class, 'index'])->name('index');
    Route::post('/', [ProductController::class, 'store'])->name('store');
    Route::get('/create', [ProductController::class, 'create'])->name('create');

    Route::prefix('{product}')->middleware('product_owner')->group(function () {
        Route::get('/', [ProductController::class, 'show'])->name('show');
        Route::patch('/', [ProductController::class, 'update'])->name('update');
        Route::delete('/', [ProductController::class, 'delete'])->name('delete');
        Route::get('/edit', [ProductController::class, 'edit'])->name('edit');
    });
});
Enter fullscreen mode Exit fullscreen mode

We just need to wrap it into a new route group, add the prefix and the middleware like this:

Route::prefix(LaravelLocalization::setLocale())->middleware(['localeSessionRedirect', 'localizationRedirect'])->group(function () {
    Auth::routes();

    Route::prefix('products')->name('products.')->middleware('auth')->group(function () {
        Route::get('/', [ProductController::class, 'index'])->name('index');
        Route::post('/', [ProductController::class, 'store'])->name('store');
        Route::get('/create', [ProductController::class, 'create'])->name('create');

        Route::prefix('{product}')->middleware('product_owner')->group(function () {
            Route::get('/', [ProductController::class, 'show'])->name('show');
            Route::patch('/', [ProductController::class, 'update'])->name('update');
            Route::delete('/', [ProductController::class, 'delete'])->name('delete');
            Route::get('/edit', [ProductController::class, 'edit'])->name('edit');
        });
    });
});
Enter fullscreen mode Exit fullscreen mode

And that's it. To make it work, just access any defined route URL with a locale prefix, like http://your-app.test/en/products and magically all routes on your app will be redirected with a locale prefixed. We use the localeSessionRedirect and localizationRedirect middleware to redirect any route to have locale prefixed as recommended by the package author in the package page.

Now you can start translating and explore this package more, have fun.


version used:

Laravel Framework 8.12.3
mcamara/laravel-localization 1.6.1
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide