Explanation of Laravel Localization

0 740

In this article, we will discuss the Laravel Localization. As we know, Laravel provides rich features and Laravel Localization is one of the best features of Laravel.

Introduction Laravel Localization

Laravel Localizations feature provides an easy and simple way to get required strings in different languages. All of the Language strings stored in a separate file located at “resource/lang” directory. Laravel provides some of the default translation files in this folder.

/resource
    /lang
        /en
            auth.php
            pagination.php
            passwords.php
            validation.php

Localization Configuration

You can manage the localization configuration in “config/app.php”. Here’s the default “locale” & “fallback_locale” available and you can change as per your default application language. You can also use the given method to change the active language at runtime.

Route::get('welcome/{locale}', function ($locale) {
    App::setLocale($locale);

});

You can use the given method to get the current default language and also apply condition as per the language.

// Get current app locale

$locale = App::getLocale();

// if statement to check the locale

if (App::isLocale('fr')) {
    //
}

Translation Strings

When we need to add the new language, then just create a new folder with the lang shortcode such as I need to add the French language, so I’m creating a folder with the name of “fr” shortcode. And creating one file name “messages.php”. In this file, we will store our translated words for the French language. Same for the default language folder “en”. Create “messages.php” in the “en” folder.

/resource
    /lang
        /en
            messages.php
        /fr
            messages.php

All language files return an array. For example:

<?php

// messages.php in "en" directory
return [
    'welcome' => 'Welcome to our application'
];

// messages.php in "fr" directory
return [
    'welcome' => 'Bienvenue dans notre application'
];

Retrieve Translated Strings

You can use the Laravel helper function underscores __() to retrieve the translated strings. The underscores __() method accepts the file & key of the string. For example, as per our file, we create welcome string so now we retrieve the same welcome string.

echo __('messages.welcome');

Above statement print, the welcome string as per mentioned the current application language. In the blade template, you can use the given statement.

{{ __('messages.welcome') }}

// Use @lang directive

@lang('messages.welcome')

Conclusion

In this article, I’m trying to explain to you the basic of the language localization. I know this topic needs more details. I personally advise you to check the official documentation once. Also, you can found more tutorial on Laravel here. In the future, we will discuss more on Laravel Localization. For now, please feel free to add comments if any query.

Leave A Reply

Your email address will not be published.