02. Storing available languages with an Enum


The languages that we want to be available need to first of all be validated because
on the back end when we choose a language or the user chooses a language we need to know that this actually exists. We also need to display the languages on the front end and we need a value and a label associated with them. So a really good use case here would be for an enum which contains the cases that we want so the different languages that we want
and then a helper function to allow us to label these so the user on the front end doesn't see something like en or de for German. So we're going to go ahead and create an enum to make this a little bit easier. So over in our main app directory doesn't really matter where we put this. I'm just going to create a lang folder and inside of here I'm going
to create a lang.php file and that's going to be our enum. So let's go ahead and namespace this as applang and then we'll create this lang enum. Now this needs to contain cases that have strings so we're going to go ahead and type in this just here and let's just choose the two languages that we want to be available. So we're going to call this en for English and for
this we're going to give the value that we want to see wherever this is stored. It could be stored in the database but in our case this is going to be stored in a session and I'm just going to choose another one here for German which is de. So we've just got two now but of course whenever you need to add to this you're just going to come to your enum, add another case, add the label and
you're good to go. Now before we start to look at these we do need to publish our lang files just so we have a better idea of where these are. So we're going to go ahead and use php artisan lang publish. That's going to go ahead and create out our language files for us and they live down here in this lang directory. At the moment we only have en but we can create different folders with
different translation files in here. So let's just take a look at one of these just in case you're not familiar with a localization within Laravel. These are just keys and values that will be displayed wherever you need them. So in the case of auth these are pretty much all used behind the scenes, same with pagination passwords. With validation there's quite a few here of course and
you can go ahead and translate each of these but we're going to be creating our own specifically for that piece of dashboard text where we welcome the user. So we'll do that in just a bit but at least we've got our language files in there to start with. Now with our enum what we want to be able to do is for any language that we give it, so for example German, English or any other languages,
we want an associated label with this. So what we want to do is create out a helper method in here which allows us to return that specific label. So I'm going to go ahead and start to create this then we'll write a test for this just to verify that this works. So I'm going to create out a label and this is going to return a string so it's going to be either English or German in our case
and for this we're going to go ahead and use the match functionality in php taking the current enum that we're working with so each of these enums when we use them are going to be represented so if we have one representing en that will give us the label for en and de so these are sort of instances of their own. So with this match statement all we need to do is say well if we have
English as the instance of this enum we want to return English as the label and if it's German so if it's de we want to go ahead and output German in here like this and of course whenever we add a new language we add a new case and then we add a new label just in here. So let's go ahead and write a test just to verify that this actually works and then of course we'll actually use this
functionality a little bit later on of course knowing this is backed up by a test and we can actually use it so we're going to go ahead and make out a test so let's say php artisan test test and we're going to call this what let's just call this lang test and we'll make this a unit test so we're going to go over to lang test and let's go ahead and say it can get an associated
language label that just about do it so what do we want to work out here well if we have an enum that represents en we basically just want to know that when we call that label function on that it returns the text English and the same with German as well we'll just test one of these we'll test the German one just so we know that this works so we're going to go ahead and use an
expectation here and we're going to use the enum so let's go ahead and pull that in and we're going to use the from method on this so that just exists as part of enums in php and we're going to say de so that is remember the value just here and when we call the label method on this instance of the enum with this de value we should get back the text Deutsch or German so we're going to go
ahead and invoke the label method and we want to expect that this is so we're going to use to be deutsch so there we go let's go ahead and run this test make sure this is working so we're going to run pests we're going to go into our test directory under unit and we're going to run the lang test and there we go it passes you can check that this does fail for example if i say
English we obviously expect that to fail so we know now that for whatever language we're either iterating through or we're trying to use we know that we can get a label for that so now that we have created this enum that uses any of the languages that we want available now it's time to share these with our client side so we can use that list of available languages within our select
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.