Reputation: 57
I have a template asp.net core mvc project and i'd like to set up localization.
What i realized is that if i set up my default cultureinfo then it will always be picked if it's inside the accept-language header regardless of it's position or precedence (q-factor weighting).
So i came up with the idea to not set a default cultureinfo at all so it can depend on the request's header. There's only 1 problem with it. I'm not sure what will happen if the accept-header contains a culture that is not supported by my application.
Is there any way to set up some kind of middleware between a request and response so i can set the default culture if the request contains an unsupported culture?
Here's my relevant part of the code inside startup.cs:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("de"),
new CultureInfo("en"),
new CultureInfo("fr")
};
var localizationOptions = new RequestLocalizationOptions
{
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
app.UseRequestLocalization(localizationOptions);
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Upvotes: 0
Views: 1376
Reputation: 713
.Net Core 3.1 has come with Globalization and Localization Middleware
here. And this is built-in middleware for you Accept-Language.
Therefore, if there are no Accept-Language
, the default Culture
will be Server Culture
. If you want to set default
that you don't sure about Server Culture
, you can write a custom middleware example to force your default Culture
Upvotes: 1