Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Template Save as Unicode

I use Template - Generator architect as Oleg Sych said to generate some code by T4, but there is some problems in using Unicode, I have some Persian characters after generate all of the Persian characters displayed as question mark.

This is my files: Template:

<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ import namespace="Microsoft.SqlServer.Management.Smo" #>
<#@ import namespace="System.Collections.Generic" #>
<#+
public class DALTable : Template
{

    public override string TransformText()
    {

        //My Template

    }

Generator:

<#@ assembly name="Microsoft.SqlServer.ConnectionInfo" #>
<#@ assembly name="Microsoft.SqlServer.Smo" #>
<#@ assembly name="Microsoft.SqlServer.Management.Sdk.Sfc" #>
<#@ include file="DALTable.tt" #>
<#+
public class TableDALGenerator : Generator
{
    public DALTable DALTableTemplate = new DALTable();

    protected override void RunCore()
{
        this.DALTableTemplate.Table = table;
        this.DALTableTemplate.RenderToFile("DAL.cs");
    }
 }

And Script:

<#@ template language="C#v4" hostspecific="True" debug="True" #>
<#@ assembly name="System.Core" #>
<#@ output extension=".cs" encoding="UTF8" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="TableDALGenerator.tt" #>
<#
    TableDALGenerator TableDALGen = new TableDALGenerator();
    //Pass Parameters
    TableORMGen.Run();
#>

As Oleg Sych said I set Unicode in output as you see in this line: <#@ output extension=".cs" encoding="UTF8" #> also I save as all of this 3 files with Unicode utf8 but the problem still remain, where is the problem? what another thing remain that I must do that?

Update

I found that none of my new generated files (DAL.cs) not saved as UTF8 all of them saved by ANSI encoding. what need to do to save my files by UTF8 encoding?

like image 582
Saeid Avatar asked Sep 06 '25 03:09

Saeid


2 Answers

As per MSDN (https://msdn.microsoft.com/en-us/library/gg586943.aspx), to do this in the output element, you need to say encoding="utf-8", so with the hyphen (the letter casing does not appear to make a difference).

like image 91
JvS Avatar answered Sep 07 '25 21:09

JvS


The <#@output directive applies to the direct output of the T4 file. You need to alter your template so its output is UTF8

public class DALTable : Template 
{ 
    public DALTable()
    {
        Output.Encoding = System.Text.Encoding.UTF8;
    } 

    public override string TransformText() 
    ...
like image 34
podiluska Avatar answered Sep 07 '25 22:09

podiluska