John Davidson

php - How do we change language in a Laravel 8 localization?

0 comments
Message:


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 class="col-md-4">
<select class="form-control Langchange">
<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.



My Answer:
The issue in your code is in the JavaScript part where you are trying to change the language. The correct syntax for the `change` event in jQuery is `change()` not `language()`.

Here is the corrected JavaScript code:

javascript



With this change, when the user selects a different language from the dropdown, the `change` event will trigger and redirect the user to the `LangChange` route with the selected language as a query parameter.

Make sure to also check if the route `LangChange` is correctly defined in your `web.php` file and that the `Localization` controller is properly imported.

Rate this post

4 of 5 based on 8098 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us