[ASP. 5] Localization-asp.net Run-time multi-language

Source: Internet
Author: User
Tags httpcontext

This section is about the Microsoft.AspNet.Localization project. The project is a run-time multilanguage setting that runs in an ASP. NET 5 environment.

ASP 5 Middleware Technology

In the new ASP. NET 5, you can set the component group book into an ASP. Registration is implemented through an extension method of the interface Iapplicationbuilder.

 Public Static Iapplicationbuilder Usemiddleware (thisparamsobject[] args)

[The extension method passes the required parameters through the Variadic args and the external requestdelegate (Delegate Task Requestdelegate (HttpContext context)) type (the system internally assigns a value to the type parameter). Create a type (middleware) instance. The Invoke method of type (middleware) is then called, but unlike the constructor, when the Invoke method argument is greater than one, the arguments are all from injected, not args]

Through the Iapplicationbuilder injected middleware, there must be one way is that the parameters of the Invoke,invoke method are variable parameters, the length is unrestricted, the system will get the parameters from the reverse control of ASP; The Invoke method is called after the system. [ Here's a question: Why not define an interface that contains an invoke method, more object-oriented ]

We can find two classes in the Microsoft.AspNet.Localization project:

    • iapplicationbuilderextensions Intermediate Registration method, the implementation in the Microsoft.AspNet.Localization project.
    • Requestlocalizationmiddleware Middleware class, is the entrance of Microsoft.AspNet.Localization.
     Public Static classiapplicationbuilderextensions { Public StaticIapplicationbuilder userequestlocalization ([Notnull] ThisIapplicationbuilder Builder) {            varOptions =Newrequestlocalizationoptions (); returnuserequestlocalization (builder, options); }         Public Staticiapplicationbuilder userequestlocalization ([Notnull] Thisiapplicationbuilder Builder, [Notnull] requestlocalizationoptions options)= Builder. Usemiddleware<requestlocalizationmiddleware>(options); }
iapplicationbuilderextensions
     Public classRequestlocalizationmiddleware {Private ReadOnlyrequestdelegate _next; Private ReadOnlyrequestlocalizationoptions _options;  PublicRequestlocalizationmiddleware ([Notnull] requestdelegate Next, [Notnull] requestlocalizationoptions options) { _next=Next; _options=options; }         Public AsyncTask Invoke ([Notnull] HttpContext context) {varRequestculture = _options. Defaultrequestculture??Newrequestculture (CultureInfo.CurrentCulture, cultureinfo.currentuiculture); Irequestcultureprovider Winningprovider=NULL; if(_options. Requestcultureproviders! =NULL)            {                foreach(varProviderinch_options. Requestcultureproviders) {varresult =awaitprovider.                    Determinerequestculture (context); if(Result! =NULL) {requestculture=result; Winningprovider=provider;  Break; }}} context. Setfeature<IRequestCultureFeature> (Newrequestculturefeature (Requestculture, Winningprovider));            Setcurrentthreadculture (requestculture); await_next (context); }        Private Static voidsetcurrentthreadculture (requestculture requestculture) {#ifDNX451Thread.CurrentThread.CurrentCulture=requestculture.culture; Thread.CurrentThread.CurrentUICulture=requestculture.uiculture;#elseCultureInfo.CurrentCulture=requestculture.culture; CultureInfo.CurrentUICulture=requestculture.uiculture;#endif        }    }
Requestlocalizationmiddleware

Pre-registration parameter Requestlocalizationoptions

In iapplicationbuilderextensions , when we register for middleware Requestlocalizationmiddleware , An instance of the Requestlocalizationoptions type needs to be passed in, and most of the time the default parameterless instance is passed in.

We see that the Requestlocalizationoptions class has only 4 properties, the code is as follows:

 Public classrequestlocalizationoptions { Publicrequestlocalizationoptions () {defaultrequestculture=Newrequestculture (CultureInfo.CurrentCulture, cultureinfo.currentuiculture); Requestcultureproviders=NewList<irequestcultureprovider>            {                NewQuerystringrequestcultureprovider {Options = This },                NewCookierequestcultureprovider {Options = This },                NewAcceptlanguageheaderrequestcultureprovider {Options = This }            }; }         PublicRequestculture Defaultrequestculture {Get;Set; }  PublicIlist<cultureinfo> SupportedCultures {Get;Set; }  PublicIlist<cultureinfo> Supporteduicultures {Get;Set; }  PublicIlist<irequestcultureprovider> Requestcultureproviders {Get;Set; } }
View Code

We see that in the parameterless constructor, the system initializes the Defaultrequestculture and Requestcultureproviders. The system iterates through the requestcultureproviders to find the appropriate language and uses defaultrequestculture when no suitable language can be found.

    • Requestcultureproviders: Default initialization of three multi-language sources: queryString, cookies, AcceptHeader.
    • SupportedCultures: System-supported languages
    • Supporteduicultures: System-supported UI language
    • Defaultrequestculture: The default language source

[When we need to customize Requestcultureprovider, we have two ways, the custom class inherits the class Requestcultureprovider (or directly inherits the interface Irequestcultureprovider), or register Customrequestcultureprovider with requestcultureproviders and register in the Customrequestcultureprovider class constructor task< Requestculture> Method ]

Order of Registration

The system obtains the appropriate language in the order of Requestcultureproviders in requestlocalizationoptions, and if not, uses defaultrequestculture. By default, the system reads < is short-circuit algorithm > based on the order of QueryString, Cookies, AcceptHeader (up to the top three languages by default).

Requestculturefeature after the registration

The ASP.NET5 Runtime environment context can register different types of parameters, so the system finally registers an instance of the Irequestculturefeature interface.

Context. Setfeature<irequestculturefeature> (new requestculturefeature (Requestculture, WinningProvider));

Overall structure

The entire project file can be expressed in the following structure:

[ASP. 5] Localization-asp.net Run-time multi-language

Related Article

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.