Created
November 9, 2019 15:10
-
-
Save Legends/780b6c66e583ce0c9beed462952a1b6a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Globalization; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.Builder; | |
| using Microsoft.AspNetCore.Hosting; | |
| using Microsoft.AspNetCore.HttpsPolicy; | |
| using Microsoft.AspNetCore.Localization; | |
| using Microsoft.AspNetCore.Mvc.Razor; | |
| using Microsoft.Extensions.Configuration; | |
| using Microsoft.Extensions.DependencyInjection; | |
| using Microsoft.Extensions.Hosting; | |
| using Microsoft.Extensions.Options; | |
| namespace Demo_LocalizationSetUp_DataAnnotation_jQueryValidation | |
| { | |
| public class Startup | |
| { | |
| public IConfiguration Configuration { get; set; } | |
| public Startup(IConfiguration config) | |
| { | |
| Configuration = config; | |
| } | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| services.AddLocalization(options => options.ResourcesPath = "Resources"); | |
| services.Configure<RequestLocalizationOptions>(o => | |
| { | |
| //o.ApplyCurrentCultureToResponseHeaders = true; | |
| var supportedCultures = new List<CultureInfo> | |
| { | |
| new CultureInfo("en-US"), | |
| new CultureInfo("fr-FR"), | |
| new CultureInfo("de-DE"), | |
| new CultureInfo("es-ES") | |
| }; | |
| // default request culture providers: --> o.RequestCultureProviders | |
| // 1- new QueryStringRequestCultureProvider | |
| // 2- new CookieRequestCultureProvider | |
| // - new AcceptLanguageHeaderRequestCultureProvider | |
| // we only want the cookie request provider here | |
| o.RequestCultureProviders = o.RequestCultureProviders.Where(rcp => rcp.GetType() == typeof(CookieRequestCultureProvider)).ToList(); | |
| // Example: Access CookieRequestCultureProvider | |
| var cp = o.RequestCultureProviders.OfType<CookieRequestCultureProvider>().FirstOrDefault(); | |
| cp.CookieName = Configuration.GetValue<string>(CommonStrings.Config_RequestCultureCookieName_Key); // "App-Culture-Cookie-Name"; | |
| o.DefaultRequestCulture = new RequestCulture("es-ES"); // if nothing found this one is used as default | |
| o.SupportedCultures = supportedCultures; | |
| o.SupportedUICultures = supportedCultures; | |
| //// Custom RequestCulture Provider | |
| //o.AddInitialRequestCultureProvider(new CustomRequestCultureProvider(async context => | |
| //{ | |
| // // Usually you extract the requst culture from: | |
| // // a cookie or | |
| // // a GET parameter or | |
| // // the Accept-Language header | |
| // // but you can also specify a custom way of getting/setting the current request culture: | |
| // // this implementation always returns "en-US", you would normally make it dynamic | |
| // // fetching the culture from another source | |
| // return await Task.FromResult(new ProviderCultureResult("en-US")); | |
| //})); | |
| }); | |
| services.AddControllersWithViews().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix) | |
| .AddDataAnnotationsLocalization(o => | |
| { | |
| o.DataAnnotationLocalizerProvider = (type, factory) => | |
| { | |
| return factory.Create(typeof(SharedResource)); | |
| }; | |
| }); | |
| } | |
| public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
| { | |
| app.UseStatusCodePages(); | |
| app.UseDeveloperExceptionPage(); | |
| app.UseStaticFiles(); | |
| // Localization middleware | |
| var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>(); | |
| app.UseRequestLocalization(localizationOptions.Value); | |
| app.Use(next => async context => | |
| { | |
| // Example of how to set a custom Request Culture Cookie: | |
| if (Configuration.GetValue<string>(CommonStrings.Config_RequestCultureProvider_Key) == CommonStrings.Cookie) | |
| { | |
| string cookieName = Configuration.GetValue<string>(CommonStrings.Config_RequestCultureCookieName_Key); | |
| context.Request.Cookies.TryGetValue(cookieName, out string cultureCookie); | |
| if (cultureCookie == null) | |
| { | |
| context.Response.Cookies.Append(cookieName, CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(Configuration.GetValue<string>(CommonStrings.Config_DefaultRequestCulture_Key)))); | |
| } | |
| } | |
| var iso = context.Features.Get<IRequestCultureFeature>().RequestCulture.Culture.Name; | |
| //iso += ";q=0.9,de;q=0.8,hr;q=0.7,bs;q=0.6"; | |
| context.Response.Headers.Add("Content-Language", iso); | |
| await next(context); | |
| }); | |
| app.UseRouting(); | |
| app.UseEndpoints(c => c.MapDefaultControllerRoute()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment