27

I am making a bilingual app. I am using same routes for each but I am using different views for both languages. Whenever I want to redirect to a route I want to pass {{ route('test.route', 'en') }}. Where I am passing en, I want to fetch the current locale value from the view and then pass it to the route. Please help.

Fahad Khan
  • 335
  • 1
  • 4
  • 14
  • You will have to get the get a value for 'en' (why not consider a session variable) then you can do something like: `code` {{ route('test.route', ['route'=>$variable_holding_route_value]) }} – Jim Jan 06 '17 at 12:51

2 Answers2

65

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()
Arun Code
  • 1,548
  • 1
  • 13
  • 18
  • in the blade? Can I use it like `@if (Config::get('app.locale'))`? – Fahad Khan Jan 06 '17 at 13:05
  • In your blade template you can use it like this `{{ Config::get('app.locale') }}` – Arun Code Jan 06 '17 at 13:27
  • @FahadIqbalAhmadKhan `app.locale` will always be set to *something* so an `@if` won't be very useful. It'll always be true. – ceejayoz Jan 06 '17 at 13:43
  • 2
    This will fetch config locale which could be different from current locale. To fetch current locale you should use `App::getLocale()` or `App::isLocale('...')`. [Localization](https://laravel.com/docs/5.6/localization#introduction). You can also use `app()->getLocale()` which in Blade would be `{{ app()->getLocale() }}`. – Marco Florian May 09 '18 at 00:21
5

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';
rashedcs
  • 3,588
  • 2
  • 39
  • 40