In this episode, we're setting up a robust way to handle available languages in our Laravel application using an Enum. First, we talk about why it's important to validate the list of languages your app supports—so both the backend and frontend know exactly which languages are on tap. We want to be able to reference languages easily, both by a value (like "en" or "de") and a user-friendly label (like "English" or "Deutsch").
We start by creating a new Lang enum, dropping it right in a folder called lang
, and giving it some cases—one for English and one for German. This enum will serve as the single source of truth for which languages are available. Alongside, we add a helper method to the Enum called label()
, which returns the human-readable label for each language. This way, your frontend doesn't end up displaying cryptic codes to users.
Next up, we use the php artisan lang:publish
command to generate language files, giving you a quick introduction to how Laravel handles localization. After that, we write a unit test to ensure our Enum’s label helper returns the correct label for a given language—because it's always nice to have that extra peace of mind!
By the end of this episode, you'll have a clean, scalable setup: anytime you need to add a new language, just pop another case into the Enum and update the helper method. In the next steps, we'll see how to share this list of available languages with the frontend so users can pick their preferred language from a dropdown.