Best place to set CurrentCulture for multilingual ASP.NET MVC web applications
NickName:tugberk Ask DateTime:2011-11-22T20:05:37

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

For multilingual ASP.NET MVC 3 web application, I am determining the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture on the controller factory as follows:

public class MyControllerFactory : DefaultControllerFactory {

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) {

        //Get the {language} parameter in the RouteData
        string UILanguage;
        if (requestContext.RouteData.Values["language"] == null)
            UILanguage = "tr";
        else
            UILanguage = requestContext.RouteData.Values["language"].ToString();

        //Get the culture info of the language code
        CultureInfo culture = CultureInfo.CreateSpecificCulture(UILanguage);
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;

        return base.GetControllerInstance(requestContext, controllerType);
    }

}

The above code is nearly a year old now! So, I open for suggestions.

And I register this on the Global.asax file like:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

This is working good but I am not sure if it is the best practice and best place to do this type of action.

I haven't dug into the main role of ControllerFactory and I am unable to compare it against ActionFilterAttribute.

What do you think about the best place to do this type of action?

Copyright Notice:Content Author:「tugberk」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8226514/best-place-to-set-currentculture-for-multilingual-asp-net-mvc-web-applications

Answers
Agile Jedi 2014-11-20T16:40:06

I know an anser has already been selected. The option we used was to just Initialize the thread current culture in the OnBeginRequest event for the Application. This ensures the culture is discovered with every request\n\npublic void OnBeginRequest(object sender, EventArgs e)\n{\n var culture = YourMethodForDiscoveringCulutreUsingCookie();\n System.Threading.Thread.CurrentThread.CurrentCulture = culture;\n System.Threading.Thread.CurrentThread.CurrentUICulture = culture;\n}\n",


Paul Stovell 2011-11-22T12:19:13

An alternative place to put this would be to put that code in the OnActionExecuting method of a custom ActionFilter, which can be registered in the GlobalFilters collection:\n\nhttp://weblogs.asp.net/gunnarpeipman/archive/2010/08/15/asp-net-mvc-3-global-action-filters.aspx ",


Hitendra 2013-08-06T05:13:29

Instead of overriding OnActionExecuting you can override Initialize here like this\n\nprotected override void Initialize(RequestContext requestContext)\n{\n string culture = null;\n var request = requestContext.HttpContext.Request;\n string cultureName = null;\n\n // Attempt to read the culture cookie from Request\n HttpCookie cultureCookie = request.Cookies[\"_culture\"];\n if (cultureCookie != null)\n cultureName = cultureCookie.Value;\n else\n cultureName = request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages\n\n // Validate culture name\n cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe\n\n if (request.QueryString.AllKeys.Contains(\"culture\"))\n {\n culture = request.QueryString[\"culture\"];\n }\n else\n {\n culture = cultureName;\n }\n\n Uitlity.CurrentUICulture = culture;\n\n base.Initialize(requestContext);\n }\n",


s.ermakovich 2012-06-16T15:35:19

I used a global ActionFilter for this, but recently I realized, that setting the current culture in the OnActionExecuting 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, DisplayName attribute values, and other Data Annotations stuff are handled using the default culture at this point.\n\nEventually I've moved setting the current culture to the custom IControllerActivator 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 ActionFilter.\n\nCultureAwareControllerActivator.cs:\n\npublic class CultureAwareControllerActivator: IControllerActivator\n{\n public IController Create(RequestContext requestContext, Type controllerType)\n {\n //Get the {language} parameter in the RouteData\n string language = requestContext.RouteData.Values[\"language\"] == null ?\n \"tr\" : requestContext.RouteData.Values[\"language\"].ToString();\n\n //Get the culture info of the language code\n CultureInfo culture = CultureInfo.GetCultureInfo(language);\n Thread.CurrentThread.CurrentCulture = culture;\n Thread.CurrentThread.CurrentUICulture = culture;\n\n return DependencyResolver.Current.GetService(controllerType) as IController;\n }\n}\n\n\nGlobal.asax.cs:\n\npublic class MvcApplication : System.Web.HttpApplication\n{\n protected void Application_Start()\n {\n ...\n ControllerBuilder.Current.SetControllerFactory(new DefaultControllerFactory(new CultureAwareControllerActivator()));\n }\n}\n",


More about “Best place to set CurrentCulture for multilingual ASP.NET MVC web applications” related questions

Best place to set CurrentCulture for multilingual ASP.NET WebAPI applications

based on this question (Best place to set CurrentCulture for multilingual ASP.NET MVC web applications) I'm looking for a global way to do the same for ASP.NET WebAPI. My current implementation fo...

Show Detail

Best place to set CurrentCulture for multilingual ASP.NET MVC web applications

For multilingual ASP.NET MVC 3 web application, I am determining the Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture on the controller factory as follows: public class

Show Detail

What is the best way to make Angularjs application as multilingual if using ASP.net WEB api?

I have worked on the application that was multilingual. We made that application in ASP.Net MVC 5 and front end was in (jquery and javascript modular pattern). We implemented localization using res...

Show Detail

ASP.NET MVC 4 multilingual web template

I'm thinking about making a multilingual website that supports arabic, english and some more languages. Does anyone know where to download or buy ASP.NET MVC 4 multilingual web template?

Show Detail

Multilingual App Toolkit for web applications

I have come across this very interesting tool for Visual Studio 2012 development of windows apps called Multilingual App Toolkit - further information here This is very clever how it generates

Show Detail

Multilingual Application in ASP.NET MVC - Best Practices?

I'm developing a web application using ASP.NET MVC (I'm new to the framework and actually quite new to web development in general). My application must support multiple languages - there's a bunch of

Show Detail

Best practices for model definition in ASP.NET MVC

Recently I've read, and implemented 80% of examples from Pro ASP.NET MVC Framework, and it seems like the authors have only given the basics. After reading this I did become able to build web

Show Detail

Where should I set the language (CurrentThread.CurrentCulture)?

In older asp.net - projects we used to set the language usually within the Application_BeginRequest - Handler (Global.asax), something like this: System.Threading.Thread.CurrentThread.CurrentCultu...

Show Detail

converting existing website to multilingual website of asp.net mvc

I want to convert my existing website which is created in asp.net mvc to a multilingual without resource file.I have a lot of client side content (obtained through ajax in javascript) what is the ...

Show Detail

Implementing Multilingual web site using ASP.Net

I am implementing an multilingual web site using ASP.Net. The languages are English,French and Arabic. Please note that Arabic's direction is RTL. What is the best way to implement the multilingual

Show Detail