How to do Scaffolding in ASP.Net MVC with texts and resources in pt-BR?

4

How to make the text generated by Scaffolding of ASP.Net MVC be texts in pt-BR?

Note: You are not using ASP.NET MVC en-US features. Features only translate automatically generated messages, and are not part of Scaffolding .

    
asked by anonymous 01.07.2016 / 02:59

1 answer

3

I'll need this answer for continue the hook .

Considering that 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 are the error messages. ErrorMessageResourceName or Name in the sample attributes are the index columns of each Resource file. ResourceType indicates what kind of Resource we are referring to.

You do not need to use the same Resource file for everything. In this case, I think it's cool to do one for the Model fields and one for the validation messages.

Once this is done, let's finally talk about Scaffolding .

I've separated some answers where I say that:

So we know that the templates files 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, CodeTemplates/Scaffolders directory 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 CodeTemplates directory of the project.

If CodeTemplates and Scaffolders within it does not exist, create it.

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

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

So you can simply translate messages or create Resources and also use them for Views .

If nothing was missing, I think that's it.

    
01.07.2016 / 06:42