Skip to content

Instantly share code, notes, and snippets.

@elizabeth-young
Last active January 6, 2016 08:05
Show Gist options
  • Save elizabeth-young/5670625 to your computer and use it in GitHub Desktop.
Save elizabeth-young/5670625 to your computer and use it in GitHub Desktop.
Base MVC Controller using cultures - to be used with LocalisationHelper
public abstract partial class BaseController : Controller
{
private const string _langCookie = "lang";
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
SetCurrentCulture();
base.OnActionExecuting(filterContext);
}
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
}
private void SetCurrentCulture()
{
if(Request.Cookies[_langCookie] == null || String.IsNullOrEmpty(Request.Cookies[_langCookie].Value))
{
string culture;
if(Request.UserLanguages != null && Request.UserLanguages[0] != null)
{
culture = LocalisationHelper.GetCulture(Request.UserLanguages[0]);
}
else
{
culture = LocalisationHelper.DefaultCulture;
}
Response.Cookies.Add(new HttpCookie(_langCookie, culture));
}
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.Cookies[_langCookie].Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment