#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Adding Localization and Translations

Localization lets a single Laravel application serve content in multiple languages, rather than maintaining separate codebases per language.

How Laravel Organizes Translations

Translation strings live in language files under the lang directory (or resources/lang in older versions), organized by locale.

lang/
├── en/
│   └── messages.php
├── es/
│   └── messages.php

Setting the Application Locale

App::setLocale('es');

The default locale is set in config/app.php, and can be changed per-request based on user preference, URL segment, or browser headers.

A Simple Translation File

// lang/en/messages.php
return [
    'welcome' => 'Welcome back!',
];

With the structure in place, the next step is actually pulling these translated strings into views and application logic.

Showing Translated Text in Views and Code

Laravel reads a translation using the file name and the key, joined by a dot. The __() helper works in controllers, and Blade offers the same helper inside templates.

// controller or any PHP code
$message = __('messages.welcome');

{{-- Blade view --}}
<h1>{{ __('messages.welcome') }}</h1>
<p>@lang('messages.welcome')</p>

If a key is missing for the active locale, Laravel tries the fallback locale set by fallback_locale. If it is missing there too, the key itself is shown, which makes gaps easy to spot during testing.

On Laravel 11 and later, a fresh project has no lang folder. Run php artisan lang:publish to create it with the default validation and auth messages.

Placeholders and Plurals

Hard-coding names or counts into sentences breaks word order in other languages. Use placeholders that start with a colon and pass values at runtime.

// lang/en/messages.php
return [
    'greeting' => 'Hello, :name!',
    'cart'     => '{0} Your cart is empty|{1} You have one item|[2,*] You have :count items',
];

// lang/es/messages.php
return [
    'greeting' => '¡Hola, :name!',
    'cart'     => '{0} Tu carrito está vacío|{1} Tienes un artículo|[2,*] Tienes :count artículos',
];

// usage
__('messages.greeting', ['name' => $user->name]);
trans_choice('messages.cart', $count);

Writing :NAME in capitals makes Laravel insert the value in upper case, and :Name capitalises the first letter.

JSON Translation Files

For large apps with many short strings, JSON files can be easier. The English sentence itself becomes the key, so templates stay readable.

// lang/hi.json
{
    "Save changes": "बदलाव सहेजें",
    "Log out": "लॉग आउट"
}

{{ __('Save changes') }}

Switching Locale for Each Request

A small middleware can read the language from the URL, a session value or the user's profile, then set it before controllers run.

public function handle(Request $request, Closure $next)
{
    $supported = ['en', 'es', 'hi'];
    $locale = $request->segment(1);

    if (in_array($locale, $supported, true)) {
        App::setLocale($locale);
    }
    return $next($request);
}

Always check against a list of supported locales, so a random URL segment cannot select a language folder that does not exist.

Register the middleware on the web route group, and store the chosen language in the session or on the user's profile so it sticks across pages. When the locale comes from the browser instead, $request->getPreferredLanguage($supported) picks the best match from the Accept-Language header.

Habits That Keep Translations Manageable

  • Group keys by feature, such as checkout.php or profile.php, instead of one huge file.
  • Never build sentences by joining translated fragments; translate the whole sentence with placeholders.
  • Format dates and money for the locale, for example with Carbon's translatedFormat() and PHP's NumberFormatter.
  • Set the HTML lang attribute from app()->getLocale() so browsers and screen readers know the language.
  • Check for missing keys before each release by switching through every locale.
  • Translate validation messages too; the published validation.php file in each locale folder controls them.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course