title: Localized views with Razor
body: <p>I was trying to be witty and use a VirtualPathProvider to find localized views. It takes the requested view path and modifies it when checking after the file. It returns a localized virtual file if found:</p>
  
  <pre><code>public pseudoclass MyFileProvider : VirtualPathProvider
  {
  
      bool FileExists(string requestedPath)
      {
          if (IsLocalizedView(requestedPath))
            return true;
         return base.FileExists(requestedPath);
      }
  
      bool IsLocalizedView(string requestedPath)
      {
          var uri = requestedUri.AddLocaleByMagic("sv");
          if (FileExistsInternal(uri))
            return true;
      }
  
      //some more stuff to return the actual file
  }
  </code></pre>
  
  <p>The problem is that I get the following exception:</p>
  
  <blockquote>
    <p>The VirtualPathProvider returned a VirtualFile object with VirtualPath set to '/Views/Shared/_Layout.sv.cshtml' instead of the expected '/Views/Shared/_Layout.cshtml'.</p>
  </blockquote>
  
  <p>Sure, I could fake the file path, but that would produce problems with caching and different localizations. Right?</p>
  
  <p>Anyone got a better way to be able to create localized views? I do not want to use the same view but with resource strings instead. Such views are so horrible that they almost makes me cry because they are so hard to read.</p>
  
  <p>If you still haven't understood what I'm looking for:</p>
  
  <p><em><strong>/Views/User/Details.sv.cshtml</em></strong></p>
  
  <pre><code>Hejsan @Model.FirstName
  
  Detta är en lite rolig text på svenska.
  </code></pre>
  
  <p><em><strong>/Views/User/Details.en.cshtml</em></strong></p>
  
  <pre><code>Hello @Model.FirstName
  
  This is a test on english.
  </code></pre>
  
  <p><em><strong>Controller</em></strong></p>
  
  <pre><code>public ActionResult Details()
  {
    return View(new User()); //should automagically use a swedish or english view
  }
  </code></pre>
  
  <p>I want to be able to switch views (to a localized one using CurrentCulture) without having to do anything manually at each request.</p>