Lokalizimi

Localization

Introduction

Laravel's localization features provide a convenient way to retrieve strings in various languages, allowing you to easily support multiple languages within your application. Language strings are stored in files within the resources/langdirectory. Within this directory there should be a subdirectory for each language supported by the application:

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

All language files return an array of keyed strings. For example:



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

Configuring The Locale

The default language for your application is stored in the config/app.php configuration file. Of course, you may modify this value to suit the needs of your application. You may also change the active language at runtime using the setLocalemethod on the App facade:

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

    //
});

You may configure a "fallback language", which will be used when the active language does not contain a given translation string. Like the default language, the fallback language is also configured in the config/app.php configuration file:

'fallback_locale' => 'en',

Determining The Current Locale

You may use the getLocale and isLocale methods on the App facade to determine the current locale or check if the locale is a given value:

$locale = App::getLocale();

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

Defining Translation Strings

Using Short Keys

Typically, translation strings are stored in files within the resources/lang directory. Within this directory there should be a subdirectory for each language supported by the application:

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

All language files return an array of keyed strings. For example:

// resources/lang/en/messages.php

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

Using Translation Strings As Keys

For applications with heavy translation requirements, defining every string with a "short key" can become quickly confusing when referencing them in your views. For this reason, Laravel also provides support for defining translation strings using the "default" translation of the string as the key.

Translation files that use translation strings as keys are stored as JSON files in the resources/lang directory. For example, if your application has a Spanish translation, you should create a resources/lang/es.json file:

{
    "I love programming.": "Me encanta programar."
}