Alex Rivera | Logout

Asp.net Mvc, Razor and Localization

Asked 2011-03-18T17:24:15.680
18

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:

  • Resource files (both local or global)
  • Localized views with a viewengine to call the appropriate view based on culture

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:

  • Clean separation, no structure whatsoever in localization.
  • Easy encoding: everything that is returned from the resource gets html encoded.

Cons:

  • If I have a paragraph with some strongs, a couple of link etc I have to split it in many resource keys. This is considered to make the view too unreadable and also takes too much time to create it.
  • For the same reason as above, if in two different languages the [strong] text is in different places (like "Il cane di Marco" and "Marcos's dog"), I can't achieve it, since all my tags are in the view.

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:

  • Clean separation, with just placeholders in the text, so if I am ever to replace [strong] with [em] I do it in the view where I pass it as parameter and it gets changed in every language

Cons:

  • Encoding is a bit harder, I have to pre-encode the value from the resource, then use string.Format, and finally return it as Mvc
Edit
Report

1 Answer

7

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

answered 2011-03-18T17:48:16.043

Your Answer