Laravel Localization

Are you working on a laravel project where you need to work with more than one language and you want your laravel urls to be dynamic enough to translate the contents depending on language supplied in the url.

Let's have a look at following awesome laravel vendor package that gives you following advantages:

  • Support Localization
  • Supports Modularization

Let's install following package and get started with laravel localization feature:

Laravel Modular App

Plugin Requirements

  • PHP >= 7.2
  • Laravel >= 6.0

Installation

Go to your root directory of your laravel project and run following command:

# install this plugin
composer require "learn2torials/laravel-modular"

Next, we need to create a new configuration file called console.php in config directory with following contents:

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "prefix"   => null,
    "i18n"     => false,
    "https"    => false,
    "modules"  => []
];

Now, we need to turn on the i18n feature so that we can play with our language functionality. Enable i18n to true.

<?php

/*
|--------------------------------------------------------------------------
| Configuration File
|--------------------------------------------------------------------------
|
| You can overwrite default configuration here according to your app requirements.
|
*/
return [
    "prefix"   => null,
    "i18n"     => true,
    "https"    => false,
    "modules"  => []
];

Now, that we have turned on localization feature we need to apply this change to our existing routes just to make sure all routes follow plugin directions to detect locale.

Open following file: app/Providers/RouteServiceProvider.php and make following changes:

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::prefix(langPrefix())
             ->middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix(langPrefix('api'))
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
}

We made a small change in above file we added following lines to existing RouteServiceProvider.php file. Notice following changes to above file.

# enable localization for web routes
prefix(langPrefix())

# enabled localization for api routes
prefix(langPrefix('api'))

Above function will make sure that when no language is added in url it will fall back to default laravel route. Let's run our laravel app and see how this changes work.

Run laravel php server using following command:

php artisan serve

Now, try using following urls in your web browser:

http://localhost:8000            -> default en translation
http://localhost:8000/en/ca      -> for english translation
http://localhost:8000/fr/ca      -> for french translation

Make sure you have your translation placed in following directories:

  • resources/lang/en
  • resources/lang/fr

Once you have your translation files with keys you want to translate. In your view file use following function to make sure it will translate your content based on locale found in the url typed in a browser.

# use following method in blade file
{{ __('YOUR TRANSLATION KEY') }}

# or you can use following method
@lang('YOUR TRANSLATION KEY')

That is it I hope you liked this tutorial. Make sure to read more about this plugin as it also offer some other benefits like:

  • translation support
  • modular app support
  • prefix for all routes