[Asp.net 5] multi-language Localization-Asp.net runtime,

Source: Internet
Author: User

[Asp.net 5] multi-language Localization-Asp.net runtime,

This section describes the Microsoft. AspNet. Localization project. This project is running inAsp.net 5Multi-language settings during running in the environment.

ASP.net 5 Middleware Technology

In the new Asp.net 5, you can add the component group to the asp.net environment. Registration is implemented through the extension method of the IApplicationBuilder interface.

 public static IApplicationBuilder UseMiddleware(this IApplicationBuilder builder, Type middleware, params object[] args)

[This extension method uses the variable parameter args to pass the required parameters and the external RequestDelegate (delegate Task RequestDelegate (HttpContext context) type (the system will assign a value to this type parameter internally ). Create a Type (middleware) instance. Then call the Type (middleware) Invoke method. However, unlike the constructor, when the Invoke method parameter is greater than one, all parameters come from injection, not args]

For middleware injected through IApplicationBuilder, one method must be Invoke. The parameters of the Invoke method are variable parameters with unlimited length. The system will retrieve parameters from asp.net's inversion control; the Invoke method will be called later. [The question is: why cannot it be defined as an interface containing the invoke method, which is more object-oriented]

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

  • IApplicationBuilderExtensionsThe intermediate registration method is implemented in the Microsoft. AspNet. Localization project.
  • RequestLocalizationMiddlewareThe middleware class is the entry to Microsoft. AspNet. Localization.
Public static class IApplicationBuilderExtensions {public static IApplicationBuilder UseRequestLocalization ([NotNull] this IApplicationBuilder builder) {var options = new RequestLocalizationOptions (); return UseRequestLocalization (builder, options );} public static IApplicationBuilder UseRequestLocalization ([NotNull] this IApplicationBuilder builder, [NotNull] RequestLocalizationOptions options) => builder. useMiddleware <RequestLocalizationMiddleware> (options );}IApplicationBuilderExtensions public class extends {private readonly RequestDelegate _ next; private readonly RequestLocalizationOptions _ options; public extends ([NotNull] RequestDelegate next, [NotNull] RequestLocalizationOptions) {_ next = next; _ options = options;} public async Task Invoke ([NotNull] HttpContext context) {var requestCulture = _ Options. DefaultRequestCulture ?? New RequestCulture (CultureInfo. CurrentCulture, CultureInfo. CurrentUICulture); IRequestCultureProvider winningProvider = null; if (_ options. RequestCultureProviders! = Null) {foreach (var provider in _ options. RequestCultureProviders) {var result = await provider. DetermineRequestCulture (context); if (result! = Null) {requestCulture = result; winningProvider = provider; break ;}} context. setFeature <feature> (new RequestCultureFeature (requestCulture, winningProvider); SetCurrentThreadCulture (requestCulture); await _ next (context);} private static void SetCurrentThreadCulture (RequestCulture requestCulture) {# if DNX451 Thread. currentThread. currentCulture = requestCulture. culture; Thread. currentThread. currentUICulture = requestCulture. UICulture; # else CultureInfo. currentCulture = requestCulture. culture; CultureInfo. currentUICulture = requestCulture. UICulture; # endif }}RequestLocalizationMiddleware

Parameters before registration: RequestLocalizationOptions 

InIApplicationBuilderExtensionsMiddlewareRequestLocalizationMiddlewareDuring registration, instances of the RequestLocalizationOptions type need to be passed in. In most cases, default instances without parameters are passed in.

The RequestLocalizationOptions class has only four attributes. The Code is as follows:

Public class RequestLocalizationOptions {public RequestLocalizationOptions () {DefaultRequestCulture = new RequestCulture (CultureInfo. currentCulture, CultureInfo. currentUICulture); RequestCultureProviders = new List <IRequestCultureProvider> {new QueryStringRequestCultureProvider {Options = this}, new CookieRequestCultureProvider {Options = this}, new Options {Options = this }};} public RequestCulture DefaultRequestCulture {get; set;} public IList <CultureInfo> SupportedCultures {get; set;} public IList <CultureInfo> SupportedUICultures {get; set ;} public IList <IRequestCultureProvider> RequestCultureProviders {get; set ;}}View Code

In the constructor without parameters, the system initializes DefaultRequestCulture and RequestCultureProviders. The system traverses RequestCultureProviders to find the appropriate language. When the appropriate language cannot be found, DefaultRequestCulture is used.

  • RequestCultureProviders: by default, three language sources are initialized: queryString, cookie, and acceptHeader.
  • SupportedCultures: language supported by the system
  • SupportedUICultures: UI Language supported by the system
  • DefaultRequestCulture: default language source

[When we need to customize RequestCultureProvider, we have two methods: the custom class inheritance class RequestCultureProvider (or the interface IRequestCultureProvider), or the CustomRequestCultureProvider is registered in RequestCultureProviders, register the Task <RequestCulture> method in the CustomRequestCultureProvider class constructor.]

Registration order

The system obtains the appropriate language in the order of RequestCultureProviders in RequestLocalizationOptions. If not, DefaultRequestCulture is used. By default, the System reads the <short-circuit algorithm> in the order of queryString, cookie, and acceptHeader (the first three languages are used by default.

Registered RequestCultureFeature

The Context of Asp. net5 runtime environment 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 represented in the following structure:

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.