Note that there are some explanatory texts on larger screens.

plurals
  1. POLocalized ViewEngines using multiple routes
    primarykey
    data
    text
    <p>I am trying to make a simple multi-language website. Overriding FindView to get the file based on the language is easy, my problem comes in the routing part.</p> <p>I would like to do is: (have a website with 2 languages - pt-br and en-us, let abbreviate to onlu pt and en - the default would be PT)</p> <p>User types www.mysite.com -> would find the languages in the user header request and if the user doesn´t have any of them, he would be redirected to the default PT, so the end result would be www.mysite.com in Portuguese. If he has en, he would be redirected to www.mysite.com/en/ , the result would be the view in English.</p> <p>If the user types www.mysite.com/pt/ -> I would check that he wants the default and would redirect to www.mysite.com in Portuguese.</p> <p>I did a custom engine to get the correct view based on the language. But it´s not working 100% because the routing problem.</p> <p>IN my case I am trying to route with </p> <pre><code>routes.MapRoute( "Localization", // Route name "{lang}/{controller}/{action}", // URL with parameters new { lang = "pt", controller = "Home", action = "Index" } //defaults ); </code></pre> <p>But with this, when someone types /pt/ will not redirect to the root of the site.</p> <p>Another problem with the route is that I don´t want to anyone typing the controller name, I just want the action, my website just have one Home controller with few actions.</p> <p>Another problem is the that I want a different action name, like, CONTACT in English and CONTATO in Portuguese, they should appear on the address bar like www.mysite.com/Contato or www.mysite.com/en/Contact</p> <p>Mu Engine I did base on this solution <a href="http://www.counity.at/blog/2012/asp-net-mvc3-localization-using-culture-dependent-views/" rel="nofollow">http://www.counity.at/blog/2012/asp-net-mvc3-localization-using-culture-dependent-views/</a> by <a href="http://www.counity.at/blog/author/breigo/" rel="nofollow">Guido Breitenhuber</a></p> <p>Here is the code with some tweaks (it´s not working 100% yet)</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Globalization; namespace Ala.MultiLanguage { public class LocalizedViewLocation : RazorViewEngine { private static readonly string[] _emptyLocations = new string[0]; public string[] LocalizedViewLocationFormats { get; set; } public string[] LocalizedMasterLocationFormats { get; set; } protected string[] LocalizedPartialViewLocationFormats { get; set; } public LocalizedViewLocation() { // Define the localized view locations // 0: Language // 1: View name // 2: Controller name LocalizedViewLocationFormats = new[] { "~/Views/{0}/{2}/{1}.cshtml", "~/Views/Shared/{0}/{1}.cshtml"}; MasterLocationFormats = new[] { "~/Views/{0}/{2}/{1}.cshtml", "~/Views/Shared/{0}/{1}.cshtml"}; LocalizedPartialViewLocationFormats = new[] { "~/Views/{0}/{2}/{1}.cshtml", "~/Views/Shared/{0}/{1}.cshtml"}; } public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) { if (controllerContext == null) throw new ArgumentNullException("controllerContext"); if (String.IsNullOrEmpty(partialViewName)) throw new ArgumentException("Parameter partialViewName is null or empty.", "partialViewName"); string[] searched; var controllerName = controllerContext.RouteData.GetRequiredString("controller"); var partialPath = GetPath(controllerContext, LocalizedPartialViewLocationFormats, partialViewName, controllerName, out searched); if (String.IsNullOrEmpty(partialPath)) { var baseRes = base.FindPartialView(controllerContext, partialViewName, useCache); if (baseRes.View != null) return baseRes; return new ViewEngineResult(searched.Union(baseRes.SearchedLocations)); } return new ViewEngineResult(CreatePartialView(controllerContext, partialPath), this); } public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) { if (controllerContext == null) throw new ArgumentNullException("controllerContext"); if (String.IsNullOrEmpty(viewName)) throw new ArgumentException("Parameter viewName is null or empty.", "viewName"); string[] viewLocationsSearched; string[] masterLocationsSearched; var controllerName = controllerContext.RouteData.GetRequiredString("controller"); var viewPath = GetPath(controllerContext, LocalizedViewLocationFormats, viewName, controllerName, out viewLocationsSearched); var masterPath = GetPath(controllerContext, LocalizedMasterLocationFormats, masterName, controllerName, out masterLocationsSearched); if (String.IsNullOrEmpty(viewPath) || (String.IsNullOrEmpty(masterPath) &amp;&amp; !String.IsNullOrEmpty(masterName))) { var baseRes = base.FindView(controllerContext, viewName, masterName, useCache); if (baseRes.View != null) return baseRes; return new ViewEngineResult(viewLocationsSearched.Union(masterLocationsSearched).Union(baseRes.SearchedLocations)); } return new ViewEngineResult(CreateView(controllerContext, viewPath, masterPath), this); } private string GetPath(ControllerContext controllerContext, string[] locations, string name, string controllerName, out string[] searchedLocations) { searchedLocations = _emptyLocations; if (String.IsNullOrEmpty(name)) return String.Empty; if (IsSpecificPath(name)) return String.Empty; return GetPathFromGeneralName(controllerContext, locations, name, controllerName, ref searchedLocations); } private static bool IsSpecificPath(string name) { char c = name[0]; return (c == '~' || c == '/'); } private string GetPathFromGeneralName(ControllerContext controllerContext, string[] locations, string name, string controllerName, ref string[] searchedLocations) { var result = String.Empty; searchedLocations = new string[locations.Length]; for (int i = 0; i &lt; locations.Length; i++) { var location = locations[i]; var virtualPath = string.Format(CultureInfo.InvariantCulture, location, CultureInfo.CurrentUICulture.TwoLetterISOLanguageName, name, controllerName); if (FileExists(controllerContext, virtualPath)) { searchedLocations = _emptyLocations; result = virtualPath; break; } searchedLocations[i] = virtualPath; } return result; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload