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 ent
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;
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.
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;
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.