Marcel
Marcel

Reputation: 15722

How to put HTML code into a .resx resource file?

Strange, that no one asked this before....

I am creating templated HTML emails for 4 languages. I want to put the HTML templates into my .resx files to have easy, internationalized access to them from code. Like so:

.resx file:

<data name="BodyTemplate" xml:space="preserve">
    <value><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <head>
            <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
            <title>Title</title>
            ...
        </body>
        </html>
    </value>
</data>

But, obviously, the compiler complains about the HTML inside the .resx file. Is there a way to use HTML (or XML at all) in .resx files. How?

I am using .NET version 4 and dotLiquid as templating Engine, if that matters.

Upvotes: 9

Views: 21375

Answers (5)

colinD
colinD

Reputation: 2039

Using Blazor (at least Server Prerendered mode, not tested with other), you can create a MarkupString (from Microsoft.AspNetCore.Components namespace) with the localizer value as parameter, or cast it.
The html will be rendered properly.

For example:

@inject IStringLocalizer<Resource> localizer

<span>@(new MarkupString(text))</span> // If assigning localizer value to a string 
<span>@((MarkupString)localizer["TextWithHtml"].Value)</span> // If using localizer value directly, need to use .Value before casting

@code {
    private string text;

    private void SetText()
    {
        text = localizer["TextWithHtml"];
    }
}


Upvotes: 0

Microlang
Microlang

Reputation: 534

This worked form me: Put all your html code in value resource file and then retrieve it using @Html.Raw(...) like I did it here

 <td class="red" colspan="2" align="center">
                    <span style="color: #b01c1c;">
                        <strong>@Html.Raw(Resources.Listino.Cassette</strong>
                    </span>
 </td>

in the Listino resource File: Resource File

Upvotes: 4

Mehmet Taha Meral
Mehmet Taha Meral

Reputation: 3843

Well, I found a way like that:

First I created partial view in multiple languages and put the path of this partial views into .resx files. Next, I called @Html.Partial() method with the Resource string.

Please see the images below:

Views

Resx file

Upvotes: 1

Samuel
Samuel

Reputation: 6490

Suggestion: Create the file you want, name it the way you want, e.g "my_template.html" then add this file to your project.

Click on it, then select in the properties window "Build Action" and set it to "Embedded Resource".

Whenever you want to access this file, you can use something like this (no with proper using block:

    public static string ReadTextResourceFromAssembly(string name)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            return new StreamReader( stream ).ReadToEnd();
        }
    }

Tailor it to your needs. The method above obtains the resource, say you have put your resource in your project MyProject in a subdirectory "HtmlTemplates and called it "my_template.html", then you can access it by the name MyProject.HtmlTemplates.my_template.html

You can then write it out to file or use it directly, etc.

This has some major benefits: You see your html file in your project, it has the html extension, so editing it in Visual Studio has syntax highlighting and all tools applied to .html files.

I have a bunch of those methods for my unit tests, this one extracts the data to a file:

    public static void WriteResourceToFile(string name, string destination)
    {
        using ( var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( name ) )
        {
            if ( stream == null )
            {
                throw new ArgumentException( string.Format( "Resource '{0}' not found", name), "name" );
            }

            using ( var fs = new FileStream( destination, FileMode.Create ) )
            {
                stream.CopyTo( fs );
            }
        }
    }

Upvotes: 16

Earth
Earth

Reputation: 3571

Put encoded html in the .resx file and then getting back the html using

@Html.Raw(Server.HtmlDecode(...resource...));

Upvotes: 7

Related Questions