Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Language Switcher with Sessions and DB

Premium
4 min read

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 users table called language
  • 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:...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

V
Velkacem ✓ Link copied!

I have followed the chapter several times, but I always have a problem

PK
Povilas Korop ✓ Link copied!

What problem exactly? The repository code is available, so maybe you can compare your code?

P
Pēteris ✓ Link copied!

There seems to be an issue with the 'ChangeLanguageController'. The message says: "ChangeLanguageController not invokable: The controller class ChangeLanguageController is not invokable. Did you forget to add the __invoke method or is the controller's method missing in your routes file?" Hm.

M
Modestas ✓ Link copied!

Hm, it seems that I forgot to add a use at the top. Please import the controller using:


use App\Http\Controllers\ChangeLanguageController;

// ... Route

and it will work!

V
Velkacem ✓ Link copied!

It's OK for me thanks everyone

M
muuucho ✓ Link copied!

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

return User::create([
  ...
	'language' => session('locale', config('app.locale')),
])
M
muuucho ✓ Link copied!

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

V
vinco ✓ Link copied!

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?

M
Modestas ✓ Link copied!

You are correct, both are required for a seamless implementation

A3
Asov 3x ✓ Link copied!

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?

M
Modestas ✓ Link copied!

Hi, please dump the $location_detail inside of your controller and check if it was found in the first place.

You can also check fi $slug is set correctly

A3
Asov 3x ✓ Link copied!

hi. 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?

A3
Asov 3x ✓ Link copied!

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