Support the ongoing development of Laravel.io →
Configuration
Last updated 3 months ago.
0

this may not be the complete solution but you can do something like this:

class HomeController extends BaseController {

	public function postChangeLanguage() 
	{
		$rules = [
		'language' => 'in:en,fr' //list of supported languages of your application.
		];
		
		$language = Input::get('lang'); //lang is name of form select field.
		
		$validator = Validator::make(compact($language),$rules);
			
		if($validator->passes())
		{
                        Session::put('language',$language);
			App::setLocale($language);
		}
		else
		{/**/ }
	}
}

and you can create a form like this:

{{ Form::open(['action' => '[email protected]']) }}
	{{Form::select('lang',['en'=>'en','fr'=>'fr'],$lang,['onchange'=>'submit()'])}}
{{ Form::close()}}

and in your App before filter

App::before(function($request)
{
   $language = Session::get('language','en'); //en will be the default language.
   App::setLocale($language);
});
Last updated 3 months ago.
0
App::setlocale($language);

This is only set for the current request - you will need to save the setting in a session and assign every page load (for example in global.php)

Last updated 3 months ago.
0

I was just editing my reply @martinhearn :)

Last updated 3 months ago.
0

Hallo @usm4n, thank you, i will try it...
Hallo @martinhearn, i muss put button select language, thank you

Last updated 3 months ago.
0

:) @usm4n

although perhaps better to retrieve the default language from the config rather than hardcode:

$language = Session::get('language',Config::get('app.locale'));
Last updated 3 months ago.
0

@martinhearn yes sure, you are right :)

Last updated 3 months ago.
0

Thanks man, its working perfect.

Last updated 3 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

wiyono wiyono wiyono Joined 3 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.