Laravel Basic Tutorial--Localization

Source: Internet
Author: User

Localization of

Brief introduction

Laravel's localization features provide a convenient way to get strings in a variety of languages. It allows you to easily support multiple languages in your app.

The language string is stored in a file in 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 is keyed using a string, such as:

 
  ' Welcome to our application '];

Configuring the language environment

The default language used by the app is stored in the config/app.php configuration file. Of course, you can freely modify the current settings as required. You can also use the App mask's SetLocale method to switch languages at run time:

Route::get (' Welcome/{locale} ', function ($locale) {  app::setlocale ($locale);  // });

You can also set an alternate language that will be used when the given language key is not found in the active locale. Alternate languages are 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 locale is given a value:

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

You can use the GetLocale method of the App mask to get the current locale:

return App::getlocale ();

Basic usage

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

Echo Trans (' Messages.welcome ');

If you use the Blade template engine, you can use the double-parenthesis syntax in the view file or use the @lang directive 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. Therefore, if the key in the above example does not exist, then the Messages.welcome is returned.

Parameter substitution in language content

If you need to, you can define a placeholder into your language content. All language placeholders are used: to identify. For example, you want to define a placeholder for a welcome XXX:

' 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 for the language content:

Echo Trans (' Message.welcome ', [' name ' = ' Dayle ']);

If your placeholder contains an uppercase or lowercase capitalization, the replaced string is also handled in the appropriate way:

' Welcome ' = ' Welcome,: Name ',//Welcome, Dayle ' goodbye ' = ' Goodbye,: Name ',//Goodbye, Dayle

Diversified

The diversification of language is a complex problem. Different languages have a variety of complex rules to define diversity. You can do this by using | String to distinguish the singular or plural form from a complete string:

' Apples ' = ' there is one apple| There is many apples ',

You can then use the Trans_choice method to get the language content based on the given number. In this example, because the number is greater than 1, the language extracts the plural form:

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

Because Laravel translators are based on the Symfony translation component, you can create more complex complex plural rules:

' Apples ' = ' {0} there is none| [1,19] There is some| [20,inf] There is many ',

Overwrite Vendor language files

Many packages will come with their own language files. If you need to replace some of these language content, you can store your own language files into the Resources/lang/vendor/{package}/{locale} directory.

So, for example, if you need to overwrite a messages.php file with a package named Skyrim/hearthfire, you need to store your own language file to resources/lang/vendor/hearthfire/en/ message.php. In this file you should only add the language content that you want to overwrite, and any content that is not overwritten 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.