Is it possible to use latin character in comments of t4 template?

拈花ヽ惹草 提交于 2020-04-18 07:06:39

问题


I'm trying to create a portuguese comments in a t4 template of the Add Controller in my project.

The comments in t4 template:

/// <summary>
/// Controller que trata as requisições de pesquisa, inclusão, alteração e exclusão de <#= mvcHost.ControllerName.Replace("Controller", "") #>.
/// </summary>

The problem is when i add a controller, the comments appear like this:

/// <summary>
/// Controller que trata as requisi��es de pesquisa, inclus�o, altera��o e exclus�o de Teste.
/// </summary>

Obs: The square � is the black "? diamond" symbol.

So, how can i use latin characteres in a t4 template?

My Template:

<#@ template language="C#" HostSpecific="True" #>
<#@ output extension=".cs" encoding="UTF-8" #> 
<#
MvcTextTemplateHost mvcHost = (MvcTextTemplateHost)(Host);
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace <#= mvcHost.Namespace #>
{
    /// <summary>
    /// Controller que trata as requisições de pesquisa, inclusão, alteração e exclusão de <#= mvcHost.ControllerName.Replace("Controller", "") #>.
    /// </summary>
    public class <#= mvcHost.ControllerName #> : Controller
    {
         // The class code
    }
}

回答1:


Try to specify the encoding explicitly for the output of your templates:

<#@ output extension=".cs" encoding="UTF-8" #>

Edit: Your code works correctly on mine (after removing the mvcHost parts).

Try resaving both your template and its output file as UTF-8 by performing this procedure for each:

  1. Open it in Visual Studio
  2. Click File, Save As…
  3. Choose Save with Encoding… (see below)
  4. Make sure that UTF-8 is selected
  5. Click OK

enter image description hereenter image description here




回答2:


Try to write the characters like this

inclus&atilde;o

See a table of HTML codes here.



来源:https://stackoverflow.com/questions/9008611/is-it-possible-to-use-latin-character-in-comments-of-t4-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!