$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[February 23, 2025] Laravel

Laravel Localization: Multi-Language Apps

Laravel Localization: Multi-Language Apps

────────────────────────────────────────────────────────

Laravel Localization: Multi-Language Apps

Tip: Translation Files Structure

return [
    'welcome' => 'Welcome to our app',
    'greeting' => 'Hello, :name',
];

Use __() or trans() to retrieve.

Gotcha: JSON Translation Files

{
    "Welcome to our app": "Bienvenido a nuestra app"
}

JSON files use the source text as the key.

Tip: Pluralization

trans_choice('messages.there_is_one_post', 5, ['count' => 5]);

Laravel picks the right form based on count.

Gotcha: Locale is Per-Request

Setting App::setLocale('es') only affects the current request. Use middleware for persistent locale.

Tip: Date Localization

Carbon::setLocale('fr');
echo now()->diffForHumans();

Gotcha: Translation Fallback

If a key doesn't exist in the current locale, Laravel returns the key itself.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Localization goes far beyond translating strings. I've worked on multilingual apps where date formats, number formatting, pluralization rules, and even sorting order differed by locale. Laravel's __() helper is just the start. The hardest lesson: test RTL (right-to-left) languages early. Adding CSS support for Arabic or Hebrew after the UI is built is a painful refactoring exercise.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts