Laravel Localization Module Learning

Source: Internet
Author: User
This article mainly introduces to you about the Laravel Learning Tutorial localization module of the relevant information, the text through the sample code to introduce you to the very detailed, to everyone's study or work has a certain reference learning value, need friends to study together.

Objective

This article mainly introduces to you about the Laravel localization module related content, shares out for everybody reference study, words not to say more, come together to see the detailed introduction.

This article is based on the Laravel 5.4 version of the localization module code for analysis and writing;

Module composition

The relationship between the various documents of the localization module is presented, and a brief description is made.


    • Translationserviceprovider
      The service provider of the localization module is both the portal of a module and the center of interaction with the IOC container; Register the translator instance Translation.loader, register the Translation Management instance translator, and declare the deferred load service;

    • Translator
      Translation management category;

    • Messageselector
      A message filter that selects the appropriate message by judging the complex value, such as the message content {0} not | [1,19] some | [20,*] Many, we preach the number is 18, then the last choice of message is "some";

    • Loaderinterface
      A translator interface; three methods load,addnamespace,namespaces were declared;

    • Fileloader
      Inherits the Loaderinterface, obtains the localization resource data from the file;

    • Arrayloader
      Inherits the Loaderinterface, maintains the localized resource data in the memory with the array;

Configuration Instructions

The parameters related to this module in config configuration directory are only locale and Fallback_locale in the app.php file;

Locale indicates what the default localization language is, which takes precedence from the language resource directory for translation (conversion) content;
If the language indicated by locale does not exist, use the alternate language of Fallback_locale;

The author's locale is Zh_cn,fallback_locale is en;

function Introduction

The Global Language Resources directory under the Resources/lang of the project, each sub-directory in the name of the language, such as EN, ZH_CN, etc.

Other subdirectories are namespace names, which are complementary replacements for third-party load library resource files;

There may also be JSON files such as En.json, ZH_CN, which sometimes read data from JSON files, which are derived from the existing JSON file;

Translating Global Language Resources

The author's language resources root directory Resources/lang under the zh_cn/validation.php, the content is as follows

<?phpreturn [' accepted '  = ' = ': attribute must be accepted. ', ' active_url '  = ': attribute is not a valid URL. ' After ' = '  : attribute must be a date after:d ate. ', ......];

by calling code

App (' translator ')->trans (' validation.accepted ', [' attribute ' = ' username ')]

or global help function trans

Trans (' validation.accepted ', [' attribute ' = ' user name '])

The output "User name must be accepted. ";

The calling process is as follows:

    • Parse key name: Resolves the key name to an array $namespace = '*', $group = 'validation', $item = 'accepted' of (); namespace is *, denoted under the global namespace; group, which is actually the file name, a file is a group; item is the meaning of the element;

    • Gets the language array: Here the $locale is null, so returns an array of default and alternate languages, that is [' zh_cn ', ' en '], and for A For loop, enter the language resource directory to find the desired element value, if found, that is, break;

    • Load resource: Because the namespace is *, the location resource root directory is Resources/lang, the language is ZH_CN, so the subdirectory is zh_cn;group named validation, then resources/lang/zh_cn/ All content in the validation.php file is loaded into memory and saved$this->loaded[$namespace][$group][$locale] = $lines;

    • Get the resource, and replace the parameter: Arr::get $this->loaded[$namespace][$group][$locale] gets the element value from the method: attribute must be accepted. At this point, the parameter array is not empty, the loop is replaced, and the resulting "User name must be accepted." ";

Translating language Resources with namespaces

Author in the Language resources root directory Resource/lang, create vendor/faker/provider/zh_cn/internet.php file, the content is as follows:

<?phpreturn [' message ' = ' Hello, Faker/provider ', ...];

At the same time, the location of the resource root of the third-party plug-in (that is, with the namespace) is registered manually in translator;

App (' translator ')->addnamespace (' Faker/provider ', Base_path (' Vendor/xx/resource/lang '))

Now, get the resource with the namespace;

Trans (' Faker/provider::internet.message ');

Output ' Hello, Faker/provider ';

The calling process is as follows:

    • Resolve key Name: Resolves the key name to an array ($namespace = 'Faker/Provider', $group = 'Internet', $item = 'message');

    • Gets the language array: Here the $locale is null, so returns an array of default and alternate languages, that is [' zh_cn ', ' en '], and for A For loop, enter the language resource directory to find the desired element value, if found, that is, break;

    • Load resource: Because the namespace is Faker/provider, this is two steps, and the first step is to read the information under the third-party plug-in repository, where the root directory of the Read namespace registration is Base_path (' Vendor/xx/resource/lang '). Reads Base_path (' Vendor/xx/resource/lang ')/zh_cn/internet.php content, the file does not exist, returns an empty array, and the second step reads the global language resource and complements it, which is the base_path('resource/lang/vendor/Faker/Provider')/zh_CN/Internet.php; last time the read is saved.$this->loaded[$namespace][$group][$locale] = $lines;

    • Get the resource, and replace the parameter: Arr::get $this->loaded[$namespace][$group][$locale]中 gets the element value from the method "Hello, Faker/provider"; At this time, the parameter array is empty, directly returning the result "Hello, faker/provider";

Translating resources in a JSON file

Author in the Language resources root directory Resource/lang, create Zh_cn.json file, the content is as follows:

{"Name": "Zh_cn.json", "Place": ".". /resources/lang/zh_cn.json "}

Now, get the name value in the JSON file;

Trans (' *.name ')

Output "Zh_cn.json";

The calling process is as follows:

    • Resolve key Name: Resolves the key name to an array($namespace = '*', $group = '*', $item = 'name');

    • Gets the language array: Here the $locale is null, so returns an array of default and alternate languages, that is [' zh_cn ', ' en '], and for A For loop, enter the language resource directory to find the desired element value, if found, that is, break;

    • Load resource: Because the namespace is * and the group is *, a JSON file whose name is a language value under the language root is read, and the Resource/lang/zh_cn.json is read and the contents are read, saved $this->loaded[$namespace][$group][$locale] = $lines;

    • Gets the resource and replaces the parameter: $this->loaded[$namespace][$group][$locale] gets the element value "Zh_cn.json" from the Arr::get method, at which time the parameter array is empty and returns the result "Zh_cn.json" directly;

Run-Time binding resources

The contents of the resources in addition to the file, when used in the reading, also can be stored in the project when the operation;

Take resources/lang/zh_cn/validation.php as an example, now want to add a new element to this group at run time called Extra, you need to specify which language to put in, you can write this

App (' translator ')->addlines (Array (' validation.extra ' = ' test add extra Data '), ' zh_cn ');

You can now get the newly added element value

Trans (' Validation.extra ')

Complex resource filtering

The author adds a translation through the runtime binding resource:

App (' translator ')->addlines (Array (' validation.extra ' = ' {0} ' without |[ 1,19] some | [20,*] many '), ' zh_cn ');

If through trans('validation.extra') , get the whole translation content, not what we expected; using the choice method:

app('translator')->choice('validation.extra', 0) Get no;

app('translator')->choice('validation.extra', 18) Get some;

app('translator')->choice('validation.extra', 20) Get a lot;

Jane can be written as a app('translator')->choice(...) global helper function Trans_choice (...) ;

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.