Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 5 translate IdentityResult

I rephrase another question I asked earlier.

I have a basic C# Asp.net MVC 5 application. I create an account through the following code (provided by the template in Vs 2013)

protected async Task<bool> CreateAccountAndAddRoleAsync(UserManager<ApplicationUser> userManager, ApplicationUser user, string password, string role)
{
    var result = await userManager.CreateAsync(user, password);

     if (result.Succeeded)
     {
          try
          {
              var roleResult = await userManager.AddToRoleAsync(user.Id, role);
              if (roleResult.Succeeded)
              {
                   //await SignInAsync(userManager, user, isPersistent: false);
                   return true;
              }
           }
           catch (Exception e)
           {
               Console.WriteLine(e);
              throw;
           }
     }
     AddErrors(result);
     return false;
}

protected void AddErrors(IdentityResult result)
{
   foreach (var error in result.Errors)
   {
       ModelState.AddModelError("", error);
   }
}

The problem is that when the username is already taken (or any other error message), the messages are in English. Such as

Name xxx is already taken

Instead of

L'identifiant xxx est déjà utilisé

The problem is that the message is provided by the framework with

var result = await userManager.CreateAsync(user, password);

And the AddErrors(result); enumerates the messages (in english) to display them to the user. How can I modify the messages?

I changed the culture in the webconfig file with

<globalization uiCulture="fr-FR" culture="fr-FR" />

But I still have an English message. In fact, the real problem is that I can't change the message provided by the framework even if I wanted another message in English.

Does anyone know how to change the message provided by the IdentityResult ?

like image 731
Daniel Avatar asked Oct 02 '14 14:10

Daniel


1 Answers

Do not forget to install your language package from nuget for example:

Install-Package Microsoft.AspNet.Identity.Core.ru
like image 142
Dovlet Mamenov Avatar answered Oct 22 '22 11:10

Dovlet Mamenov