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.
Globalization is the process of designing and developing applications that function for multiple cultures. Localization is the process of customizing your application for a given culture and locale.
Introduction. The i18n library is designed to replace the use of . NET resources in favor of an easier, globally recognized standard for localizing ASP. NET-based web applications.
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:
public static string Localize(string error) {
switch (error) {
case "<english error>": return "<localized version";
}
}
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.
//Original method
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
//My method
private void AddLocalizedErrors(IdentityResult result, ApplicationUser user)
{
foreach (var error in result.Errors)
{
var localizedError = error;
string userName = "";
string email = "";
if (user != null)
{
userName = user.UserName;
email = user.Email;
}
//password errors
localizedError = localizedError.Replace("Passwords must have at least one uppercase ('A'-'Z').", AspNetValidationMessages.password_uppercase);
localizedError = localizedError.Replace("Passwords must have at least one digit ('0'-'9').", AspNetValidationMessages.password_digit);
localizedError = localizedError.Replace("Passwords must have at least one lowercase ('a'-'z').", AspNetValidationMessages.password_lowercase);
localizedError = localizedError.Replace("Passwords must have at least one non letter or digit character.", AspNetValidationMessages.password_nonletter_nondigit);
localizedError = localizedError.Replace("Passwords must have at least one non letter or digit character.", AspNetValidationMessages.password_nonletter_nondigit);
localizedError = localizedError.Replace("Passwords must have at least one non letter or digit character.", AspNetValidationMessages.password_nonletter_nondigit);
//register errors
localizedError = localizedError.Replace("Name "+userName+" is already taken.", AspNetValidationMessages.name_taken.Replace("{0}", userName));
localizedError = localizedError.Replace("Email '" + email + "' is already taken.", AspNetValidationMessages.email_taken.Replace("{0}", email));
ModelState.AddModelError("", localizedError);
}
}
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With