I have a localized .Net Core 3 app. My resources are in a separate assembly in .resx files. The localization is set up to use cookies like this:
var cultureProvider = new CookieRequestCultureProvider();
cultureProvider.CookieName = "MyCultureCookie";
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("de"),
SupportedCultures = StaticData.SupportedCultures,
SupportedUICultures = StaticData.SupportedCultures,
RequestCultureProviders = new List<IRequestCultureProvider>
{
cultureProvider,
new AcceptLanguageHeaderRequestCultureProvider()
}
};
app.UseRequestLocalization(localizationOptions);
and to localize validation messages:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddControllersWithViews()
.AddDataAnnotationsLocalization(options =>
{
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(MyLocalizationDll));
})
.AddViewLocalization()
.AddRazorRuntimeCompilation();
services.AddMvc()
.SetupModelBindingLocalization(services)
.AddViewLocalization();
The input in my view:
<input type="number" class="form-control" min="0" max="100" step="0.1" />
It works well, I can switch the language and all page texts including almost all data validation error messages will be translated, numbers are shown in correct format (e.g. 1.00 for "en" and 1,00 for "de").
But the problem is: when I have a decimal number like 1,5 in german culture and I submit my form, then it comes as 15 in my controller. Only when I switch the language to "en" I get the right number, because the decimal separator is point as expected. And I also get some of the validation messages for this field in other language. Just can't figure out what is here the problem...
Custom model binder may provide a workaround for the issue, but if you need a better approach you have to install a set of cldr scripts for number/date/currency/etc validation.
Option 1: Manually install cldr scripts
wwwroot/lib/cldr-data/package.json
file and add below code before the last closing paranthes:Option 2: Use LocalizationValidationScripts taghelper
This tag helper provides the above solution in a simple way.
Sources: