Another example of how we can add a language selector to our application is by using sessions and a DB table to store the user's preferred language.
This example will not use a URL to determine the language which will allow us to use the same URL for all languages.

Setup
Our setup process for Database stored language selection will touch these things:
- Configuration: Adding a language list to our configuration - this will be used to display the language selector
-
DB Structure: Creating a new field on the
userstable calledlanguage - Controller: Create a controller that will handle the language change
- Middleware: Add a middleware to handle language settings based on user's preferences
- Views: Adding a language selector to our views
- Routes: Adding a route that will allow us to change the language
Let's start with the configuration:...
I have followed the chapter several times, but I always have a problem
What problem exactly? The repository code is available, so maybe you can compare your code?
There seems to be an issue with the 'ChangeLanguageController'. The message says: "ChangeLanguageController not invokable: The controller class
ChangeLanguageControlleris not invokable. Did you forget to add the__invokemethod or is the controller's method missing in your routes file?" Hm.Hm, it seems that I forgot to add a use at the top. Please import the controller using:
and it will work!
It's OK for me thanks everyone
If you have installed Jetstream and Livewire recently and like to do the last step inte this tutorial, "Transforming Guest Language to a User Language Preference", the Controller is in another place. Open app/Action/Fortify/CreateNewUser.php add the line "'language' => session('locale', config('app.locale'))," to
And, If you have livewire installed, the middleware in this torurial doesn't affect the fortify nor the livewire routes. They need to be ignored in vendor and then all teh routes copied to your web.php. I follwed successfully Jeremy Belolo's updated (from Sep 24 2020) solution here: https://stackoverflow.com/questions/64016900/laravel-localization-and-routes-from-jetstream-fortify
Correct me if I'm wrong but it seems we would need both options (URL & Sessions) to make it bulletproof.
URL is necessary for SEO and when not logged in. Session is necessary so that a logged in user doesn't have to switch his language.
Or am I missing something?
You are correct, both are required for a seamless implementation
hellooo. i just start to learn multi language. but i have a problem
please see my route below:
Route::prefix('{locale}') ->where(['locale' => '[a-zA-Z]{2}']) ->middleware('setlocale') ->group(function () { Route::get('location/{slug}', [LocationController::class, 'show'])->name('locations.show'); });
and also this is my controller:
public function show(string $slug) { $location_detail = Location::where('status', '1')->where('slug', $slug)->first(); return view('front.location-detail')->with(compact('location_detail')); }
when i tried to access this url: http://localhost:8000/en/location everything is working fine. but when i access this url: http://localhost:8000/en/location/jungle the data is not showing. they said {{ $location_detail->title }} is null
can somebody please help me with this?
Hi, please dump the
$location_detailinside of your controller and check if it was found in the first place.You can also check fi
$slugis set correctlyhi. the data in database is there. maybe the problem is here: $location_detail = Location::where('status', '1')->where('slug', $slug)->first()
do i need to setup locale to call the data from database?
the problem is when calling the data from database. it should be like this:
$location_detail = Location::where('status', '1')->where('slug->' . $locale, $slug)->first();
all fixed now