Laravel basic tutorial-localization-php Tutorial

Source: Internet
Author: User
Laravel basic tutorial-Introduction to localization

Laravel's localization feature provides a convenient way to obtain strings in various languages. It allows you to easily support multiple languages in applications.

The language string is stored in files under the resources/lang directory. In this directory, multiple supported language subdirectories should be divided:

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

All language files simply return an array that uses string-based keys, for example:

  'Welcome to our application'];

Configure the language environment

The default language used by the application is stored in the config/app. php configuration file. Of course, you can modify the current settings as needed. You can also use the setLocale method of the App mask to switch the language at runtime:

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

You can also set a backup language that is used when the given language key is not found in the activated language environment. The standby language is also set in the config/app. php configuration file:

'fallback_local' => 'en',

You can use the isLocale method of the App mask to determine whether the current language environment has a given value:

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

You can use the getLocale method of the App mask to obtain the current language environment:

return App::getLocale();
Basic usage

You can use the trans help method to extract content from a language file. The trans method receives the file name and key value as its first parameter. For example, let's retrieve the welcome key in the resources/lang/messages. php language file:

echo trans('messages.welcome');

If you use the Blade template engine, you can use the double brackets syntax in the view file or use the @ lang command to extract the content:

{{ trans('message.welcome') }}@lang('messages.welcome')

If the key in the specified language file does not exist, the trans method simply returns the key name. If the key in the preceding example does not exist, messages. welcome is returned.

Replace parameters in the language content

If you need it, you can define a placeholder in your language content. All language placeholders are identified. For example, you want to define a placeholder that welcomes XX:

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

You can pass an array as the second parameter in the trans method, which replaces the value of the array with the placeholder of the language content:

echo trans('message.welcome', ['name' => 'dayle']);

If your placeholder contains uppercase letters or all uppercase letters, the replaced string is also processed accordingly:

'welcome' => 'Welcome, :NAME', // Welcome, DAYLE'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle
Diversified

Language diversification is a complex problem. Different languages have various complex rules to define diversity. You can use a | string to distinguish the singular or plural forms from a complete string:

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

Then you can use the trans_choice method to obtain the language content based on the given number. In this example, the language extracts the plural form because the number is greater than 1:

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

Laravel translators are based on Symfony Translation components, so you can create more complex rules:

'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',
Cover vendor language files

Many packages contain their own language files. If you need to replace some of the language content, you can store your own language files to the resources/lang/vendor/{package}/{locale} directory.

Therefore, for example, if you need to overwrite a package named skyrim/hearfire messages. php file, you need to store your own language file to resources/lang/vendor/hearthur fire/en/message. php. In this file, you should add only the language content you want to overwrite. any unoverwritten content will also use the original language file that comes with 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.