Matteo Mosca
Matteo Mosca

Reputation: 7448

Asp.net Mvc, Razor and Localization

I know this matter has already been brought on these pages many times, but still I haven't found the "good solution" I am required to find. Let's start the explanation.

Localization in .net, and in mvc, is made in 2 ways that can even be mixed together:

I'll explain the solutions I tried and all the problems I got with every one of them.

Text in resource files, all tags in the view

This solution would have me put every text in resources, and every tag in the view, even the inline tags such as [strong] or [span].

Pros:

Cons:

Text and inline tags in resource files, through parameters

This method will have the resources contain some placeholders for string.Format, and those placeholders will be filled with inline tags post-encoding.

Pros:

Cons:

Localized views

Pros:

Cons:

Conclusions

A mix of the above techinques inherits pros and cons, but it's still no good. I am challenged to find a proper productive solution, while all of the above are considered "unpractical" and "time consuming".

To make things worse, I found out that there isn't a single tool that refactors "text" from aspx or cshtml (or even html) views/pages into resources. All the tools out there can refactor System.String instances in code files (.cs or .vb) into resources only (resharper for instance, and a couple of others I can't remember now).

So I'm stuck, can't find anything appropriate on my own, and can't find anything on the web either. Is it possible noone else got challenged with this problem before and found a solution?

Upvotes: 18

Views: 11343

Answers (3)

Bob The Janitor
Bob The Janitor

Reputation: 20802

Have you thought about using localized models, have your view be strongly types to IMyModel and then pass in the appropriately decorated model then you can use/change how your doing your localization fairly easy by modifying the appropriated model.

it's clean, very flexible, and very easy to maintain.

you could start out with Recourse file based localization and then for paces you need to update more often switch that model to a cached DB based localization model.

Upvotes: 0

ChevCast
ChevCast

Reputation: 59234

I personally like the idea of storing inline tags in the resource file. However I do it a little differently. I store very plain tags like <span class='emphasis'>dog</span> and then I use CSS to style the tags appropriately. Now, instead of "passing in" a tag as a parameter, I simply style the span.emphasis rule in my CSS appropriately. Change carries over to all languages.


The Sexier Option:

Another option I thought of and quite enjoy is to use a "readable markup" language like StackOverflow's very own MarkdownSharp. This way you aren't storing any HTML in the resource file, only markdown text. So in your resource you would have **dog** and then it gets shunted through markdown in the view (I created a helper for this, (Usage: Html.Markdown(string text)). Now you're not storing tags, you're storing a common human readable markup language. The markdownsharp source is one .CS file and it's easy to modify. So you could always change the way it renders the ending HTML. This gives you total control over all your resources without storing HTML, and without duplicating views or chunks of HTML.

EDIT

This also gives you control over the encoding. You could easily make sure the content of your resource files contain no valid HTML. Markdown syntax (as you know from using stack overflow) does not contain HTML tags and thus can be encoded without harm. Then you just use your helper to convert the Markdown syntax to valid HTML.

EDIT #2

There is one bug in markdown that I had to fix myself. Anything markdown detects is to be rendered as a "code" block will be HTML encoded. This is a problem if you have already HTML encoded all content being passed to markdown as anything in the code blocks will be essentially re-encoded which turns &gt; into &amp;gt; and completely screws up the text within code blocks. To fix this I modified the markdown.cs file to include a boolean option that stops markdown from encoding text within code blocks. See this issue for the fixed .cs file that I added to the MarkdownSharp project issues.

EDIT #3 - Html Helper Sample

public static class HtmlHelpers
{
    public static MvcHtmlString Markdown(this HtmlHelper helper, string text)
    {
        var markdown = new MarkdownSharp.Markdown
        {
            AutoHyperlink = true,
            EncodeCodeBlocks = false, // This option is my custom option to stop the code block encoding problem.
            LinkEmails = true,
            EncodeProblemUrlCharacters = true
        };
        string html = markdown.Transform(markdownText);
        return MvcHtmlString.Create(html);
    }
}

Upvotes: 7

Max Toro
Max Toro

Reputation: 28618

Nothing stops you from storing HTML in resource files, then calling @Html.Raw(MyResources.Resource).

Upvotes: 2

Related Questions