Wep.php
Route::get('language/lang', [Localization::class, "lang_change"])->name('LangChange');
controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
class Localization extends Controller
{
public function lang_change(Request $request)
{
App::setLocale($request->lang);
session()->put('locale', $request->lang);
return view('profiles');
}
}
view file
<div >
<select >
<option value="ko" {{ session()->get('locale') == 'ko' ? 'selected' : '' }}>korea</option>
<option value="hi" {{ session()->get('locale') == 'hi' ? 'selected' : '' }}>urdu</option>
</select>
</div>
<a href="#">{{__('profiles.Home')}}</a>
<a href="#">{{__('profiles.Contact')}}</a>
<a href="#">{{__('profiles.About')}}</a>
</div>
<script type="text/javascript">
var url = "{{ route('LangChange') }}";
$(".Langchange").language(function(){
window.location.href = url "?lang=" $(this).val();
});
</script>
I'm trying to change the language but nothing happens. And I did create files for different language also just mention where is problem.
CodePudding user response:
I would suggest you to check this out: mcamara/laravel-localization.
There is just simple middleware that solves all your problems:
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
Route::prefix(LaravelLocalization::setLocale())->group(function() {
// your routes here
})
CodePudding user response:
1. you have to collect the route
Route::get('language/{lang}', [Localization::class, "lang_change"])->name('LangChange');
2. set your lang_change parameter property
public function lang_change($lang)
{
App::setLocale($lang);
session()->put('locale', $lang);
return redirect()->back();
}
3. Then create a middleware LocalizationMiddleware to handle all the session and add codes in class handle method
if(Session::get('locale') != null){
App::setLocale(Session::get('locale'));
} else {
Session::put('locale','en');
App::setLocale(Session::get('locale'));
}