turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 155 forks source link

Translate Password error messages? #405

Closed mrsuau627 closed 3 years ago

mrsuau627 commented 3 years ago

I'm in the process of translating an ASP.net MVC app using this framework and just came across my first translation scenario that I can't figure out.

We have an IdentityConfig that creates an ApplicationUserManager and sets the PasswordValidator object with our password requirements. For example:

            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

Now, when a user goes to change their password, we call the UserManager.ChangePasswordAsync function. If they don't meet certain password requirements, that function returns errors such as "Passwords must be at least 6 characters. Passwords must have at least one digit ('0'-'9').". What would be the best way to translate these error messages?

turquoiseowl commented 3 years ago

If the ChangePasswordAsync method doesn't support localization, it's not going to be easy.

If it does however, then presumably it will either take a CultureInfo parameter or it will detect the ambient setting (Thread.CurrentThread.CurrentUICulture) which i18n should be setting automatically for you (by default).

mrsuau627 commented 3 years ago

It doesn't appear to support localization. Would my best bet be to trap the various error messages that are coming back and replace them with the brackets around them? Something like:

            switch (errorMessage)
            {
                case "Password must contain 6 characters.":
                    return "[[[Password must contain 6 characters.]]]";
                ... etc
            }

I've done something similar in a similar scenario, but was hoping that someone had already come across this issue before because it seems like it would be common if using Identity for authentication in an ASP.NET app.

turquoiseowl commented 3 years ago

Yes, I would agree, that is your best bet unfortunately.

mrsuau627 commented 3 years ago

I did end up taking this route and it worked out just fine. Thanks!