Search code examples
c#asp.net-coreareaculture

multi cultures for specific one area in multi area project


I have a project and this has multi areas I want to apply culture in just one area. I use culture but this has side effects on another area do you have any idea for solve problem

for example: double in ajax requests converted to a string changed money type in another area


Solution

  • You set the culture manually on request based on the route of the specific area:

    public class CustomRouteDataRequestCultureProvider : RequestCultureProvider
    {
        public int IndexOfCulture;
    
        public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
        {
            if (httpContext == null)
                throw new ArgumentNullException(nameof(httpContext));
    
            ProviderCultureResult providerResultCulture;
    
            if (httpContext.Request.Path.Value.Contains("your-area-path"))
                providerResultCulture = new ProviderCultureResult("en");
            else
                providerResultCulture = new ProviderCultureResult("fa");
    
            return Task.FromResult(providerResultCulture);
        }
    }
    

    And in Startup class:

    services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new CultureInfo[]
        {
            new CultureInfo("fa"),
            new CultureInfo("en")
        };
    
        options.DefaultRequestCulture = new RequestCulture("fa");
        options.SupportedCultures = supportedCultures;
        options.SupportedUICultures = supportedCultures;
    
        options.RequestCultureProviders = new[] { new CustomRouteDataRequestCultureProvider { IndexOfCulture = 0 } };
    });