Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

c# - 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);

        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"));
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...