How to do Scaffolding in ASP.Net MVC with text and resources in en-BR?

Asked

Viewed 238 times

4

How to do the texts generated by Scaffolding of ASP.Net MVC to be texts in?

Note: It is not using ASP.NET MVC resources en-BR. Resources only translate automatically generated messages, and are not part of the Scaffolding.

  • @diegofm the other question was myself I asked, and it is not duplicated, one is scaffolding and the other is automatically generated resources.

1 answer

3


I’m gonna need this answer to continue the hook.

Considering you did everything that was in the link response, let’s set up a Model:

public class CustomUser
{
    [Key]
    public Guid CustomUserId { get; set; }

    [Display(Name = "UserName", ResourceType = typeof(Resources.Language))]
    [Required(ErrorMessageResourceName = "UserNameIsRequired", ErrorMessageResourceType = typeof(Resources.Language))]
    public String UserName { get; set; }
    [Display(Name = "FirstName", ResourceType = typeof(Resources.Language))]
    [Required(ErrorMessageResourceName = "FirstNameIsRequired", ErrorMessageResourceType = typeof(Resources.Language))]
    public String FirstName { get; set; }
    [Display(Name = "LastName", ResourceType = typeof(Resources.Language))]
    [Required(ErrorMessageResourceName = "LastNameIsRequired", ErrorMessageResourceType = typeof(Resources.Language))]
    public String LastName { get; set; }
    [Display(Name = "Points", ResourceType = typeof(Resources.Language))]
    [DefaultValue(0)]
    [Range(0, 100, ErrorMessageResourceName = "PointsOutsideRange", ErrorMessageResourceType = typeof(Resources.Language))]
    public int Points { get; set; }
    [Display(Name = "Suspended", ResourceType = typeof(Resources.Language))]
    [DefaultValue(false)]
    public Boolean Suspended { get; set; }

    ...
}

This is the most important part of the translations, because here go the error messages. ErrorMessageResourceName or Name in the example attributes are the index columns of each file of Resource. ResourceType indicates what type of Resource we are referring to.

You don’t need to use the same file from Resource for everything. In this case, I think it’s nice to make one for the fields of Model and another for validation messages.

Once that’s done, let’s finally talk about Scaffolding.

I’ve sorted out some answers where I talk about it:

So we know that the templates are in the following directories:

  • diretório do seu projeto\packages\MvcScaffolding.VS2015.<versão>\tools\Views for Mvcscaffolding. Copy the directories from there into your project, directory CodeTemplates/Scaffolders and rename them as follows:
    • Action -> MvcScaffolding.Action
    • ActionUnitTest -> MvcScaffolding.ActionUnitTest
    • ActionWithUnitTest -> MvcScaffolding.ActionWithUnitTest
    • AspxView -> MvcScaffolding.AspxView
    • Controller -> MvcScaffolding.Controller
    • RazorView -> MvcScaffolding.RazorView
    • Views -> MvcScaffolding.Views
  • C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates. Copy all directories to the root of your directory CodeTemplates of the project.

If CodeTemplates and Scaffolders within it do not exist, create them.

You will need to install this tool here to be able to edit templates. These are T4 templates that can be freely edited by you.

Start with directories RazorView and MvcView, respectively. This is where the Views.

Therefore, you can simply translate the messages or create Resources and use them also for Views.

If there’s nothing missing, I guess that’s it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.