I'm trying to get localized error messages for Swedish for Asp.Net Identity by using advice from this post: How to localize ASP.NET Identity UserName and Password error messages?
Using NuGet I downloaded the German language pack and then opened \packages\Microsoft.AspNet.Identity.Core.2.0.0\lib\net45\de\Microsoft.AspNet.Identity.Core.resources.dll in dotPeek and then exported this to a new VS project:
https://github.com/nielsbosma/AspNet.Identity.Resources.Swedish/
I've copied the generated \Microsoft.AspNet.Identity.Core.resources.dll to a new folder under \packages\Microsoft.AspNet.Identity.Core.2.0.0\lib\net45\se.
When I run my site locally I see that Microsoft.AspNet.Identity.Core.resources.dll has been copied to MySite\bin\sv\
But I can't get it to work :(
If I set in my Web.config:
<system.web>
...
<globalization culture="sv-SE" uiCulture="sv" />
</system.web>
I still get english default error messages. But if I change to german that I've included from NuGet I get german error messages.
Using dotPeek I've compared my dll with the german and they are the same except my has PublicKeyToken=null and the one for german is "31bf3856ad364e35". Could this be why I can't get my dll to load? Is there anyway to set a PublicKeyToken for a dll? Any workaround?
Thanks for any pointers.
At the moment this is a really crappy solution, but a workaround solution none the less. To save time for those of us who do need localization and don't work at Microsoft... here's what I did as a workaround for the dutch language.
Yet another workaround for
Password
field could be to implementRegisterViewModel.Password
with aCustomValidationAttribute
:And to have
CustomValidations.ValidatePassword
method which would mimic the password validation rules that you've set up forPasswordValidator
. I.e.:Here you can obviously localize your error messages per you personal preference using standard resx resource files. So, to sum it up, you would simply prohibit any invalid passwords from ever reaching the
PasswordValidator
.For
Email
field this would get a little bit ugly as it would require a roundtrip to DB to verify uniqueness etc., but it should be doable.Extra bonus for using this solution is that the errors produced would be "per field", i.e. you would not have to display all the validation errors using
@Html.ValidationSummary
but could do:Here is the en-US Resources.resx file key and value list. These are the values that require localization. Source code at http://aspnetidentity.codeplex.com
Asp.Net Identity {name}Validator.cs ErrorMessage Resources.resx Localization See: http://aspnetidentity.codeplex.com/discussions/638351
Not unless you have the private key that Microsoft uses to sign dlls.
Updated: as a workaround until we add support for plugging in your own resources, you can probably just wrap all the default identity result error messages with an explicit switch for now, there should only be about 10-20 user facing errors.
Something like:
Inspired by Peter's Answer I came up with crappy but quick solution as well.
I needed to localize errors in default AccountController template so I wrote my own
AddLocalizedErrors
method. I'm using Resources to localize errors.I am using
string.Replace()
because for example the password errors are just joined strings of a single password requirements.When it comes to collection of roles or so I should be more creative. Probably using
string.Contains()
.Another option is to inherit from Microsoft.AspNet.Identity.PasswordValidator then override ValidateAsync.
You can then use your own resource files for localizaion. The original resource file for English can be found using DotPeek or similar, then you can use that for the English as a template for your own translation in other languages.
My resx files:
MyLocalization/IdentityResource.resx (same as in Microsoft.AspNet.Identity.Core.Resources)
MyLocalization/IdentityResource.nb-no.resx (my Norwegian translations)
In MyCustomPasswordValidator.cs: (please also notice the bugfix I also had to do in ValidateAsync)
`