[Laravel5.2 documentation] service-localization

Source: Internet
Author: User
[Laravel5.2 documentation] service-localization 1. Introduction

Laravel's localization feature allows you to easily implement multi-language support in applications.

The language string is stored in the resources/lang directory by default. the directory should contain subdirectories of each language supported by the application:

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

All language files return an array of key-value pairs, for example:

  'Welcome to our application'];

Configure Locale options

The default application language is stored in the configuration file config/app. php. of course, you can modify this value to match the application's needs. You can also use the setLocale method on the App facade to change the current language at runtime:

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

You can also configure a "slave language". when the current language does not contain a given language, the slave language is returned. Like the default language, the backup language is also configured in the config/app. php configuration file:

'fallback_locale' => 'en',
2. Basic use

You can use the help function trans to obtain rows from the language file. this method receives the keys of the file and the Language Line as the first parameter. for example, let's get the rows from the language file resources/lang/messages. obtain the Language Line welcome in php:

echo trans('messages.welcome');

Of course, if you use the Blade template engine, you can use the {} syntax to print the language line:

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

If the specified language line does not exist, the trans function returns the key of the Language Line. Therefore, in the preceding example, if the language line does not exist, the trans function returns messages. welcome.

Replace the parameters in the Language Line

If needed, you can define placeholders in the Language Line. All placeholders have a prefix. for example, you can define a welcome message with a placeholder name:

'welcome' => 'Welcome, :name',

To replace the placeholder when obtaining the Language Line, pass a replacement array as the second parameter of the trans function:

echo trans('messages.welcome', ['name' => 'Dayle']);

Diversified

Diversification is a complicated problem because different languages have different rules for diversification. by using the pipe character "|", you can distinguish between the singular and plural forms of a string:

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

Then, you can use the trans_choice function to obtain the Language Line of the given number of rows. In this example, the plural form of the Language Line is returned because the number of rows is greater than 1:

echo trans_choice('messages.apples', 10);

Laravel translator is provided by the Symfony Translation component, so you can create more complex diverse rules:

'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',
3. cover the language files of the Vendor package

Some packages can process their own language files. You can modify these sentences by placing your files in the resources/lang/vendor/{package}/{locale} directory to overwrite them, rather than destroying the core files of these packages.

So, for example, if you need to overwrite the messages in the package named skyrim/hearfire. you can create a resource/lang/vendor/hearthur fire/en/messages. php file. In this file, you only need to define the sentence you want to overwrite. the sentence you have not covered is still loaded from the original language file of the package.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.