Localizations and Translations in Laravel (Spanish and English)

Video thumbnail

With the native features offered by Laravel for handling localization, that is, the place where the application is being consumed, and with this, being able to offer another service, such as the automatic translation of texts based on localization or user selection. In this section, we will see how to implement both topics.

We already know how to handle Custom Exceptions in Laravel

Localization in Laravel is one of those features that, once you master them, allow you to take your applications to another level. In my experience, it not only improves usability but also opens the door to new markets and users. Throughout this guide, I will tell you how to configure, create, and manage translations in Laravel, relying on real examples and best practices.

1. What is Localization in Laravel and Why is it Important

Localization (L10n) is the process of adapting your application to different languages, currencies, or regional formats. Laravel integrates it natively, facilitating both text translation and automatic user language detection.

In one of my first multi-language projects, I discovered that minimal configuration was enough to offer the complete interface in English and Spanish. This capability is especially useful if your application has international users or if you plan to expand outside your initial market.

Key difference:

  • Internationalization (i18n): preparing the app to handle multiple languages.
  • Localization (L10n): adapting specific texts and configurations to each language.

2. Initial Localization Configuration in Laravel

When creating a new Laravel project, you will notice that the /lang folder does not exist. In my first attempts, I thought it was an error, but Laravel actually expects you to generate it yourself with a simple command:

$ php artisan lang:publish

This command creates the /lang folder and publishes the base language files that Laravel uses internally.

The lang:publish command will create the lang directory in your application and publish the default set of language files used by Laravel:

Folder for translations

Inside config/app.php you can define the default language by modifying the line:

'locale' => 'es',

If you work with multiple languages, it is also advisable to adjust the fallback language:

'fallback_locale' => 'en',

In this way, if a translation is missing in Spanish, Laravel will display the text in English.

3. Creating Language Files (PHP and JSON) and Text Strings for Translation

Laravel provides two ways to manage translation strings, which are used to display the translated texts of our application in different languages; in both cases, we must create a folder to store them:

/lang

Where we create our translation files, whether PHP:

/lang
    /en
        messages.php
    /es
        messages.php

Or in JSONs:

/lang
    en.json
    es.json

We will use the format of the files in PHP; you can create as many as you want and modularize the messages according to your preferences. 

Finally, it is up to us to define the translated strings and customize them by changing the predefined text by the application and creating our own; using the key/value pair, the key and the translation are indicated; for example:

lang/en/messages.php

 'Welcome to our application!', ];

lang/es/messages.php

 'Bienvenido a nuestra aplicación!', ];

4. Displaying Translations in Views and Controllers

Once the strings are created, you can display them with the __() function or with the @lang directive in Blade.

echo __('messages.welcome')

As you can see, we must place the file name and the key as the argument for the __() function, which returns the translated text; we can use this scheme in both the controller and similar places, as well as in the view.

Common errors:

  • Using an incorrect key (Laravel will return the literal key).
  • Not publishing the /lang folder.
  • Forgetting to clear the cache with php artisan config:clear after changing language files.

5. How to Change the Language Dynamically in Your App

To change the language during navigation, you can do it manually or through a middleware.

Generate localization middleware

$ php artisan make:middleware Localization

And in the handle method:

public function handle($request, Closure $next)
{
   $locale = $request->get('lang', config('app.locale'));
   app()->setLocale($locale);
   return $next($request);
}

Thus, if the user visits /home?lang=es, the application will automatically switch to Spanish.

In one of my projects, I added a language selector in the site's header and saved the preference in the session to maintain the choice on each visit:

session(['locale' => $locale]);
app()->setLocale(session('locale', 'es'));

6. Advanced Localization and Best Practices

Once you master the basic configuration, you can advance to more complex scenarios:

  • Translation of dynamic content (from the database): using packages like Spatie Translatable.
  • Translated routes: with Laravel Localization.
  • Multi-language SEO: implementing tags.

Additionally, Laravel allows pluralization and variable substitution in strings:

'notifications' => '{0} You have no notifications|{1} You have one notification|[2,*] You have :count notifications',

With this structure, the framework automatically chooses the correct translation based on the number.

7. Conclusion: Experience and Practical Recommendations

Localization is a tool that marks the difference between a functional app and an app prepared to scale globally.
In my experience, the most important thing is to maintain a clear structure in the language files, use consistent key names, and document new translations within the team.

If you work in a team, an additional tip is to use a tool like Phrase or Linguise to synchronize translations and avoid inconsistencies.

And remember: although Laravel does much of the work for you, the quality of localization depends on how organized you keep your texts and your workflow.

❓Frequently Asked Questions

What is the difference between JSON and PHP translations?
PHP files allow grouping texts by module, while JSON saves the strings directly. PHP is more scalable.

How do I automatically detect the language?
You can use Request::getPreferredLanguage() or create a middleware that reads the language from the browser or the URL.

Can I translate dynamic content?
Yes, with packages like Spatie Translatable you can save versions in multiple languages in the database.

How do I change the language without reloading the page?
You can use AJAX or Livewire to update the content based on the user's language selection.

The next step is to learn the Authorization system in Laravel with Gates and Policies.

I agree to receive announcements of interest about this Blog.

We will see how we can create translation texts in Laravel to use them according to the detected or configured location.

| 👤 Andrés Cruz

🇪🇸 En español