5
Vote

How to specify the encoding of the generated files

description

I have problems with umlauts in my generated views (compile errors).

The files are generated as ANSI, if I convert them to UTF-8, everything works fine.

Is it possible to add a command line parameter to specify the encoding?

comments

mahmoud690 wrote Aug 17, 2011 at 7:07 AM

a work-around :
1st : change the scaffolder to customer view templates (so you have all T4 temaplates in a folder in your project)
2nd: open the T4 template file and change the following line

<#@ Output extension="cshtml" encoding="UTF8"#>

to this line

<#@ Output extension="cshtml" #>

hope to be helpful

TobiasSchittkowsi wrote Aug 17, 2011 at 8:10 AM

Thank you for your help.
I tried both ways and set
<#@ Output extension="vbhtml" encoding="UTF8" #>
in my Index.vb.t4 in my project.

This file has "UTF-8" encoding and the resulting file has a "UTF-8 without BOM" encoding -> I get a compile error.

If I set the encoding of the resulting file to just "UTF-8" manually, everything works...

mahmoud690 wrote Aug 21, 2011 at 10:04 AM

yes I hvae the same Issue,
I should open the file and re-save it with "Unicode(UTF-8 with signature) - Codepage 65001" encoding.
any better solution?

mahmoud690 wrote Aug 21, 2011 at 1:37 PM

I found a work-around finally.
the work-around is through power shell script. You can re-write the file(s) after generation by this script :

Set-Content -Encoding UTF8 -Path $fullPath -value (Get-Content -Path $fullPath -Encoding UTF8)


hope to be helpful

TobiasSchittkowsi wrote Aug 22, 2011 at 5:47 AM

Hi Mahmoud,

thank you for your tip, I solved my problem by re-encoding all files after the scaffolding:

Foreach ($file in Get-Childitem "XXX\Areas\Fahrzeuge\Views" -recurse -force)
{
    $name = $file.fullname
    If ($file.extension -eq ".vbhtml")
        {
        $content=Get-Content -Encoding "UTF8" -Path "$name"
        Write-Host "$name Saving content"
        Out-File -FilePath "$name" -Encoding "UTF8" -InputObject $content
        }
}