Easy Locale Routing with Laravel-Localization
Tags: laravel, php, l10n, i18n
Published: 2020-11-28 16:37:40

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](https://github.com/mcamara/laravel-localization#redirect-middleware) 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
```

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

```
php artisan vendor:publish --provider="Mcamara\LaravelLocalization\LaravelLocalizationServiceProvider"
```

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

```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
    
    ...
    
];
```

## Grouping the Routes for Localization

Now let's say we have these existing route in the app, specifically in `routes\web.php` file:
```php
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');
    });
});
```

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

```php
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');
        });
    });
});
```

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](https://github.com/mcamara/laravel-localization#recommendations).

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
```