将Asp.net Core中的CultureInfo设置为.作为CurrencyDecimalSeparator而不是,
本文关键字:CurrencyDecimalSeparator 作为 设置 net Asp Core 中的 CultureInfo | 更新日期: 2023-09-27 18:16:25
我要疯了。我只是希望在整个Asp.net核心应用程序中使用的文化设置为"en-US"。但似乎什么都不起作用。在哪里为整个应用程序设置区域性?我对客户端浏览器文化等不感兴趣。唯一能改变的似乎是改变Windows的语言设置。我只是希望文化由应用程序本身决定,而不是由客户端决定。
我已经试过了:
- 在web.config 设置
- 在"Startup"中设置"
System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo;
"answers"CurrentUICulture
"。在控制器中配置和even。 使用
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);
<system.web><globalization uiCulture="en" culture="en-US" /></system.web>
但是似乎没有改变CurrencyDecimalSeparator从(nl-NL)到(en-US)。
如何设置区域性?
编辑:@soren这就是configure方法的样子。我在DetermineProviderCultureResult
上设置了一个断点,但在访问网站时它从未被击中。
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;
有点晚了,但这是我的工作:
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,
}
});
您的代码看起来一切正常。问题是你对
的调用app.UseRequestLocalization(...);
需要在调用
之前发生app.UseMvc();
你的断点永远不会被击中的原因是它从来没有走到那么远。UseMVC完成请求并返回结果。请记住,中间件是按顺序发生的,任何一个中间件都可能使进程短路并停止进一步的处理。
本地化在Startup中配置。ConfigureServices 方法:
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()
};
});
启动。方法配置
app.UseRequestLocalization();
则UseRequestLocalization
初始化RequestLocalizationOptions
对象。这应该至少放在UseMvc调用
改变文化:
[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);
}
当前语言:var currentLanguage = HttpContext.Features.Get<IRequestCultureFeature>().RequestCulture.Culture.Name;
这对我有用
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
只在启动时配置对我不起作用
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()
};
});
我添加了jquery全球化验证插件:然后你需要在jquery- validate - Globalize插件中使用Globalize。看到这里了
下面是一个基于接受的答案和其他答案的快速答案:
var cultureInfo = new CultureInfo("fr-FR");
cultureInfo.NumberFormat.NumberDecimalSeparator = ".";
cultureInfo.NumberFormat.CurrencyDecimalSeparator = ".";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
测试和工作:dotNet 6
PS:写在program.cs上(因为在Asp.net MVC 6上没有更多的Startup.cs文件),确保这行代码在 app.UseRouting();
行