Mike de Klerk Mike de Klerk - 1 year ago 158
C# Question

Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,

I'm going mad. I just want the culture used in the entire Asp.net core application to be set to "en-US". But nothing seems to work. Where to I set the culture for the entire application? I'm not interested in client browser cultures and what not. The only thing that seems to change it is changing the language settings of Windows. I just want the culture to be determined from within the application itself, not by the client.

What I have tried so far:


  • Set
    <system.web><globalization uiCulture="en" culture="en-US" /></system.web>
    in web.config

  • Set
    System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
    and
    CurrentUICulture
    in Startup.Configure and even in the controller.

  • Use
    app.UseRequestLocalization(..
    as shown below

    var enUsCulture = new CultureInfo("en-US");
    var localizationOptions = new RequestLocalizationOptions()
    {
    SupportedCultures = new List<CultureInfo>()
    {
    enUsCulture
    },
    SupportedUICultures = new List<CultureInfo>()
    {
    enUsCulture
    },
    DefaultRequestCulture = new RequestCulture(enUsCulture),
    FallBackToParentCultures = false,
    FallBackToParentUICultures = false,
    RequestCultureProviders = null
    };

    app.UseRequestLocalization(localizationOptions);



But nothing seems to change the CurrencyDecimalSeparator from (nl-NL) , to (en-US).

How can the culture be set?

EDIT:
@soren
This is how the configure method looks like. I've put a breakpoint on
DetermineProviderCultureResult
but it is never hit while visiting the website.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, FinOsDbContext context)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();

app.UseIdentity();

// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

//TODO: Clean up
//var cultureInfo = new CultureInfo("en-US");
//System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
//System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo;

app.UseRequestLocalization();

// UseCookieAuthentication..
// UseJwtBearerAuthentication..

//add userculture provider for authenticated user
var requestOpt = new RequestLocalizationOptions();
requestOpt.SupportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US")
};
requestOpt.SupportedUICultures = new List<CultureInfo>
{
new CultureInfo("en-US")
};
requestOpt.RequestCultureProviders.Clear();
requestOpt.RequestCultureProviders.Add(new SingleCultureProvider());
app.UseRequestLocalization(requestOpt);

app.UseRequestLocalization(requestOpt);


FinOsDbContext.Initialize(context);
FinOsDbContext.CreateTestData(context);
}

public class SingleCultureProvider : IRequestCultureProvider
{
public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
return Task.Run(() => new ProviderCultureResult("en-US", "en-US"));
}
}

Answer Source

This is what solves it for me:

Setting the following in StartUp.Configure

        var cultureInfo = new CultureInfo("en-US");
        cultureInfo.NumberFormat.CurrencySymbol = "€";

        CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
        CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download