Last active
January 6, 2016 08:05
-
-
Save elizabeth-young/5670625 to your computer and use it in GitHub Desktop.
Base MVC Controller using cultures - to be used with LocalisationHelper
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
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