How to fetch current locale in view in Laravel 5.3
At first create a locale route and controller :
Route::get('/locale/{lang}', 'LocaleController@setLocale')->name('locale');
class LocaleController extends Controller
{
public function setLocale($locale)
{
if (array_key_exists($locale, Config::get('languages')))
{
Session::put('app_locale', $locale);
}
return redirect()->back();
}
}
Now you can check easily in every page:
$locale = app()->getLocale();
$version = $locale == 'en' ? $locale . 'English' : 'Bangla';
try this. It will give the locale set in your application
Config::get('app.locale')
Edit:
To use this in blade, use like the following, to echo your current locale in blade.
{{ Config::get('app.locale') }}
If you want to do a if condition in blade around it, it will become,
@if ( Config::get('app.locale') == 'en')
{{ 'Current Language is English' }}
@elseif ( Config::get('app.locale') == 'ru' )
{{ 'Current Language is Russian' }}
@endif
To get current locale,
app()->getLocale()