Painless laravel multi language application

Painless laravel multi language application

220

Hello to all TJD followers,

Today we will learn how to make our laravel application support multi language.

What we need:

  1. Language controller
  2. Language middelware
  3. Language config file
  4. Route
  5. Language switcher

Let's begin:

First of all make LanguageController we will use terminal provided by laragon (read tutorial here)

php artisan make:controller LanguageController
Markup

Now copy codes below to your LanguageController



namespace AppHttpControllers;

use IlluminateHttpRequest;
use Config;
use IlluminateSupportFacadesRedirect;
use IlluminateSupportFacadesSession;

class LanguageController extends Controller
{
    public function index($lang)
    {
      if (array_key_exists($lang, Config::get('languages'))) {
            Session::put('applocale', $lang);
        }
        return Redirect::back();
    }
}
PHP

 

Now let's make our middelware

php artisan make:middleware Language
Markup

Now copy codes below to your Language middelware and save it



namespace AppHttpMiddleware;

use Closure;
use IlluminateFoundationApplication;
use IlluminateHttpRequest;
use IlluminateRoutingRedirector;
use IlluminateSupportFacadesApp;
use IlluminateSupportFacadesConfig;
use IlluminateSupportFacadesSession;

class Language
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
      if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
          App::setLocale(Session::get('applocale'));
      }
      else { // This is optional as Laravel will automatically set the fallback language if there is none specified
          App::setLocale(Config::get('app.fallback_locale'));
      }
      return $next($request);
    }
}
PHP

 

As our third step open your config folder and create new file name it Languages.php , then copy codes below and save it.



return [
  'en' => [
      'name' => 'English',
      'flag' => 'images/flags/en.png'
  ],
  'id' => [
      'name' => 'Indonesian',
      'flag' => 'images/flags/indonesia.png'
  ],
  'fa' => [
      'name' => 'پارسی',
      'flag' => 'images/flags/iran.png'
  ],
];
PHP

note: here we provided 3 languages for our app you can change it, add into it, or make it less, all are optional depend on your needs.

 

Now in  step 4 we will making our route for languages, open your web.php file and all line below:

Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LanguageController@index']);
PHP

 

Step 5 is to making dropdown in our view so users can select their preferred language:

note: personally I add it in my navbar but you are free to use it wherever it suits you.


  
     {{ Config::get('languages')[App::getLocale()]['name'] }} 
  
    @foreach (Config::get('languages') as $lang => $language)
    @if ($lang != App::getLocale())
          {{ $language['name'] }}
       
    @endif
    @endforeach
   
Markup

 

Last thing you need to do is to add your language files in resources -> lang -> language name we made in our config file such as (en , fa , id ) and add your words,

 

Here is an example:

in en folder we have file named admindashboard.php and inside it is look like this:



  return [
    'welcome' => 'Welcome',
  ];
PHP

in our id folder we have same file and inside it is look like this:



  return [
    'welcome' => 'Selamat Datang',
  ];
PHP

 

now to show the line of word in our view we will use:

{{ trans('admindashboard.welcome') }}
PHP

first value is name of our file admindashboard and second value is line of translation welcome .

 

 

PS: in this tutorial I pulled the trigger and showed a trick that normally language packages will not provide and that's showing languages flag image, it took me a while till I understand how to show languages images and now you have it painless and free.

- Last updated 4 years ago

Be the first to leave a comment.

You must login to leave a comment