
Hello to all TJD followers,
Today we will learn how to make our laravel application support multi language.
What we need:
- Language controller
- Language middelware
- Language config file
- Route
- 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
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();
}
}
Now let's make our middelware
php artisan make:middleware Language
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);
}
}
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'
],
];
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']);
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
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',
];
in our id folder we have same file and inside it is look like this:
return [
'welcome' => 'Selamat Datang',
];
now to show the line of word in our view we will use:
{{ trans('admindashboard.welcome') }}
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 3 years ago
Be the first to leave a comment.
You must login to leave a comment