Skip to content

Instantly share code, notes, and snippets.

@ashour
Last active April 23, 2020 12:35
Show Gist options
  • Save ashour/f949cd673a5e382cf1a62a09902622b9 to your computer and use it in GitHub Desktop.
Save ashour/f949cd673a5e382cf1a62a09902622b9 to your computer and use it in GitHub Desktop.
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