title: Asp.net Mvc, Razor and Localization
body: <p>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.</p>
  
  <p>Localization in .net, and in mvc, is made in 2 ways that can even be mixed together:</p>
  
  <ul>
  <li>Resource files (both local or global)</li>
  <li>Localized views with a viewengine to call the appropriate view based on culture</li>
  </ul>
  
  <p>I'll explain the solutions I tried and all the problems I got with every one of them.</p>
  
  <p><strong>Text in resource files, all tags in the view</strong></p>
  
  <p>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].</p>
  
  <p>Pros:</p>
  
  <ul>
  <li>Clean separation, no structure whatsoever in localization.</li>
  <li>Easy encoding: everything that is returned from the resource gets html encoded.</li>
  </ul>
  
  <p>Cons:</p>
  
  <ul>
  <li>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.</li>
  <li>For the same reason as above, if in two different languages the [strong] text is in different places (like "Il <strong>cane</strong> di Marco" and "Marcos's <strong>dog</strong>"), I can't achieve it, since all my tags are in the view.</li>
  </ul>
  
  <p><strong>Text and inline tags in resource files, through parameters</strong></p>
  
  <p>This method will have the resources contain some placeholders for string.Format, and those placeholders will be filled with inline tags post-encoding.</p>
  
  <p>Pros:</p>
  
  <ul>
  <li>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</li>
  </ul>
  
  <p>Cons:</p>
  
  <ul>
  <li>Encoding is a bit harder, I have to pre-encode the value from the resource, then use string.Format, and finally return it as MvcHtmlString to tell the view engine to not re-encode it when displaying.</li>
  <li>For the same reason as above, including, for instance, an ActionLink as parameter would be troublesome. Let's say I get the text for my actionlink from a resource. My method already encodes it. But then, the ActionLink method would re-encode it again. I would need a distinct method to get resources without encoding them, or new helper methods that get an MvcHtmlString instead of a string as text parameter, but both are rather unpractical.</li>
  <li>Still takes a whole lot of time to build views, having to create all the resource keys and then fill them.</li>
  </ul>
  
  <p><strong>Localized views</strong></p>
  
  <p>Pros:</p>
  
  <ul>
  <li>All views are plain html. No resources to read.</li>
  </ul>
  
  <p>Cons:</p>
  
  <ul>
  <li>Duplicated html everywhere. I don't even need to explain how this is totally evil.</li>
  <li>Have to manually encode all troublesome characters like grave vowels, quotes and such.</li>
  </ul>
  
  <p><strong>Conclusions</strong></p>
  
  <p>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".</p>
  
  <p>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).</p>
  
  <p>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?</p>