In this episode, we tackle the task of making our available languages accessible throughout the entire app. Our end goal is to have a nice, structured list of the languages (with both their code and display label) that we can use globally — for example, to power a language-switching dropdown in the header.
We start by pulling all the language enum cases using PHP’s built-in features, but quickly realize we need more than just their codes (e.g. EN
, DE
). So, we set up a custom API Resource (a LanguageResource
) to format each language as an object with both its value and label, making it much more convenient for use on the front-end.
To keep our shared data from being unnecessarily wrapped inside a data
array, we configure our app’s JSON resources to not wrap their responses by default. This gives us a simple, clean array available to our JavaScript.
Finally, to make sure everything’s working (and stays working as things change), we write a feature test. The test checks that the languages are always included as a prop in our Inertia pages, and that they’re properly structured with the correct values and labels. This sets us up nicely so that, in the next episode, we can build a dropdown that actually displays and uses these language choices.