Created
May 1, 2012 19:15
-
-
Save brendanmckenzie/2570631 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Globalization; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
using System.Web.Mvc; | |
namespace MyNamespace | |
{ | |
public class MyViewEngine : RazorViewEngine | |
{ | |
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) | |
{ | |
partialPath = GetViewPath(controllerContext, partialPath); | |
return base.CreatePartialView(controllerContext, partialPath); | |
} | |
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) | |
{ | |
viewPath = GetViewPath(controllerContext, viewPath); | |
return base.CreateView(controllerContext, viewPath, masterPath); | |
} | |
private static string GetViewPath(ControllerContext controllerContext, string viewPath) | |
{ | |
var request = controllerContext.HttpContext.Request; | |
var session = controllerContext.HttpContext.Session; | |
var culture = new CultureInfo(session.GetCulture()); | |
var paths = new List<string>(); | |
string[] parms = null; | |
if (controllerContext.RouteData.DataTokens.ContainsKey("org")) | |
{ | |
parms = new string[] { culture.Name, culture.TwoLetterISOLanguageName, (string)controllerContext.RouteData.DataTokens["org"] }; | |
if (session.IsMobile()) | |
{ | |
paths.AddRange(new string[] { | |
string.Format("~/Views/Clients/{2}/Mobile/i18n/{0}/", parms), // mobile client + locale | |
string.Format("~/Views/Clients/{2}/Mobile/i18n/{1}/", parms), // mobile client + language | |
string.Format("~/Views/Clients/{2}/Mobile/", parms), // mobile client | |
}); | |
} | |
paths.AddRange(new string[] { | |
string.Format("~/Views/Clients/{2}/i18n/{0}/", parms), // client + locale | |
string.Format("~/Views/Clients/{2}/i18n/{1}/", parms), // client + language | |
string.Format("~/Views/Clients/{2}/", parms), // client | |
}); | |
} | |
else | |
{ | |
parms = new string[] { culture.Name, culture.TwoLetterISOLanguageName }; | |
} | |
if (session.IsMobile()) | |
{ | |
paths.AddRange(new string[] { | |
string.Format("~/Views/Mobile/i18n/{0}/", parms), // mobile client + locale | |
string.Format("~/Views/Mobile/i18n/{1}/", parms), // mobile client + language | |
string.Format("~/Views/Mobile/", parms), // mobile client | |
}); | |
} | |
paths.AddRange(new string[] { | |
string.Format("~/Views/i18n/{0}/", parms), // global + locale | |
string.Format("~/Views/i18n/{1}/", parms) // global + language | |
}); | |
foreach (var path in paths) | |
{ | |
var localizedViewPath = Regex.Replace(viewPath, "^~/Views/", path); | |
if (File.Exists(request.MapPath(localizedViewPath))) | |
{ | |
viewPath = localizedViewPath; | |
} | |
} | |
return viewPath; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment