// // // using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; using System.Globalization; namespace ERP.Framework.Extensions { public static class I18NExtension { public static void UseI18N(this IApplicationBuilder app) { app.UseRequestLocalization(options => { var supporttedCultures = new List { new CultureInfo("en-US"), new CultureInfo("zh-CN"), }; options.DefaultRequestCulture = new RequestCulture("en-US"); options.SupportedCultures = supporttedCultures; options.SupportedUICultures = supporttedCultures; options.RequestCultureProviders = new List { new LangCultrueProvider(), }; }); } } public class LangCultrueProvider : RequestCultureProvider { /// public override Task DetermineProviderCultureResult(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } var lang = httpContext.Request.Headers["lang"].ToString(); if (!lang.IsNullOrEmpty()) { return Task.FromResult(new ProviderCultureResult(lang)); } return NullProviderCultureResult; } } }