将Asp.net Core中的CultureInfo设置为有一个.作为CurrencyDecimalSeparator而不是, [英] Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,

查看:346
本文介绍了将Asp.net Core中的CultureInfo设置为有一个.作为CurrencyDecimalSeparator而不是,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要疯了.我只希望将整个Asp.net核心应用程序中使用的区域性设置为"en-US".但是似乎没有任何作用.我在哪里设置整个应用程序的区域性?我对客户端浏览器文化不感兴趣,而对不感兴趣.似乎唯一改变的是更改Windows的语言设置.我只希望从应用程序内部而不是由客户端确定文化.

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:

  • 在web.config中设置<system.web><globalization uiCulture="en" culture="en-US" /></system.web>
  • 在Startup.Configure甚至在控制器中设置System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;CurrentUICulture.
  • 按如下所示使用app.UseRequestLocalization(..

  • 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);

但是似乎没有任何改变将CurrencyDecimalSeparator从(nl-NL)更改为(en-US).

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

如何设置文化?

@soren 这就是configure方法的样子.我在DetermineProviderCultureResult上设置了一个断点,但是在访问该网站时从未遇到该断点.

@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"));
        }
    }

推荐答案

这是为我解决的问题:

StartUp.Configure

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

CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

这篇关于将Asp.net Core中的CultureInfo设置为有一个.作为CurrencyDecimalSeparator而不是,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆