Skip to main content

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? (36 h 00 min)

You also get:

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

Already a member? Login here

Velkacem avatar

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

👍 2
Povilas Korop avatar

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

Pēteris avatar

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.

Modestas avatar

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!

👍 1
Velkacem avatar

It's OK for me thanks everyone

muuucho avatar

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')),
])
👍 2
muuucho avatar

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

vinco avatar

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?

Modestas avatar

You are correct, both are required for a seamless implementation

Asov 3x avatar

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?

Modestas avatar

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

Asov 3x avatar

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?

Asov 3x avatar

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

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.