Localization
| Lang files | |
| The __ helper | |
| Switch locale | |
| Plurals |
Lang files
Phrases live in plain PHP files under the lang folder, grouped by locale code such as en or de. Each file returns an array that maps short keys to full sentences, so templates never hardcode words. A small English file looks like this:
lang/en/messages.php
welcome => Welcome back bye => See you soon
The __ helper
The double underscore helper looks up a key and prints the phrase for the current locale. Pass the file name, a dot, then the key, and Laravel finds the matching line automatically.
echo __("messages.welcome"); echo __("messages.bye");
Switch locale
The App facade stores the active locale for the current request. Set it from a route parameter or a user setting, and every later helper call renders in the new language.
use Illuminate\Support\Facades\App; App::setLocale("de"); echo App::getLocale();
Plurals
Some lines depend on a count, like one apple versus many apples. The trans choice helper picks the right form from a pipe separated line stored in the lang file, driven by the number you pass in.
echo trans_choice("messages.apples", 1); echo trans_choice("messages.apples", 5);
Next: Testing
Article author: Arthur Isaev