将 Asp.net 核心中的 CultureInfo 设置为.as CurrencyDecimalPartiator,而不是,

我要疯了。我只是希望在整个 Asp.net 核心应用程序中使用的文化设置为“ en-US”。但似乎没什么效果。将整个应用程序的区域性设置到哪里?我对客户浏览器文化之类的不感兴趣。唯一改变它的似乎是改变 Windows 的语言设置。我只希望从应用程序本身而不是由客户机来确定区域性。

到目前为止我所做的努力:

  • 在 web.config 中设置 <system.web><globalization uiCulture="en" culture="en-US" /></system.web>
  • 在启动时设置 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);
    

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"));
}
}
102819 次浏览

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;

Your code looks all fine. The issue is your call to

app.UseRequestLocalization(...);

Needs to happen before your call to

app.UseMvc();

The reason your breakpoint is never hit is because it never goes that far. UseMVC completes the request and returns the result. Remember, Middleware happens in order and any one of the middleware can shortcircuit the process and halt processing going any further.

A bit late but here is what worked for me:

var defaultDateCulture = "fr-FR";
var ci = new CultureInfo(defaultDateCulture);
ci.NumberFormat.NumberDecimalSeparator = ".";
ci.NumberFormat.CurrencyDecimalSeparator = ".";


// Configure the Localization middleware
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(ci),
SupportedCultures = new List<CultureInfo>
{
ci,
},
SupportedUICultures = new List<CultureInfo>
{
ci,
}
});

this worked for me

 Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);

Localization is configured in the Startup.ConfigureServices method:

CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("fa"),
new CultureInfo("en")
};


services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};


});

Startup.Configure method

 app.UseRequestLocalization();

then UseRequestLocalization initializes a RequestLocalizationOptions object. This should be placed atleast before your UseMvc call

Change Culture:

[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new     RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);


return LocalRedirect(returnUrl);
}

Current language:

var currentLanguage = HttpContext.Features.Get<IRequestCultureFeature>().RequestCulture.Culture.Name;

Only configuring in startup didn't work for me

 services.Configure<RequestLocalizationOptions>(options =>
{
var ksCultureInfo = new CultureInfo("sq");
var enCultureInfo = new CultureInfo("en");
var srCultureInfo = new CultureInfo("sr");


ksCultureInfo.NumberFormat.NumberDecimalSeparator = ".";


var supportedCultures = new[]
{
ksCultureInfo,
enCultureInfo,
srCultureInfo
};


options.DefaultRequestCulture = new RequestCulture(culture: enCultureInfo, uiCulture: ksCultureInfo);
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});

I added jquery globalize validation plugins: Then you need to use Globalize with jquery-validation-globalize plugin. Saw this here

Now it works as expected.

Here is a quick answer based on the accepted one and the others :

var cultureInfo = new CultureInfo("fr-FR");
cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
cultureInfo.NumberFormat.CurrencyDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;

Tested and worked on : dotNet 6
PS : writed on program.cs (because there's no more Startup.cs file on Asp.net MVC 6), ensure that this lines of code are added before app.UserRouting(); line