i18n in Laravel: A Practical Guide (with Examples)

Updated: February 13, 2024 By: Guest Contributor Post a comment

Introduction

Laravel, a powerful MVC PHP framework, is known for its ease of use and flexibility, especially when it comes to building scalable and maintainable web applications. Internationalization (i18n) is a feature that’s essential for creating applications that cater to a global audience, enabling you to present your application’s interface in different languages. In this guide, we’ll dive into how to implement i18n in Laravel, complete with practical examples.

Basic i18n in Laravel

Laravel provides built-in support for internationalization, making it easier for developers to add multiple languages to their applications. This is accomplished through language files, which are used to store string translations for different languages. Laravel uses these files to swap out text based on the user’s preferred language, allowing for a seamless multilingual experience.

Setting Up Language Files

To begin, language files are typically stored in resources/lang directory. Inside this directory, create a folder for each language you plan to support (e.g., en for English, fr for French). Within each language folder, you’ll create PHP files that return key-value pairs of translated strings. For example:

<?php

return [
    'welcome' => 'Welcome to our application!',
];

This is a basic example of an English language file (resources/lang/en/messages.php).

Using Translations in Views

In your Laravel views, you can use the __() helper function to retrieve translations. The __() function accepts the file name followed by the key of the translation string. For instance:

<h1>{{ __('messages.welcome') }}</h1>

If you had a French translation file with the equivalent entry, and the application locale is set to French, Laravel would display the French welcome message instead.

Changing the Application Locale

The application’s locale determines which language files to use for translations. You can change this setting in the config/app.php file by modifying the 'locale' value:

'locale' => 'en',

Alternatively, you can dynamically change the application’s locale using the App::setLocale($locale) method.

Advanced Localization Techniques

While basic key-value translations serve many applications well, sometimes more complex localization scenarios arise. Laravel provides several tools to tackle these cases effectively.

Pluralization

Laravel’s translation engine supports pluralization, allowing you to define singular and plural forms of words based on the quantity. This is done through a special syntax in your language files:

'apples' => 'There is one apple|There are many apples',

In your views, you can utilize the trans_choice() function to handle pluralization automatically:

@php
$appleCount = 10;
@endphp

<p>{{ trans_choice('messages.apples', $appleCount) }}</p>

JSON Translation Files

For projects requiring a high degree of flexibility, Laravel supports JSON language files. These files allow you to map a single key to multiple languages without the hierarchical structure of PHP files. To use this feature, simply place a JSON file named after the locale in the resources/lang directory:

{
    "Welcome to our application!": "Bienvenue sur notre application!",
}

In views, you can use the __() function as you do with PHP files, but with the string to translate as the only parameter:

<h1>{{ __('Welcome to our application!') }}</h1>

Conclusion

Through this guide, it’s clear that Laravel makes it straightforward to add internationalization to your application, broadening your reach to a global audience. By utilizing language files, taking advantage of pluralization, and incorporating JSON translation files when necessary, you can create inclusive, multilingual applications with ease.

Remember, every international application starts with a single step: planning your language strategy. With Laravel as your framework and this guide in your toolkit, you’re well-equipped to take that step confidently.