I've been Following this tutorial "https://www.youtube.com/watch?v=Hy9G30nncMM". with my own modifications.
ConfigureServices:
services.AddLocalization(opt => { opt.ResourcesPath = "Resources"; });
services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
services.AddControllersWithViews();
Configure:
var supportedCultures = new[] {"en","ar"};
var localizationOptions = new RequestLocalizationOptions()
.SetDefaultCulture(supportedCultures[0])
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);
app.UseRequestLocalization(localizationOptions);
Folder Hierarchy:
Result:
Even that i set the default culture to supportedCultures[0] which is "en", the app gets the "ar".
Any Help on this matter would be appreciated. Thanks in Advance.
By default Asp.Net Core uses below RequestCultureProviders
in order to detect the request culture:
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptedLanguageHeaderRequestCultureProvider
So, depending on the supported cultures list provided in startup, it will try to match cultures in the list by the request culture in the providers till it find the first match and respond accordingly.
e.g.:
en
and ar
as supported culture list,ar
as DefaultRequestCulture
?culture=xx
So the localization middleware will first try with query string, but since there is no culture defined there, it will check the next provider CookieRequestCultreProvider
(below is a sample culture cookie for TR
culture.)
and if there is no culture param in the cookie it will check the last culture provider AcceptedLanguageHeaderRequestCultureProvider
which provides a list of accepted cultures.
Below you can see a screenshot of chrome supported cultures list, you may change the order of your browsers cultures and see how it affect the request culture.
If it can't detect the request culture in any of the above providers then it will use the DefaultRequestCulture
that has been defined in RequestLocalizationOptions
in startup.
In most cases we may insert RouteDataRequestCultureProvider
at 0
position to provide culture in the route data, so it will be the first provider to look at during request localization:
services.Configure<RequestLocalizationOptions>(ops =>
{
// ...
ops.AddInitialRequestCultureProvider(new RouteDataRequestCultureProvider());
});
or
services.Configure<RequestLocalizationOptions>(ops =>
{
// ...
ops.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
});