本文介绍了在 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 中设置
  • 在 Startup.Configure 甚至控制器中设置 System.Threading.Thread.CurrentThread.CurrentCulture =cultureInfo;CurrentUICulture.
  • 使用app.UseRequestLocalization(..如下图

    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).

文化如何设置?

@索伦这就是 configure 方法的样子.我在 DetermineProviderCultureResult 上设置了一个断点,但在访问网站时它从未被命中.

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

推荐答案

这就是为我解决的方法:

This is what solves it for me:

StartUp.Configure

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

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

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

10-29 10:27