Localization

    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 directory. Within this directory there should be a subdirectory for each language supported by the application:

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

    1. return [
    2. 'welcome' => 'Welcome to our application'
    3. ];

    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 setLocale method on the App facade:

    1. Route::get('welcome/{locale}', function ($locale) {
    2. App::setLocale($locale);
    3. //
    4. });

    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:

    1. '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:

    1. $locale = App::getLocale();
    2. if (App::isLocale('en')) {
    3. }

    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:

    1. <?php
    2. // resources/lang/en/messages.php
    3. return [
    4. 'welcome' => 'Welcome to our application'
    5. ];

    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:

    1. {
    2. "I love programming.": "Me encanta programar."
    3. }

    You may retrieve lines from language files using the helper function. The method accepts the file and key of the translation string as its first argument. For example, let's retrieve the welcome translation string from the resources/lang/messages.php language file:

    1. echo __('messages.welcome');
    2. echo __('I love programming.');

    Of course if you are using the , you may use the syntax to echo the translation string or use the @lang directive:

    1. {{ __('messages.welcome') }}
    2. @lang('messages.welcome')

    If the specified translation string does not exist, the function will return the translation string key. So, using the example above, the function would return messages.welcome if the translation string does not exist.

    To replace the place-holders when retrieving a translation string, pass an array of replacements as the second argument to the __ function:

    1. echo __('messages.welcome', ['name' => 'dayle']);

    If your place-holder contains all capital letters, or only has its first letter capitalized, the translated value will be capitalized accordingly:

    1. 'welcome' => 'Welcome, :NAME', // Welcome, DAYLE
    2. 'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle

    Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish singular and plural forms of a string:

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

    You may even create more complex pluralization rules which specify translation strings for multiple number ranges:

    1. 'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',

    After defining a translation string that has pluralization options, you may use the trans_choice function to retrieve the line for a given "count". In this example, since the count is greater than one, the plural form of the translation string is returned:

    You may also define place-holder attributes in pluralization strings. These place-holders may be replaced by passing an array as the third argument to the trans_choice function:

    1. 'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
    2. echo trans_choice('time.minutes_ago', 5, ['value' => 5]);

    So, for example, if you need to override the English translation strings in messages.php for a package named , you should place a language file at: resources/lang/vendor/hearthfire/en/messages.php. Within this file, you should only define the translation strings you wish to override. Any translation strings you don't override will still be loaded from the package's original language files.