Last active
April 23, 2020 12:35
-
-
Save ashour/f949cd673a5e382cf1a62a09902622b9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Globalization; | |
using System.Threading; | |
using System.Web.Mvc; | |
namespace I18nDemo.Controllers | |
{ | |
public class BaseController : Controller | |
{ | |
protected override void OnActionExecuting( | |
ActionExecutingContext filterContext) | |
{ | |
string culture = filterContext.RouteData.Values["culture"]?.ToString() | |
?? "en"; | |
// Set the action parameter just in case we didn't get one | |
// from the route. | |
filterContext.ActionParameters["culture"] = culture; | |
var cultureInfo = CultureInfo.GetCultureInfo(culture); | |
Thread.CurrentThread.CurrentCulture = cultureInfo; | |
Thread.CurrentThread.CurrentUICulture = cultureInfo; | |
// Because we've overwritten the ActionParameters, we | |
// make sure we provide the override to the | |
// base implementation. | |
base.OnActionExecuting(filterContext); | |
} | |
public ActionResult RedirectToLocalized() | |
{ | |
return RedirectPermanent("/en"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment