Blog Detail

25

Nov
Automatically Detect & Set App Locale with Laravel Localizer cover image

arrow_back Automatically Detect & Set App Locale with Laravel Localizer

Laravel Localizer is an awesome package that allows you to automatically detect and set an app locale that matches your visitor’s preference.

  • Define your supported locales and match your visitor’s preference
  • Uses the most common locale detectors by default
  • Uses the most common locale stores by default
  • Easily create and add your own detectors and stores

Requirements

  • PHP >= 7.1
  • Laravel >= 5.6

Installation

You can install this package via composer by running this command.

composer require codezero/laravel-localizer

Laravel will automatically register the ServiceProvider.

Add Middleware

Add the middleware to the web middleware group in app/Http/Kernel.php, after StartSession and before SubstituteBindings:

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Session\Middleware\StartSession::class, // <= after this
        //...
        \CodeZero\Localizer\Middleware\SetLocale::class,
        //...
        \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
    ],
];

In Laravel 6.x you also need to add the middleware to the $middlewarePriority array in app/Http/Kernel.php to trigger it in the correct order:

protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class, // <= after this
    //...
    \CodeZero\Localizer\Middleware\SetLocale::class,
    //...
    \Illuminate\Routing\Middleware\SubstituteBindings::class, // <= before this
];

Publish Configuration File

php artisan vendor:publish --provider="CodeZero\Localizer\LocalizerServiceProvider" --tag="config"

You will now find a localizer.php file in the config folder.

Configure Supported Locales

Add any locales you wish to support to your published config/localizer.php file:

'supported-locales' => ['en', 'nl', 'fr'];

Drivers

Detectors

By default, the middleware will use the following detectors to check for a supported locale:

  1. The URL slug
  2. The session
  3. A cookie
  4. The browser
  5. The app’s default locale

If you publish the configuration file, you can choose which detectors to run and in what order.

You can also create your own detector by implementing the \CodeZero\Localizer\Detectors\Detector interface and add a reference to it in the config file. The detectors are resolved from Laravel’s IOC container so that you can add any dependencies to your constructor.

Stores

The first supported locale that is returned by a detector will then be stored in:

  1. The session
  2. A cookie
  3. The app locale

If you publish the configuration file, you can choose which stores to use.

You can also create your own store by implementing the \CodeZero\Localizer\Stores\Store interface and add a reference to it in the config file. The stores are resolved from Laravel’s IOC container, so you can add any dependencies to your constructor.

For more details, please visit Github.

Published at : 25-11-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project