I have a method that grabs the localization of a given string and returns it. This method is called by an extension method of HtmlHelper, so that it is accessible from Razor views. However, sometimes we also use it in controllers and other non-Razor places, and that's why we have the "inner" method.

Now, for debug purposes, I'd like to wrap the returned, localized (or maybe not) string into some HTML (something like <span class="localized">Returned text</span>). However, if I write

return string.Format("<span class=\"localized\">{0}</span>", text);

then the rendered page includes the < and > characters as if they were HTML-encoded, so the output is bogus.

I can't, of course, call HtmlHelper.Raw(string s), partly because I wouldn't know how to create an HtmlHelper from outside an MVC controller, and then because it returns an IHtmlString which isn't assignable to string.

If I call HttpUtility.HtmlEncode(string s), the text is twice-encoded, and the rendered page shows &lt;span class&eq;&quot;....

What should I do to have a string that renders to HTML in the view?


EDIT

Here's the code, from a Helper static class (all extension methods work, except they behave wrong):

public static string localize(string textId)
{
    string ret = "";

    // Query to obtain the localized string, 
    //   or to obtain a fallback in case there are issues 
    //   (missing localization, wrong id...)
    // In the end, ret is filled with the output string

#if !OFFICIAL
    ret = string.Format("<span class=\"localized\" title=\"{1}\">{0}</span>", ret, expression);
#endif

    return ret;
}

public static string localize(this HtmlHelper htmlhelper, string expression)
{
    return trad(textId);
}

Related posts

Recent Viewed