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
Playing
09. Caching translations

Transcript

00:00
Every time we access the translation props which drive the translations that we've got, we're actually reading files. Now, this isn't great for every single request. This is going to take a little bit longer and the more and more files that you add to your translations, the slower this is going to get. So really what we want to do is cache this
00:20
so it doesn't make a request to our files or read these files every single time. So we're going to make a really simple change here just to cache everything that we're grabbing here so this speeds things up. Now, we can go ahead and monitor this. We can either use something like debug bar or in this case
00:36
just look at vet check XHR or our all tab. At the moment, we obviously don't have a very slow page load, but you can always monitor that there as well. Okay, so let's go ahead and cache this. Now to do this what we're going to do is use the cache helper. You might also want to configure how you're caching things. At the moment, our cache driver is just set to file.
00:57
This might be something set to something like Redis. It really doesn't matter, but either way it's going to speed it up. So we're going to go ahead and use the cache helper and we're going to remember this forever. You can change this just to remember this for a certain amount of time, but I think for translations, when these do change, you're going to deploy this and have your cache cleared on your
01:16
deployment anyway, or you can manually clear these. So I'm just going to go ahead and say we want to remember these forever because they're not something dynamic that changes based on user input. So in this we're going to go ahead and pass in a closure and we're just going to return here. We need to add a key in here. That's really important, of course. We're going to return everything that we've done in here.
01:37
So we're just going to grab our entire collection that we have in here that is being returned and that will do it. Now we need to come up with a cache key. We can't do something like translations because that's going to cache this for one time and that could be English or German and we're actually going to end up with an issue here. So we need to append onto this. Just to give you a demo of this, if we just give this a refresh now,
01:59
we're on German, but when I switch to English, it doesn't change because the cached version now contains German. So we're just stuck with the German language in there. So we're going to append onto this a cache key and of course, that's going to be the current locale that we're working with. So we're just going to say app get locale and that should be it.
02:19
So now we have a unique cache key for each locale that contains all of them translations. So English works, German works, and although we can't really notice at this point, it is a lot faster because we're caching them values and we're returning them from our cache. Now, of course, the only issue with this is if you add more translations, particularly while you're developing,
02:41
these are not going to show up. You can either disable the cache on development, or if you need to clear the cache at any point, you're just going to run phpr to send cache clear. That's going to clear it out for you on the first load. That's going to refetch them and then subsequent requests will just return the cached translations.
9 episodes 57 mins

Overview

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!

Comments

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