This episode is for members only

Sign up to access "Translations with Laravel and Inertia" right now.

Get started
Already a member? Sign in to continue
05. Storing the chosen language in a session


There are two steps now to setting the actual language within our application. And that is the first one, when we change this, we want to go ahead and send a
request to our backend to set the value that we've just selected within this dropdown, within a session. And then we're going to need some middleware that makes sure that every time we have a request to our application, the correct locale or
language is set globally for our app. So in this episode, we're going to work on sending a request through to the backend, when we choose one of these options, and then we're going to work on that middleware, which will set this.
And we're going to test both of these things as well. So the first thing that we're going to do, we'll write the test afterwards. We're going to make out a controller here. And we're going to call this language store controller.
You can call this whatever you want. So let's go over to our web routes and create this out. We'll just do this under our dashboard here. So we're going to make a post request through to slash language.
We're going to reference that language store controller. And let's give this a name as well. So we can reference it over on the client and we'll just call that language dot store.
Okay. So what we want to happen here, if we just create out an invoke magic method in here and die dump store. When we change one of the options over here, we want that to make a request
through to this end point just here. So how do we do this? Well, over in our authenticated layout, we've got our selected. It's not a form and we don't really want a form because we don't want the users
have to manually click a submit button. What we do want to do is create a V on change in here. And we want to send a request using inertia's router through to our backend to make a request here.
So for this, we need to go ahead and make sure that our inertia router is imported. So the only change we need to add here is also import router from inertia JS view three. So let's go down to our language selector here. And we're going to say router host, of course, through to that language end point.
Now we gave this a name earlier and using Ziggy, we can just use the route helper and say language store. So if you ever change the name of that end point, of course, that's still going to work. Now this isn't good enough because of course we need to actually send
the language that we have chosen down. So as the second argument to this, this is the data that we want to send down with this. So if we go ahead and choose the language in here, where is this coming from? Well, it comes from the event and from this, we can go into the target.
The target is the select itself. Then we can grab the value of the select. So when we change this from English to German, this will be DE because the thing that we are, have this event on has a value of DE and the same with EN and
any other languages that you choose. So this should now work. Let's go ahead and select German. And sure enough, we get died up to store.
We can just improve our kind of manual testing here by grabbing the actual value that we take down from the request. So we can go ahead and say request and language, which we called here. And let's go ahead and change this to German.
And then we go, we get DE. Let's change this back to English and we get EN. Great. So we've got both of them values.
We know that that is working. So what do we want to do inside of this controller? Well, the first thing I'm going to do is just return back. That's going to probably handle our inertia request here, but in here,
we just want to set a session value. So for this, we're going to say session and put, and we're going to add this into a key. I'm just going to call this language.
And here we want to grab that value. Now we don't really want to go ahead and say request language, because technically this value could be anything. We could go ahead and validate this and you can validate that
something exists within enum. But what I'm going to do here is I'm going to kind of bypass that and I'm going to use our enum and I'm going to say try from, so we already looked at the from method within enum, try from will return a null value if this doesn't exist.
So we can grab that language from there. Let's say language. And that's going to be EN DE or something that doesn't exist. If it doesn't exist, then it's not going to give us back a value.
It's going to give us back a null value. So what we can do in here is we can say, well, if we try this and we don't end up with a value that we want, we can just go ahead and say config app locale, and let's just make sure we add in a null safe here as well, just so if this doesn't work and
we get a null back, then value just doesn't exist. So we'll add tests to cover this. But basically now what's happening is we are making sure that the language that we get set actually comes from this enum.
Like I said, you can use validation, but I find it a little bit easier to do this all in one line and then just default really nicely to the default locale or the locale that's currently selected. So if we just go ahead and switch this like so, nothing really changes.
We give this page a refresh. Nothing has changed whatsoever, but we can write a test just to make sure that this is putting the correct value in based on what we've chosen. So let's go ahead and create our test for this.
So let's say PHP artisan pest test. Let's just call this language test. And we will make this obviously not a unit test. It's going to be a feature test.
So let's go over to that language test over in features and let's go and write our first test. So we'll say sets the language correctly. So we'll make sure that whatever we pass down to this endpoint, remember
this is a post endpoint actually get set. So we will choose to pass down a language here of DE, and let's just go ahead and set that to a language variable so we can reference it later on down here. And we want to assert that the status.
Now this is a tricky one because we are hitting an endpoint that redirects us back, the status is actually going to be a 302 we can change that over, or we can just ignore it. Of course, with inertia, it's just the way that this works, where
we've always redirected back. So, uh, as well as a saying that the status is 302, we want to assert that the session has under the language key, the value of the language that we've just set, that is pretty much it.
Let's pull these two things down. Let's run this and see how we get on. So let's run pest tests and feature and language test. And that pass is great.
Obviously, if we were to change this over to something that wasn't correct, it's going to fail. So we know that that is now successfully setting that language. So next up, we can just duplicate this whole test down.
We want to make sure that it sets the default language. If the chosen language is invalid. So we want to pass down a language here and we can get rid of this language variable, let's say Spanish.
And in here we want to make sure that the session contains EN because that's how default locale. We could even reference this from config just to make this a little bit less brittle so we can say app locale and that should be good.
So if we pass down one that doesn't exist, this should set it to EN by default. Let's go ahead and rerun that test. And there we go. Sure enough, it's green.
So that pretty much sums up how we initially set this, but now we need to do something with this within middleware. So for every request, it actually sets the current locale to the one that we have contained within that long running session.
9 episodes 57 mins


Localisation is a breeze in Laravel applications, but what happens when you need to bring this to the client-side? Turns out in Inertia, it's pretty simple.

In this course, we'll build a language switcher, share translations with the client, and build a simple translation helper for Vue to use directly in templates. We'll also cover caching translations to keep things running smoothly.

The best part? With the magic of reactivity, we'll be able to switch languages without any page refresh, and see everything instantly translated.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.