Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used a global <code>ActionFilter</code> for this, but recently I realized, that setting the current culture in the <code>OnActionExecuting</code> method is too late in some cases. For example, when model after POST request comes to the controller, ASP.NET MVC creates a metadata for model. It occurs before any actions get executed. As a result, <code>DisplayName</code> attribute values, and other Data Annotations stuff are handled using the default culture at this point.</p> <p>Eventually I've moved setting the current culture to the custom <code>IControllerActivator</code> implementation, and it works like a charm. I suppose it's almost the same from the request lifecycle perspective to host this logic in the custom controller factory, like you have today. It's much more reliable, than usage of global <code>ActionFilter</code>.</p> <p><strong>CultureAwareControllerActivator.cs</strong>:</p> <pre><code>public class CultureAwareControllerActivator: IControllerActivator { public IController Create(RequestContext requestContext, Type controllerType) { //Get the {language} parameter in the RouteData string language = requestContext.RouteData.Values["language"] == null ? "tr" : requestContext.RouteData.Values["language"].ToString(); //Get the culture info of the language code CultureInfo culture = CultureInfo.GetCultureInfo(language); Thread.CurrentThread.CurrentCulture = culture; Thread.CurrentThread.CurrentUICulture = culture; return DependencyResolver.Current.GetService(controllerType) as IController; } } </code></pre> <p><strong>Global.asax.cs</strong>:</p> <pre><code>public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ... ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new CultureAwareControllerActivator())); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload